iOS14新增了 精确定位 和 模糊定位 的概念,用户可以手动选择,模糊定位的误差约 500m 。可以根据实际功能判断是否可以接受用户选择模糊定位。
iOS14 授权弹窗新增的 Precise的开关默认会选中精确位置。用户通过这个开关可以进行更改,当把这个值设为 On 时,地图上会显示精确位置;切换为Off时,将显示用户的大致位置。
用户可以通过在 “隐私设置” 中设置来开启精确定位,但是若用户不愿意开启。这个时候,iOS14 在 CLLocationManager 新增两个方法用于向用户申请临时开启一次精确位置权限。
申请的单次精准定位本次APP生命周期内有效,APP一个生命周期内可以多次申请。任何一次允许后则可正常获取精准定位。申请方式:需要在info.plist中配置NSLocationTemporaryUsageDescriptionDictionary
:
NSLocationTemporaryUsageDescriptionDictionary的value是字典;
其中中需要配置 key 和 value 表明使用位置的原因,以及具体的描述。
不同产品场景可以定义不同的purposeKey。
<key>NSLocationTemporaryUsageDescriptionDictionary</key>
<dict>
<key> purposeKey </key>
<string>This app needs accurate location so it can verify that you're in a supported region. </string>
<key> AnotherPurposeKey </key>
<string>This app needs accurate location so it can show you relevant results.</string>
</dict>
requestTemporaryFullAccuracyAuthorizationWithPurposeKey
单独请求一次精确定位//参数 purposeKey 是在 info.plist 中设置NSLocationTemporaryUsageDescriptionDictionary 字典时候的key `
CLLocationManager *manager = [[CLLocationManager alloc] init];
manager.desiredAccuracy = kCLLocationAccuracyBest;
// 通过字段判断可以明确当前用户开启的定位,如果是模糊定位
// 申请精准定位
if (@available(iOS 14.0, *)) {
// -1
if( manager.accuracyAuthorization == CLAccuracyAuthorizationReducedAccuracy){
if (@available(iOS 14.0, *)) {
[manager requestTemporaryFullAccuracyAuthorizationWithPurposeKey:@"purposeKey4changeInfo" completion:^(NSError * _Nullable error) {
NSLog(@"%@", [NSString stringWithFormat:@"requestTemporaryFullAccuracyAuthorizationWithPurposeKey error%@",error]);
}];
} else {
// Fallback on earlier versions
}
}
} else {
// Fallback on earlier versions
}
这样app会获取到临时的精确位置权限直至下次冷启动。当然这个授权也可能被用户无情拒绝。
当然如果你使用第三方SDK进行定位的话,请升级版本即可
kCLLocationAccuracyReduced
是iOS14才支持的
*
CL_EXTERN const CLLocationAccuracy kCLLocationAccuracyBestForNavigation API_AVAILABLE(ios(4.0), macos(10.7));
CL_EXTERN const CLLocationAccuracy kCLLocationAccuracyBest;
CL_EXTERN const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;
CL_EXTERN const CLLocationAccuracy kCLLocationAccuracyHundredMeters;
CL_EXTERN const CLLocationAccuracy kCLLocationAccuracyKilometer;
CL_EXTERN const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;
/*
* kCLLocationAccuracyReduced
*
* Discussion:
* If you set your CLLocationManager's desiredAccuracy property to
* this value, locations delivered to your delegate in response to
* startUpdatingLocation or requestLocation will have their
* accuracy reduced. The locations you receive will match the
* locations your app would have received if the user had decided
* not to grant your app authorization for precise location.
*/
CL_EXTERN const CLLocationAccuracy kCLLocationAccuracyReduced API_AVAILABLE(ios(14.0), macos(11.0), watchos(7.0), tvos(14.0));
locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyReduced;
✎ 对于地理位置不敏感的App 来说,iOS14 也可以通过直接在 info.plist 中添加 NSLocationDefaultAccuracyReduced 为 true 默认请求大概位置。
设置之后,即使用户想要为该 App 开启精确定位权限,也无法开启。
pod 'AMapLocation', '2.6.7'
<key>NSLocationTemporaryUsageDescriptionDictionary</key>
<dict>
<key>purposeKey4changeInfo</key>
<string>此app需要精确的定位信息,以便于更好的为你服务. </string>
</dict>
if (@available(iOS 14.0, *)) {
self.location.locationAccuracyMode = AMapLocationFullAndReduceAccuracy;
} else {
// Fallback on earlier versions
}
/**
* @brief 当plist配置NSLocationTemporaryUsageDescriptionDictionary且desiredAccuracyMode设置CLAccuracyAuthorizationFullAccuracy精确定位模式时,如果用户只授权模糊定位,会调用代理的此方法。此方法实现调用申请临时精确定位权限API即可:
* [manager requestTemporaryFullAccuracyAuthorizationWithPurposeKey:@"PurposeKey" completion:^(NSError *error){
* if(completion){
* completion(error);
* }
* }]; (必须调用,不然无法正常获取临时精确定位权限)
* @param manager 定位 AMapLocationManager 类。
* @param locationManager 需要申请临时精确定位权限的locationManager。
* @param completion 临时精确定位权限API回调结果,error: 直接返回系统error即可。
* @since 2.6.7
*/
- (void)amapLocationManager:(AMapLocationManager *)manager doRequireTemporaryFullAccuracyAuth:(CLLocationManager*)locationManager completion:(void(^)(NSError *error))completion;
{
if(@available(iOS 14.0,*)){
[locationManager requestTemporaryFullAccuracyAuthorizationWithPurposeKey:@"purposeKey4changeInfo" completion:^(NSError * _Nullable error) {
if(completion){
completion(error);
}
}];
}
}
/**
记录当前是否向用户申请临时开启一次精确位置权限,用于【如果定位精度权限变更为精确的时候,再次更新定位信息】
*/
@property (assign, nonatomic) BOOL isrequestTemporaryFullAccuracyAuthorizationWithPurposeKey;
/**
存储获取定位信息的回调
*/
@property (nonatomic, copy) AMapLocatingCompletionBlock block;
/**
* @brief 定位权限状态改变时回调函数。注意:iOS14及之后版本回调
* @param manager 定位 AMapLocationManager 类。
* @param locationManager 定位CLLocationManager类,可通过locationManager.authorizationStatus获取定位权限,通过locationManager.accuracyAuthorization获取定位精度权限
*/
- (void)amapLocationManager:(AMapLocationManager *)manager locationManagerDidChangeAuthorization:(CLLocationManager*)locationManager{
//
// - 如果定位精度权限变更为精确的时候,再次更新定位信息
if (@available(iOS 14.0, *)) {
if( locationManager.accuracyAuthorization == CLAccuracyAuthorizationFullAccuracy){
if( self.isrequestTemporaryFullAccuracyAuthorizationWithPurposeKey == YES){
self.isrequestTemporaryFullAccuracyAuthorizationWithPurposeKey = NO;
[self.location requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
[SVProgressHUD dismiss];
if(self.block){
self.block(location,regeocode,error);
}
}];
}
}
} else {
// Fallback on earlier versions
}
}
在iOS14之后的SDK新增的API
-> Installing AMapLocation 2.6.7 (was 2.6.0)
> Http download
$ /usr/bin/curl -f -L -o /var/folders/6t/z1p2xgls4wz_pw36r58hhspr0000gn/T/d20200919-24852-1mkf6su/file.zip
https://amappc.oss-cn-zhangjiakou.aliyuncs.com/lbs/static/zip/AMap_iOS_Loc_Lib_V2.6.7.zip --create-dirs --netrc-optional --retry 2
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2165k 100 2165k 0 0 553k 0 0:00:03 0:00:03 --:--:-- 553k
$ /usr/bin/unzip /var/folders/6t/z1p2xgls4wz_pw36r58hhspr0000gn/T/d20200919-24852-1mkf6su/file.zip -d /var/folders/6t/z1p2xgls4wz_pw36r58hhspr0000gn/T/d20200919-24852-1mkf6su
Archive: /var/folders/6t/z1p2xgls4wz_pw36r58hhspr0000gn/T/d20200919-24852-1mkf6su/file.zip
creating: /var/folders/6t/z1p2xgls4wz_pw36r58hhspr0000gn/T/d20200919-24852-1mkf6su/AMapLocationKit.framework/
inflating: /var/folders/6t/z1p2xgls4wz_pw36r58hhspr0000gn/T/d20200919-24852-1mkf6su/AMapLocationKit.framework/AMapLocationKit
creating: /var/folders/6t/z1p2xgls4wz_pw36r58hhspr0000gn/T/d20200919-24852-1mkf6su/AMapLocationKit.framework/Headers/
inflating: /var/folders/6t/z1p2xgls4wz_pw36r58hhspr0000gn/T/d20200919-24852-1mkf6su/AMapLocationKit.framework/Headers/AMapLocationKit.h
inflating: /var/folders/6t/z1p2xgls4wz_pw36r58hhspr0000gn/T/d20200919-24852-1mkf6su/AMapLocationKit.framework/Headers/AMapGeoFenceError.h
inflating: /var/folders/6t/z1p2xgls4wz_pw36r58hhspr0000gn/T/d20200919-24852-1mkf6su/AMapLocationKit.framework/Headers/AMapGeoFenceManager.h
inflating: /var/folders/6t/z1p2xgls4wz_pw36r58hhspr0000gn/T/d20200919-24852-1mkf6su/AMapLocationKit.framework/Headers/AMapLocationRegionObj.h