2016-12-03 37 views
0

感谢以下Thomas的建议我从原始请求修改了我的代码。我想知道如果我试图做一些不可能的事情,或者如果不行,我该如何做到这一点。在应用程序位置设置更改后重新评估正在运行的应用程序中的CLLocationManager.authorizationStatus

如果用户没有授权位置服务,我通过一个提示与“打开设置”按钮来更改应用程序的位置设置。这工作。但是,从设置返回到应用程序,我想确认是否进行了更改并激活位置服务。这可以做到吗?下面的关闭成功地让用户进入应用程序的设置,用户可以进行更改,用户可以返回,但是当用户按下“打开设置”(在设置被更改之前)时触发关闭。顺便说一句:如果有更好的方法来处理用户批准应用程序当前未授权的位置首选项,我会很感激建议。谢谢!

locationManager.requestWhenInUseAuthorization() // request authorization 

let authStatus = CLLocationManager.authorizationStatus() 

switch authStatus { 
    case .denied: 
     let alertController = UIAlertController(title: "Background Location Access Disabled", message: "In order to show the location weather forecast, please open this app's settings and set location access to 'While Using'.", preferredStyle: .alert) 
     alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) 
     alertController.addAction(UIAlertAction(title: "Open Settings", style: .`default`, handler: { action in 
      if #available(iOS 10.0, *) { 
       let settingsURL = URL(string: UIApplicationOpenSettingsURLString)! 
       UIApplication.shared.open(settingsURL, options: [:], completionHandler: {(success) in 
        print("*** Success closure fires") 
        let newAuthStatus = CLLocationManager.authorizationStatus() 
        switch newAuthStatus { 
        case .authorizedWhenInUse: 
         self.locationManager.startUpdatingLocation() 
        default: 
         print("Not .authorizedWhenInUse") 
        } 
       }) 
      } else { 
       if let url = NSURL(string:UIApplicationOpenSettingsURLString) { 
        UIApplication.shared.openURL(url as URL) 
       } 
      } 
     })) 
     self.present(alertController, animated: true, completion: nil) 

    case .authorizedWhenInUse, .authorizedAlways: 
     locationManager.startUpdatingLocation() 
    case .restricted : 
     print("App is restricted, likely via parental controls.") 
    default: 
     print("UH!! WHAT OTHER CASES ARE THERE? ") 
    } 
+0

我想你是误会是'present'方法。当视图控制器完成呈现时,完成将会触发。我的描述中有点不清楚,但听起来您希望在用户选择了其中一个警报选项后触发此操作,因此此代码应位于UIAlertAction的完成处理程序之一中。 –

+0

谢谢,托马斯。这很有帮助。当用户点击“打开设置”按钮时,我可以移动闭包并执行它。但我想知道我是否在问不可能的事情。我希望在用户通过以下应用程序从应用程序设置返回到应用程序时再次测试授权:UIApplicationOpenSettingsURLString。当用户从更改设置返回时,是否可以以某种方式触发代码并检查我们现在是否具有授权?再次感谢您的建议! – Gallaugher

回答

1

我认为这里的扩展代码来完成我想要的东西 - 一个简单的获取地点,但它处理所有授权状态情况: - .notDetermined:requestWhenInUseAuthorization - .authorized:startUpdatingLocations - .denied:提示用户并使用UIApplicationOpenSettingsURLString打开应用的隐私/位置设置,以便他们可以进行更改。在didUpdateLocations中会返回具有新状态的应用程序,以便在更新设置后捕获用户位置 - .restricted - 显示一条警告,提示用户检查父级或系统管理员以解除应用程序限制。

如果发生didFailWithError,警报还会显示w /错误代码。

刚刚成立的LocationManager & currentLocation

let locationManager = CLLocationManager() 
var currentLocation: CLLocation! 

和viewDidLoad中的实例变量设置委托&调用的getLocation()

locationManager.delegate =自 的getLocation()

希望这是合理的,但是以更好的方式回答这个问题是最受欢迎的。希望它能帮助别人,因为我努力寻找全面的东西。再次感谢托马斯!

扩展视图控制器:CLLocationManagerDelegate {

func getLocation() { 

    let status = CLLocationManager.authorizationStatus() 

    handleLocationAuthorizationStatus(status: status) 
} 

func handleLocationAuthorizationStatus(status: CLAuthorizationStatus) { 
    switch status { 
    case .notDetermined: 
     locationManager.requestWhenInUseAuthorization() 
    case .authorizedWhenInUse, .authorizedAlways: 
     locationManager.startUpdatingLocation() 
    case .denied: 
     print("I'm sorry - I can't show location. User has not authorized it") 
     statusDeniedAlert() 
    case .restricted: 
     showAlert(title: "Access to Location Services is Restricted", message: "Parental Controls or a system administrator may be limiting your access to location services. Ask them to.") 
    } 
} 

func showAlert(title: String, message: String) { 
    let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) 

    let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil) 

    alertController.addAction(defaultAction) 

    present(alertController, animated: true, completion: nil) 
} 

func statusDeniedAlert() { 
    let alertController = UIAlertController(title: "Background Location Access Disabled", message: "In order to show the location weather forecast, please open this app's settings and set location access to 'While Using'.", preferredStyle: .alert) 
    alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) 
    alertController.addAction(UIAlertAction(title: "Open Settings", style: .`default`, handler: { action in 
     if #available(iOS 10.0, *) { 
      let settingsURL = URL(string: UIApplicationOpenSettingsURLString)! 
      UIApplication.shared.open(settingsURL, options: [:], completionHandler: nil) 
     } else { 
      if let url = NSURL(string:UIApplicationOpenSettingsURLString) { 
       UIApplication.shared.openURL(url as URL) 
      } 
     } 
    })) 
    self.present(alertController, animated: true, completion: nil) 
} 

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { 
    handleLocationAuthorizationStatus(status: status) 
} 

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    if let currentLocation = locations.last { 
     print("My coordinates are: \(currentLocation.coordinate.latitude), \(currentLocation.coordinate.longitude)") 
     locationManager.stopUpdatingLocation() 
     updateUserInterface() 
    } 
} 

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { 
    showAlert(title: "Location Access Failure", message: "App could not access locations. Loation services may be unavailable or are turned off. Error code: \(error)") 
} 

}

相关问题