美文网首页iOS Developer
黑心医院开发总结

黑心医院开发总结

作者: 千煌89 | 来源:发表于2016-08-15 11:14 被阅读606次

    莆田系医院作的恶已经人尽皆知,为了让家人远离附近的莆田系医院,我特意开发了一款APP供大家使用,现已上架AppStore,地址在这里,而因为实现简单又不涉及许多隐私,所以把这个app开源了,地址在这

    这是我首个个人作品,也是首个全用swift完成的作品。当然使用swift进行开发早在14年就开始了,不过因为版本更新太快,不得不放弃了后续维护,改用OC继续实现。如今swift的稳定性已经相当可观,第三方库的支持也比较到位了,是时候全面转向swift的怀抱了。今后的开源项目也将尽量都用swift来实现。

    黑心医院的实现非常简单:

    1. 从凤凰网获取名单,然后到高德地图上获取地理位置数据,并保存下来。
    2. 使用SwiftyJSON进行解析,使用Realm进行存储
    func loadData() -> Void {
          let realm = try! Realm()
          
          hospitals = realm.objects(Hospital)
          print("count = \(hospitals.count)")
          if hospitals.count == 0 {
              let path = NSBundle.mainBundle().pathForResource("hospital", ofType: "geojson")
              let data = NSFileManager.defaultManager().contentsAtPath(path!)
              
              let dic = try!  NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as? NSDictionary
              
              let json = JSON(dic!)
              
              print(json)
              
              let array = json["features"]
              for hos in array.array! {
                  let name = hos["properties"]["name"].string
                  let lng = hos["geometry"]["coordinates"][0].double
                  let lat = hos["geometry"]["coordinates"][1].double
                  
                  let hospital: Hospital = Hospital()
                  hospital.name = name!
                  hospital.lat = lat!
                  hospital.lng = lng!
                  
                  try! realm.write {
                      realm.add(hospital)
                  }
              }
              
              hospitals = realm.objects(Hospital)
          }
          
          print("count = \(hospitals.count)")
      }
    

    这里我曾经想把做好的realm数据库直接放进bundle并设为只读,然后直接读取bundle中的数据库,但是无奈并没有这个权限,只好放弃了。

    1. 使用MapKit进行地图绘制
    override func viewDidLoad() {
          super.viewDidLoad()
    
          // Do any additional setup after loading the view.
          
          locationManager.delegate = self
          locationManager.desiredAccuracy = kCLLocationAccuracyBest
          locationManager.requestWhenInUseAuthorization()
          locationManager.startUpdatingLocation()
          
          mapView.showsUserLocation = true
          for hospital in hospitals {
              let ann = MKPointAnnotation()
              ann.title = hospital.name
              let coordinate = CLLocation(latitude: hospital.lat, longitude: hospital.lng).coordinate
              ann.coordinate = coordinate
              mapView.addAnnotation(ann)
          }
      }
    
    //MARK: mapview delegate
      func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
          
          if annotation is MKUserLocation {
              //return nil so map view draws "blue dot" for standard user location
              return nil
          }
          
          let reuseId = "pin"
          
          var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
          if pinView == nil {
              pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
              pinView!.canShowCallout = true
              pinView!.animatesDrop = true
    //            pinView!.pinTintColor = UIColor.purpleColor()
              pinView!.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
              
          }
          else {
              pinView!.annotation = annotation
          }
          return pinView
      }
      
      var locationUpdated = false
      
      func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {
          if locationUpdated == false {
              
              //创建一个MKCoordinateSpan对象,设置地图的范围(越小越精确)
              let latDelta = 0.5
              let longDelta = 0.5
              let currentLocationSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
              
              //定义地图区域和中心坐标(
              //使用自定义位置
              let center:CLLocation = userLocation.location!
              let currentRegion:MKCoordinateRegion = MKCoordinateRegion(center: center.coordinate,
                                                                        span: currentLocationSpan)
              
              //设置显示区域
              mapView.setRegion(currentRegion, animated: true)
              locationUpdated = true
          }
      }
      
    
    1. 使用UISearchController进行检索
      本来检索想用UISearchDIsplayController做的,由于只支持最新版本,才发现这玩意已经被弃用了。从后面的版本开始需要使用UISearchController了。
    override func viewDidLoad() {
          super.viewDidLoad()
    
          // Uncomment the following line to preserve selection between presentations
          // self.clearsSelectionOnViewWillAppear = false
    
          // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
          // self.navigationItem.rightBarButtonItem = self.editButtonItem()
          searchController = UISearchController(searchResultsController: nil)
          searchController.delegate = self
          searchController.searchResultsUpdater = self
          searchController.dimsBackgroundDuringPresentation = false
          searchController.obscuresBackgroundDuringPresentation = false
          searchController.hidesNavigationBarDuringPresentation = false
          self.tableView.tableHeaderView = searchController.searchBar
      }
    
    func updateSearchResultsForSearchController(searchController: UISearchController) {
          let searchText = searchController.searchBar.text
          
          var arr = Array<Hospital>()
          for hospital in self.hospitals {
              if (hospital.name as NSString).containsString(searchText!) {
                  arr.append(hospital)
              }
          }
          searchResults = arr
          self.tableView.reloadData()
      }
    
    1. 使用UIActivityController 来分享
    func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
          let image = UIImage(named: "60")
          let url = NSURL(string: "http://finance.ifeng.com/a/20160504/14362042_0.shtml")
          let string = (view.annotation?.title!)!+"属于莆田系医院,小伙伴们千万不要去!"
          let activityController = UIActivityViewController(activityItems: [image! ,url!,string], applicationActivities: nil)
          self.presentViewController(activityController, animated: true, completion: nil)
      }
    

    原本想使用微信分享的,但一来微信分享需要引入微信库,二来还要各种依赖,集成好了之后又被我删掉了。反正自带的分享也可以支持微信的,而且代码相当简单。

    我也知道github上有些开源项目正在收集莆田系医院的数据,不过一来没提供获取数据与提交数据的接口,仍然是本地数据的模式,二来没有地理位置的信息,我觉得相比什么电话网址这些数据,地理位置更有用一些。所以目前我还是会维持使用本地数据的方式,直到有可用的接口为止。

    相关文章

      网友评论

        本文标题:黑心医院开发总结

        本文链接:https://www.haomeiwen.com/subject/kjrusttx.html