IOS请求授权定位服务

2018-10-27  本文已影响0人  安静的程序员

定位服务有两种授权模式:

一、请求“使用期间”使用定位服务

01.配置Info.plist文件

打开Info.plist,添加一个键值对,输入键:
NSLocationWhenInUseUsageDescription,按下“Enter”后,如果显示为:
Privacy - Location When In Use Usage Description,表示配置正确。

值需要输入你为什么要在应用“使用期间”使用定位服务的原因,必须填写,否则审核无法通过。

02.创建和配置CLLocationManager对象

02.1.要处理定位,首先要包含<CoreLocation/CoreLocation.h>

为了更快速的实现功能,我选择在新项目默认的ViewController类中添加CLLocationManager对象

ViewController.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController
{
    CLLocationManager * _locationManager;       // 位置管理器
}

@end
02.2.创建CLLocationManager

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (_locationManager == nil) {
        _locationManager = [CLLocationManager new];
    }
}

@end

03.开始请求授权

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (_locationManager == nil) {
        _locationManager = [CLLocationManager new];
    }
    
    if (_locationManager != nil) {
        // 请求“使用期间”使用定位服务
        [_locationManager requestWhenInUseAuthorization];
    }
}

04.处理授权结果

若要得知用户是“允许”还是“不允许”授权,需要继承一个协议,并绑定至_locationManager对象

04.1.继承协议并实现方法

修改ViewController.h,使之继承CLLocationManagerDelegate

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController<CLLocationManagerDelegate>
{
    CLLocationManager * _locationManager;       // 位置管理器
}

@end

修改ViewController.m,实现以下方法

// 当定位授权状态改变时
- (void)locationManager:(CLLocationManager *)manager did-ChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status)
    {
        case kCLAuthorizationStatusDenied:                  // 拒绝授权
            NSLog(@"授权失败:用户拒绝授权或未开启定位服务");
            break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:     // 在使用期间使用定位
            NSLog(@"授权成功:用户允许应用“使用期间”使用定位服务");
            break;
    }
}
04.2.以上仅仅是实现了方法,还需要将处理此方法的对象绑定至_locationManager对象

修改viewDidLoad方法,在请求授权前,绑定代理:

if (_locationManager != nil)
{
    // 绑定代理
    _locationManager.delegate = self;
    // 请求“使用期间”使用定位服务
    [_locationManager requestWhenInUseAuthorization];
}

二、请求“始终”使用定位服务

01.配置Info.plist文件

类似请求“使用期间”授权,添加一个键值对:
键:NSLocationAlwaysAndWhenInUseUsageDescription,输入后,如果显示“Privacy - Location Always and When In Use Us-age Description”,则配置正确。
值:输入为什么“始终”需要使用定位服务

重要
如果要请求“始终”使用定位服务,除了添加键:
NSLocationAlwaysAndWhenInUseUsageDescription
之外,还必须添加
NSLocationWhenInUseUsageDescription
否则,运行时会提示:
Info.plist must contain both “NSLocationAlwaysAndWhenInUseUsageDescription” and “NSLocationWhenInUseUsageDescription” keys
并且,请求授权的窗口也不会显示。

注意
如果你的应用需要支持iOS10以及更早版本,那么除了在Info.plist文件中包含以下键之外:
NSLocationWhenInUseUsageDescription
NSLocationAlwaysAndWhenInUseUsageDescription
还需额外包含此键:
NSLocationAlwaysUsageDescription
如果没有,请求会立即返回失败。
不过,我发现在iOS12上并不需要包含NSLocationAlwaysUsageDescription,我的Deployment Target选择的是10.0,可能只有在iOS10的手机上会返回失败。

02.请求“始终”使用定位服务

只需在上文的基础上修改viewDidLoad方法即可,修改请求即可:

if (_locationManager != nil)
{
    // 绑定代理
    _locationManager.delegate = self;
    // 请求“始终”使用定位服务
    [_locationManager requestAlwaysAuthorization];
}

注意:
官方文档上推荐,在请求“始终”使用定位服务之前,先请求“使用期间”授权。然后在稍后,再次请求“始终”授权。
我不清楚用意何在,据我测试,直接请求“始终”使用定位服务,好像也没什么问题。

03.处理授权结果

在上文的处理授权结果中,添加一个分支即可:

// 当定位授权状态改变时
- (void)locationManager:(CLLocationManager *)manager did-ChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status)
    {
        case kCLAuthorizationStatusDenied:                  // 拒绝授权
            NSLog(@"授权失败:用户拒绝授权或未开启定位服务");
            break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:     // 在使用期间使用定位
            NSLog(@"授权成功:用户允许应用“使用期间”使用定位服务");
            break;
        case kCLAuthorizationStatusAuthorizedAlways:
            NSLog(@"授权成功:用户允许应用“始终”使用定位服务");    // 始终使用定位服务
            break;
    }
}

最后,附上项目链接
https://pan.baidu.com/s/1yfjFnysqKHFHy6wEdRaZbg

上一篇 下一篇

猜你喜欢

热点阅读