2017-11-04 83 views
0

当用户在MainViewController如何检查是否用户点击不上NSLocationAlwaysAndWhenInUseUsageDescription雨燕3.2

override func viewDidLoad() { 
     super.viewDidLoad() 
     locationManager.delegate = self 
     locationManager.requestAlwaysAuthorization() 
     locationManager.requestWhenInUseAuthorization() 
} 

viewDidLoad()将运行并警告会出现

enter image description here

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key> 
     <string>Accessing user location to determine the nearest pickup location</string> 
     <key>NSLocationWhenInUseUsageDescription</key> 
     <string>Accessing user location to determine the nearest pickup location</string> 

的主要问题是,如果用户点击不允许,我该如何检查编程?例如

// Example code snippet, not from apple officials 
if NSlocation is nil then I want to do some alert 
+0

https://developer.apple.com/documentation/corelocation/cllocationmanagerdelegate/1423701-locationmanager – Paulw11

回答

2

如果用户选择拒绝定位服务您的应用程序访问,CLLocationManager.authorizationStatus().denied(虽然你也应该注意.restricted。如果你想通知当用户选择是否没有允许或拒绝您的应用程序,你可以让自己的位置经理delegate和实施正确的方法:

class MainViewController: CLLocationManagerDelegate{ 

    let locationManager = CLLocationManager() 

    override func viewDidLoad() -> Void{ 
     super.viewDidLoad() 
     locationManager.delegate = self 
    } 

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) -> Void{ 
     switch status{ 
      case .authorizedAlways, .authorizedWhenInUse: 
       // You're allowed to use location services 
      case .denied, .restricted: 
       // You can't use location services 
      default: 
       // This case shouldn't be hit, but is necessary to prevent a compiler error 
     } 
    } 
} 
+0

非常感谢!它确实有效 – sinusGob

+0

如果您不介意,还有一个问题,我如何引导用户进入应用程序的设置,以便用户可以切换位置 – sinusGob

+0

https://stackoverflow.com/a/5655727/2895075 –

0

低于CLLocationManager委托方法用于检查用户点击不要让许可警报按钮。

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { 
     switch status { 
     case .denied: 
      print("Show you own alert here") 
      break 
     } 
    } 

要位置设置重定向用户下面如下:

  1. 添加URL类型 “首选项” 像下面的图片 enter image description here

  2. 编写代码上的按钮动作:

    UIApplication.sharedApplication().openURL(NSURL(string:"prefs:root=LOCATION_SERVICES")!)