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

标签 iphone cllocationmanager

CLLocationManager 提示警报

"App name" Would you like to use your location

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

最佳答案

当您单击“不允许”按钮时

- (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];

     }
}

编辑

替代:您可以显示一条警报,要求用户通过从设置启用位置服务来允许位置访问。

您可以在 iOS 5.0 及更高版本上使用此功能:

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

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

关于iphone - 如何知道CLLocationManager用户触摸了哪个警报按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9109316/

相关文章:

iphone - EAGLView 到 UIImage 颜色转换问题

iphone - 为什么 iPhone 没有 NSArrayController?

iOS 区域监控 : Is my app crashing or is this expected?

javascript - 从 obj c 类调用 javascript 函数对我不起作用

ios - 如何在 objective-c 中将这两个值加在一起

iphone - 是否可以大规模更新多个iPhone应用程序?

iphone - 在 MKMapView 上获取用户当前位置

ios - 你如何获得特定设备的当前位置?

ios - 应用程序终止/暂停时的后台用户位置

iOS 9 didRangeBeacons 调用了更多次。 iOS-8 每秒一次