想通过xcode启用位置服务

时间:2016-01-23 08:09:50

标签: ios objective-c

我是ios的新手,我想知道如何在下面使用此功能时通过编程启用位置服务....

enter image description here

我正在使用此标签显示每当禁用位置服务时启用按钮(绿色)以启用位置服务。但是如何以编程方式进行此操作。我将如何了解位置服务已禁用以及如何按钮将启用此功能。请帮帮我。

1 个答案:

答案 0 :(得分:2)

您可以使用 CLLocationManager的locationServicesEnabled 属性来检查系统范围/整体可用性。使用 CLLocationManagerDelegate的locationManager:didFailWithError:方法并检查 kCLErrorDenied 错误,以查看用户是否拒绝了位置服务。

if([CLLocationManager locationServicesEnabled]){

           NSLog(@"Location Services Enabled");

           if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied){
                 [[[UIAlertView alloc] initWithTitle:@"App Permission Denied" 
                                                        message:@"To re-enable, please go to Settings and turn on Location Service for this app." 
                                                       delegate:nil 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles:nil]show];

            }
        }

<强>选择-2

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{

    NSLog(@"%@",error);
}
  

如果您的应用禁用了位置服务,那么它会给您错误

     

错误域= kCLErrorDomain代码= 1&#34;无法完成操作。 (kCLErrorDomain错误1。)&#34;

<强>更新

如果用户拒绝了该权限,我们无法以编程方式启用,用户可以手动启用位置服务,以获得更多reference