0

当用户开启时MainViewController

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

viewDidLoad()将运行并出现警报

在此处输入图像描述

<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
4

2 回答 2

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
        }
    }
}
于 2017-11-04T05:10:23.540 回答
0

使用下面的委托方法CLLocationManager来检查用户单击权限警报中的不允许按钮。

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

要在位置设置中重定向用户,请执行以下操作:

  1. 添加 URL 类型“首选项”,如下图所示 在此处输入图像描述

  2. 在按钮操作上编写此代码:

    UIApplication.sharedApplication().openURL(NSURL(string:"prefs:root=LOCATION_SERVICES")!)
    
于 2017-11-04T05:25:09.303 回答