iphone 位置服务警报回调允许和不允许处理

iphone location service alert call back allow and don't allow handling

我有一个按钮,单击该按钮我将授权我的应用程序启用定位服务。我想从警报中捕获允许按钮事件。当用户按下允许时,我的按钮颜色将更改为其他颜色。我无法捕获 allow 按钮的回调。但是,借助此 link

,我能够捕获 不允许 按钮事件

Location service iOS alert call back

任何人都可以告诉我如何捕获允许按钮点击并成功后我想执行我的代码。

执行这个:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
        //allowed - do your logic
    }
    else if (status == kCLAuthorizationStatusDenied) {
        //denied
    }
}

Swift 4/5:

self.locationManager.requestAlwaysAuthorization()

然后在委托方法中处理:

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    switch status {
    case .notDetermined:
        manager.requestAlwaysAuthorization()
    case .restricted, .denied:
        // location services unauthorized
    case .authorizedAlways, .authorizedWhenInUse:
        manager.startUpdatingLocation()
    @unknown default:
        print("New CLLocationManager.authorizationStatus() not handled!")
    }    
}

记得设置locationManager委托:

self.locationManager.delegate = self;

并通过您的 class 实施 CLLocationManagerDelegate