检查位置权限集

时间:2018-10-23 09:29:02

标签: ios swift core-location cllocationmanager ios-permissions

我的iOS应用已被苹果拒绝,因为如果用户选择“不允许”位置访问,该应用将崩溃。然后点击我的地图按钮。

如何包装此按钮以查看用户是否已获得许可,如果没有,我该如何再次请求许可?

//Map Button Action - Opens Maps - Gives choice of Google or Apple maps
  @IBAction func googleMapBtn(_ sender: UIButton) {
    UIDevice.current.isBatteryMonitoringEnabled = true
    let state = UIDevice.current.batteryState
    //If user is in Loop - Cant open maps
    if state == .charging {
      print("In Loop - Cant open maps")
    }
      //Present Map Options
    else {
      let alertController = UIAlertController.init(title: "Open Map", message: "", preferredStyle: .alert)
      alertController.addAction(UIAlertAction.init(title: "Google Maps", style: .default, handler: { (action) in
        self.googleMapsOpen()
      }))
      alertController.addAction(UIAlertAction.init(title: "Apple Maps", style: .default, handler: { (action) in
        self.appleMapsOpen()
      }))
      alertController.addAction(UIAlertAction.init(title: "Back", style: .default, handler: { (action) in
        self.dismiss(animated: true, completion: nil)
      }))
      self.present(alertController, animated: true) {
      }
    }
  }

每当用户选择Google / Apple地图类型并执行self.googleMapsOpen()或self.appleMapsOpen()时,代码就会崩溃。特别是在let scheme =

上崩溃了
func googleMapsOpen(){
    print("Google Maps Pressed")
    let scheme  = "comgooglemaps://?center=\(LocationManager.sharedInstance.location.coordinate.latitude),\(LocationManager.sharedInstance.location.coordinate.longitude)&zoom=15"
    self.open(scheme: scheme)
}

func appleMapsOpen(){
    print("Apple Maps Pressed")
    let scheme  = "http://maps.apple.com/?ll=\(LocationManager.sharedInstance.location.coordinate.latitude),\(LocationManager.sharedInstance.location.coordinate.longitude)&zoom=15"
    self.open(scheme: scheme)
}

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

func checkLocationPermissionEnabled()
    {
        if CLLocationManager.locationServicesEnabled()
        {
            switch(CLLocationManager.authorizationStatus())
            {
            case .notDetermined, .restricted, .denied:
                self.openDeviceLocationSetting()
                return
            case .authorizedAlways, .authorizedWhenInUse:
                //do whatever you want to do with location
                return
            }
        }
        else
        {
            self.openDeviceLocationSetting()
            return
        }
    }


    func openDeviceLocationSetting()
    {
        let alertController = UIAlertController(title: "", message:"For best results, let your device turn on location using Google's location service.", preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
            UIAlertAction in
            self.isAlertShowing = false
            let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString)
            if let url = settingsUrl {
                UIApplication.shared.openURL(url as URL)
            }
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default) {
            UIAlertAction in

        }
        alertController.addAction(okAction)
        alertController.addAction(cancelAction)
        self.present(alertController, animated: true, completion: nil)
    }