2012-02-02 64 views
2

CLLocationManager提示警报如何知道CLLocationManager使用哪个警报按钮?

“应用名称” 你想使用你的位置

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

+0

为什么你不检查你是否有权访问用户的位置?如果你这样做,他选择确定。 – Kheldar 2012-02-02 08:45:34

回答

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进行位置访问。在您的应用程序

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

打开设置应用:

您可以在iOS 5.0和更高版本使用。

+0

我在应用程序中这样做了两次。一旦进入appdelegate,然后进入我有mapview的控制器之一。如果用户在appdelegate CLLocationManager警报中选择“确定”,则应用程序不应在ma控制器内提示此警报。但是,如果用户选择不允许在开始,应用程序应该在地图控制器内提示此警报。这个怎么做? – Nitish 2012-02-02 08:55:45

+0

@Nitish:当用户选择不允许并在地图控制器中检查该标志时设置一个标志。如果标志为假,则请求用户允许。 – Maulik 2012-02-02 09:00:51

+0

这不是我所要求的。如果用户选择不允许在应用程序启动时,CLLocationManager应该在地图控制器中提示相同的警报。如果用户在开始时选择了“确定”,则地图控制器中不应出现警报。 – Nitish 2012-02-02 09:03:55

4

实施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. 

讨论

这种方法被称为每当应用程序的能力,以使用位置SERVIC es变化。 可能会发生更改,因为用户允许或拒绝为您的应用程序或整个系统使用位置服务