首页 技术 正文
技术 2022年11月8日
0 收藏 528 点赞 1,438 浏览 2753 个字

苹果在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:来观察。

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,071
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,549
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,397
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,174
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,809
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,889