检查位置权限集

Check of Location permissions set

我的 iOS 应用程序已被 Apple 拒绝,因为如果用户选择 "Dont allow" 位置访问权限,该应用程序就会崩溃。然后点击我的地图按钮的收益。

我如何包装这个按钮以检查用户是否已授予权限,如果没有我如何再次请求权限?

//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)
}

你可以这样做:

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)
    }