Проверка разрешений местоположения установлена - PullRequest
0 голосов
/ 23 октября 2018

Мое приложение iOS было отклонено Apple, так как приложение аварийно завершает работу, если пользователь выбрал «Не разрешать» доступ к местоположению.И приступает к нажатию на кнопку «Моя карта».

Как обернуть эту кнопку в проверку, чтобы увидеть, было ли предоставлено разрешение пользователю, и если нет, то как я могу запросить разрешение еще раз?

//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 =

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 голосов
/ 23 октября 2018

Вы можете сделать что-то вроде этого:

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