(七十五)CoreLocation(一)在iOS7和iOS8设备上获取授权

苹果在iOS8上更新了CoreLocation的授权获取方式,在原来的基础上,不仅需要调用授权函数,还需要对info.plist进行相应的配置。

在iOS上获取经纬度使用的是CoreLocationManager,它来自CoreLocation.framework框架,使用时应当包含框架的总头文件:

#import <CoreLocation/CoreLocation.h>

一般是先创建管理者,然后成为其代理,对于iOS7,直接调用startUpdatingLocation即可开始监听,而对于iOS8,还需要调用一个方法进行授权,根据使用状况的不同,有两种授权,分别是使用时获取位置和持续获取位置,根据需要的不同进行调用:

[self.manager requestAlwaysAuthorization];
[self.manager requestWhenInUseAuthorization];
一般直接获取第一个即可,应用面更广。

除此之外,在info.plist中加入两个键,值为string,string中的内容就是请求授权时显示的说明:

对应于两种授权,有NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription两个键。

在每次开启应用时,会调用一个代理方法来判断授权状态:通过这个方法判断是否授权成功,如果成功则调用startUpdatingLocation来开始定位。

// 授权状态发生改变时调用
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    /*
    typedef NS_ENUM(int, CLAuthorizationStatus) {
        // User has not yet made a choice with regards to this application
        kCLAuthorizationStatusNotDetermined = 0,
        
        // 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
        kCLAuthorizationStatusRestricted,
        
        // User has explicitly denied authorization for this application, or
        // location services are disabled in Settings.
        kCLAuthorizationStatusDenied,
        
        // User has granted authorization to use their location at any time,
        // including monitoring for regions, visits, or significant location changes.
        kCLAuthorizationStatusAuthorizedAlways NS_ENUM_AVAILABLE(NA, 8_0),
        
        // User has granted authorization to use their location only when your app
        // is visible to them (it will be made visible to them if you continue to
        // receive location updates while in the background).  Authorization to use
        // launch APIs has not been granted.
        kCLAuthorizationStatusAuthorizedWhenInUse NS_ENUM_AVAILABLE(NA, 8_0),
        
        // This value is deprecated, but was equivalent to the new -Always value.
        kCLAuthorizationStatusAuthorized NS_ENUM_DEPRECATED(10_6, NA, 2_0, 8_0, "Use kCLAuthorizationStatusAuthorizedAlways") = kCLAuthorizationStatusAuthorizedAlways
    };*/
    
    switch (status) {
        case kCLAuthorizationStatusAuthorizedAlways:
            NSLog(@"持续使用授权");
            [self.manager startUpdatingLocation];
            break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:
            NSLog(@"使用中授权");
            [self.manager startUpdatingLocation];
            break;
        case kCLAuthorizationStatusDenied:
            NSLog(@"授权被拒绝");
            break;
        case kCLAuthorizationStatusNotDetermined:
            NSLog(@"等待用户授权");
            break;
        default:
            break;
    }
    
    
}

因为iOS7直接开始获取位置即可,因此应当对两种系统区分对待,用UIDevice获取到系统版本,针对不同的系统进行不同的处理,如果是iOS8以前的系统,直接开始获取位置:

- (void)viewDidLoad {

    [super viewDidLoad];
    
    // 1.创建CoreLocationManager
    CLLocationManager *manager = [[CLLocationManager alloc] init];
    self.manager = manager;
    // 2.成为其代理
    self.manager.delegate = self;
    
    if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
        [self.manager requestAlwaysAuthorization];
    }else{
        // iOS8以前直接开始定位
        [self.manager startUpdatingLocation];
    }
    
}

要判断是否成功的开始定位,实现代理方法locationManager:didUpdateLocations:来观察。

有关经纬度、方向、区域判断等请见下节。



posted on 2015-07-19 15:42  张大大123  阅读(148)  评论(0编辑  收藏  举报

导航