如何知道用户为CLLocationManager触摸了哪个警报按钮?

问题描述 投票:2回答:2

CLLocationManager提示警报

“应用名称”您想使用您的位置吗?

它提供了两个按钮,OK和Do not allow。如何知道用户选择了哪些按钮?

iphone cllocationmanager
2个回答
5
投票

单击“不允许”按钮时

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 将被调用kCLAuthorizationStatusDenied例外。你可以把它写在里面。

另请参阅:

kCLAuthorizationStatusNotDetermined = 0, // User has not yet made a choice with regards to this application
kCLAuthorizationStatusRestricted,        // This application is not authorized to use location services.  Due
                                             // to active restrictions on location services, the user cannot change
                                             // this status, and may not have personally denied authorization
kCLAuthorizationStatusDenied,            // User has explicitly denied authorization for this application, or
                                             // location services are disabled in Settings
kCLAuthorizationStatusAuthorized         // User has authorized this application to use location services

示例:

如果用户点击允许那么

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
      [self refreshYourView];
}

如果点击不允许

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{
    if ([error code]== kCLAuthorizationStatusDenied) 
    {
         UIAlertView *alert;
    alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"User has clicked don't allow button." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];

     }
}

编辑

备用:您可以通过Settings启用位置服务来显示要求用户允许位置访问的警报。

您可以在iOS 5.0及更高版本上使用它:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs://"]];

在您的应用中打开“设置”应用。


5
投票

实施CLLocationManagerDelegate Protocol

的LocationManager:didChangeAuthorizationStatus:

告知代理该应用程序的授权状态已更改。

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

参数

经理

 The location manager object reporting the event.

状态

The new authorization status for the application.

讨论

只要应用程序使用位置服务的能力发生变化,就会调用此方法。由于用户允许或拒绝为您的应用程序或整个系统使用位置服务,因此可能会发生更改。

© www.soinside.com 2019 - 2024. All rights reserved.