ios - 如何首先为 'While Using' 请求 GPS 许可,然后为 'Always' 请求 GPS 许可? (2 个步骤)

标签 ios geolocation

我正在设计一款支持签到的应用。

使用应用程序时的 GPS 权限是必须。但是有一个使用地理围栏自动 checkin 的可选功能,它需要“始终”gps 权限。

我想首先请求常规的“使用时”权限。然后,仅当用户想要自动 checkin 时,请求“始终”权限。

这可能吗?

最佳答案

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

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

-->在“myclass.h”中将委托(delegate)设置为“CLLocationManagerDelegate”,它会在第一次使用时要求您提供 GPS,并将以下代码写入“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

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

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

关于ios - 如何首先为 'While Using' 请求 GPS 许可,然后为 'Always' 请求 GPS 许可? (2 个步骤),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31469573/

相关文章:

iphone - 有没有获取全局当前时间的API

ios - 动态添加项目到 Xamarin CarouselPage 在 IOS 中不起作用

ios - 使用新的核心数据初始化 RestKit 的 RKPaginator

ios - 模拟器启动等待 120 秒超时

javascript - 从谷歌地点获取附近的位置并从结果中在谷歌地图上添加标记

ruby-on-rails - 无法在我的 Controller 中使用 Geocoder 类

ios - 从 NSDate 获取百分之一秒的差异

php - 如何测试网站的地理定位?

android - broadcastReceiver 可以捕获多个广播吗?

android - 获得 10 米精度 GPS 定位的合理时间是多少?