Location service iOS alert call back

不打扰是莪最后的温柔 提交于 2019-11-28 00:41:34

问题


When we use location services in an application, we receive an iOS alert saying the application is trying to use the current location -- Allow/Don't Allow.

Do we have a delegate call back for these buttons?

I want to handle tap on "Don't Allow".


回答1:


You don't have direct access to that alert.

If the user presses "Don't Allow", or if the app otherwise doesn't have permission to use location services then CLLocationManager will call locationManager:didFailWithError: on its delegate. The error domain will be kCLErrorDomain and the error code will be kCLErrorDenied.




回答2:


You can simply get the action selected like below:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
        [self addRegion];
    }
    else if (status == kCLAuthorizationStatusDenied) {
        NSLog(@"Location access denied");
    }
}

make sure to set the delegate of location manager.




回答3:


You should also check to see if the user has allowed location services for your app before starting the location manager. Use the CLLocationManager class method locationServicesEnabled to check.

Here's the doc:

locationServicesEnabled

Returns a Boolean value indicating whether location services are enabled on the device.

+ (BOOL)locationServicesEnabled

Return Value YES if location services are enabled or NO if they are not.

Discussion The user can enable or disable location services altogether from the Settings application by toggling the switch in Settings > General > Location Services.

You should check the return value of this method before starting location updates to determine if the user has location services enabled for the current device. If this method returns NO and you start location updates anyway, the Core Location framework prompts the user with a confirmation panel asking whether location services should be reenabled.



来源:https://stackoverflow.com/questions/5905940/location-service-ios-alert-call-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!