如何首先询问“使用时”的GPS权限,然后选择“始终”? (2个步骤)

时间:2015-07-17 06:27:18

标签: ios geolocation

我正在设计一个适用于 Check-Ins 的应用。

使用该应用时的GPS权限是必须。但是有一个可选功能用于使用Geofencing进行自动检入,这需要“始终”gps权限。

我想首先要求定期“使用时”权限。然后,仅在用户想要自动检入时,请求“始终”权限。

有可能吗?

1 个答案:

答案 0 :(得分:1)

please add these 3 keys in your project's .plist file

- >首先将这三个密钥添加到info.plist文件中,或者您可以根据需要添加第一个和第二个密钥或第一个和第三个密钥。

- >在" myclass.h"将委托设置为" CLLocationManagerDelegate"当它第一次使用时会询问你GPS,而下面的代码会在#34; myclass.m"文件

CLLocationManager *locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    [locationManager requestWhenInUseAuthorization];   //For while use the app
[locationManager requestAlwaysAuthorization]; // For always usage of GPS 
[locationManager startUpdatingLocation];


NSLog(@"%@",[NSString stringWithFormat:@"%f",locationManager.location.coordinate.latitude] );
NSLog(@"%@",[NSString stringWithFormat:@"%f",locationManager.location.coordinate.longitude] );

  - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"OldLocation %f %f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude);
NSLog(@"NewLocation %f %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);

}

- 注意:如果您想要始终使用GPS权限,则在使用许可时无需要求。

enter image description here

- >如果您添加了两个权限,那么您将在设备设置应用程序中获得此类选项,用户还可以设置何时使用哪些权限。一项权限不会同时显示在设备设置中。

设备设置 - >我的应用 - >允许我的应用访问(选择位置) - >位置访问权限