Location service iOS alert call back

后端 未结 3 707
迷失自我
迷失自我 2020-12-17 23:56

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 h

相关标签:
3条回答
  • 2020-12-18 00:12

    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.

    0 讨论(0)
  • 2020-12-18 00:15

    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.

    0 讨论(0)
  • 2020-12-18 00:29

    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.

    0 讨论(0)
提交回复
热议问题