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

时间:2012-02-02 08:42:57

标签: iphone cllocationmanager

CLLocationManager会提示警告

  

“应用名称”是否要使用您的位置

提供两个按钮,确定不允许。如何知道用户选择了哪些按钮?

2 个答案:

答案 0 :(得分: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://"]];
在您的应用中

打开“设置”应用。

答案 1 :(得分: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.

讨论

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