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

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

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

#import <CoreLocation/CoreLocation.h>

通常是先建立管理者,而后成为其代理,对于iOS7,直接调用 startUpdatingLocation便可开始监听,而对于iOS8,还须要调用一个方法进行受权,根据使用情况的不一样,有两种受权,分别是使用时获取位置和持续获取位置,根据须要的不一样进行调用:

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

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

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

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

// 受权状态发生改变时调用
-(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:来观察。

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

相关文章
相关标签/搜索