专栏首页iOSer定位权限请求时易犯的错误小结

定位权限请求时易犯的错误小结

起因

用户群反馈app可能请求了不合适的定位权限:始终定位。

看到这个截图,根据经验判断可能是后台定位功能导致可能不得不请求始终定位权限。再加上之前提交审核时,苹果要求在plist文件中新增NSLocationAlwaysAndWhenInUseUsageDescription和NSLocationAlwaysUsageDescription键值对。猜测可能是对某些点的理解混淆了,因为这种用户体验的确不好。

解决思路

因为此项目是前同事遗留代码,自从接手后迭代次数比较有限。于是计划根据以下步骤进行排查和验证:

  • 查现有代码和plist文件
  • 查官方文档
  • 改工程代码
  • 提交审核,查看邮件提醒
  • app退入后台,查看定位信息是否输出

查plist文件和权限请求代码

plist文件

以下键都已加入:

NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
NSLocationAlwaysAndWhenInUseUsageDescription

权限请求代码

全局搜索request,找到定位权限请求代码,发现

  if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
            [_locationManager requestAlwaysAuthorization];
   }

查官方文档

plist文件配置

  • NSLocationAlwaysUsageDescription

Discussion Use this key if your iOS app accesses location information in the background, and you deploy to a target earlier than iOS 11. In that case, add both this key and NSLocationAlwaysAndWhenInUseUsageDescription to your app’s Info.plist file with the same message. Apps running on older versions of the OS use the message associated with NSLocationAlwaysUsageDescription, while apps running on later versions use the one assocated with NSLocationAlwaysAndWhenInUseUsageDescription. If your app only needs location information when in the foreground, use NSLocationWhenInUseUsageDescription instead. For more information, see Choosing the Authorization Level for Location Services. If you need location information in a macOS app, use NSLocationUsageDescription instead. Important This key is required if your iOS app uses APIs that access the user’s location at all times and deploys to targets earlier than iOS 11.

  • NSLocationWhenInUseUsageDescription

Discussion Use this key if your iOS app accesses location information only when running in the foreground. If your app needs location information when in the background, use NSLocationAlwaysAndWhenInUseUsageDescription instead. For more information, see Choosing the Authorization Level for Location Services. If you need location information in a macOS app, use NSLocationUsageDescription instead. Important This key is required if your iOS app uses APIs that access the user’s location information while the app is running in the foreground.

  • NSLocationAlwaysAndWhenInUseUsageDescription

Discussion Use this key if your iOS app accesses location information while running in the background. If your app only needs location information when in the foreground, use NSLocationWhenInUseUsageDescription instead. For more information, see Choosing the Authorization Level for Location Services. If you need location information in a macOS app, use NSLocationUsageDescription instead. If your iOS app deploys to versions earlier than iOS 11, see NSLocationAlwaysUsageDescription. Important This key is required if your iOS app uses APIs that access the user’s location information at all times.

对比可知:

  • 如果app需要前台运行定位权限,需要配置NSLocationWhenInUseUsageDescription;
  • 如果app需要后台运行定位权限,需要配置NSLocationAlwaysAndWhenInUseUsageDescription;如果适配iOS11之前版本,还需要配置NSLocationAlwaysUsageDescription

这几种键的区别主要在于区分app 前后台运行 时的权限配置。

权限请求代码

requestAlwaysAuthorization

When the user grants “Always” authorization to your app, your app can start any of the available location services while your app is running in the foreground or background. In addition, services that allow your app to be launched in the background continue to do so. Important Requesting “Always” authorization is discouraged because of the potential negative impacts to user privacy. You should request this level of authorization only when doing so offers a genuine benefit to the user.

requestWhenInUseAuthorization

You must call this method or the requestAlwaysAuthorization()method prior to using location services. If the user grants “when-in-use” authorization to your app, your app can start most (but not all) location services while it is in the foreground. (Apps cannot use any services that automatically relaunch the app, such as region monitoring or the significant location change service.) When started in the foreground, services continue to run in the background if your app has enabled background location updates in the Capabilities tab of your Xcode project. Attempts to start location services while your app is running in the background will fail. The system displays a location-services indicator in the status bar when your app moves to the background with active location services.

官方建议:

The type of authorization (“when-in-use” or “always”) that you request determines which location services your app can access and when it can use them:

When-in-use authorization. Your app can use most services but cannot use services that automatically relaunch the app. Your app must always start services while running in the foreground. If you enable the background location capability for your app, a suspended app will wake in the background to handle location events. However, if your app is not running, it will not be launched.

Always authorization. Your app can use all location services, and it can start those services from either the foreground or the background. If a location-related event occurs when your app is not running, the system launches your app and delivers the event.

官方更详细说明:

https://developer.apple.com/documentation/corelocation/choosing_the_location_services_authorization_to_request

改工程代码

  • 保持plist配置不变
  • 修复请求方法:
  if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
            [_locationManager requestWhenInUseAuthorization];
  }

提交审核,查看邮件提醒

新增version,打包上传,查看邮件提醒:并未提及定位权限有何问题。

app退入后台,查看定位信息是否输出

为验证app退到后台依旧可以定位,故实测退到后台后,gps回调方法中log是否持续输出,结果:会!

结论

plist权限配置的定义和通过代码请求权限不是绝对的一一对应关系,容易被误解,前同事也是在这个地方混淆了。这也是本bug出现的根本原因。 以后遇到类似易混淆概念,还是查好多篇相关官方文档,并充分理解后再写代码比较好。关于本知识点,网上有的教程可能会混淆,请注意鉴别。

  • 让我们杜绝浅尝辄止,追求精益求精!

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 逻辑漏洞的原理及分析

    之前的文章都是写的SQL注入,命令执行,文件上传等一步到位的高危漏洞原理及防御,接下来说一说逻辑漏洞,因为现在工具的大量使用,之前的那些主流漏洞,很难被轻易利用...

    雪痕@
  • 【年度系列】经过多年交易之后你应该学到的东西(深度分享)

    公众号推送了很多技术类的文章,今天为大家带来一篇软文,直指交易实战。所有策略、算法等,可能都需要经过实践的检验和不断的改进才有可能为你带来一定的财富,但也不是永...

    量化投资与机器学习微信公众号
  • 作为开发者犯过的两次愚蠢的错误 一定切记切记

    上周我和同事们简单地聊了聊我们工作中搞砸的那些事儿。如今早已不再犯那些错了,所以想起过去就觉得很好笑。但是笑归笑,其实当时犯的这些错让我们受益颇深。

    java架构师
  • 【奥斯卡理财星体系 第四章】丨你该如何选择适合的理财工具

    在我们学习了理财“心法”,认识了“自己”,并且对自己适合的收益预期,也有了明确概念之后,接下去就到了童鞋们期盼已久的,学习理财“招式”的时间啦。

    Hongten
  • 中国台湾大学林轩田机器学习技法课程学习笔记4 -- Soft-Margin Support Vector Machine

    上节课我们主要介绍了Kernel SVM。先将特征转换和计算内积这两个步骤合并起来,简化计算、提高计算速度,再用Dual SVM的求解方法来解决。Kernel ...

    红色石头
  • sqlplus登录报ORA-06502错误的问题排查和解决

    最近碰见了一个sqlplus登陆报错的问题,我一开始排查问题的方向就错了,不得已,还是请教了dbsnake大师,对于这么一个小问题,就像“小罗的不看人传球”般解...

    bisal
  • 机器学习线性分类算法:感知器原理

    感知器PLA是一种最简单,最基本的线性分类算法(二分类)。其前提是数据本身是线性可分的。 模型可以定义为 ? ,sign函数是阶跃函数,阈值决定取0或1。 模型...

    机器学习AI算法工程
  • 我一个自行车爱好者,被谷歌出卖定位信息成“嫌疑犯”,花了大价钱才避免窦娥冤

    一个普通的骑行爱好者,突然就变成了疑犯,蒙受不白之冤,花了一大笔钱找律师自证清白。

    量子位
  • IPinfo 多接口IP查询工具源码

    说明:应昨天某位同学的要求,研究了下Opage,Opage是一个客制化上网首页,基于Material Design & Vue.js,集成搜索,网站,天气,TO...

    砸漏
  • 中国台湾大学林轩田机器学习技法课程学习笔记8 -- Adaptive Boosting

    上节课我们主要开始介绍Aggregation Models,目的是将不同的hypothesis得到的gtg_t集合起来,利用集体智慧得到更好的预测模型G。首先我...

    红色石头
  • 创新怎么教?

    创新能力不是从天上掉下来的,而是你本来就有,却被你忽略或者压抑的。因此它才可以通过有效的方式挖掘出来。

    王树义
  • 误删了公司数据库,但我还是活下来了

    小小科
  • 误删了公司数据库,但我还是活下来了!

    上周我与同事们进行了一次关于职业生涯中搞砸了一些事情的简短谈话。这确实会沦为他人笑柄,却更给我们带来了珍贵的教训。重要的是,我们应该分享那些曾经的错误,这样其他...

    马哥linux运维
  • 误删了公司数据库,但我还是活下来了!

    上周我与同事们进行了一次关于职业生涯中搞砸了一些事情的简短谈话。这确实会沦为他人笑柄,却更给我们带来了珍贵的教训。重要的是,我们应该分享那些曾经的错误,这样其他...

    马哥linux运维
  • 快速入行数据科学,老司机带你避开这九个大坑!

    大数据文摘
  • [喵咪PHP]页面显示空白问题

    #[喵咪PHP]页面显示空白问题# ? ##前言## 哈喽!大家好啊,喵咪PHP第一次和大家见面了,熟悉的朋友呢也之道喵咪我也开了喵咪Liunx和喵咪Golan...

    喵了个咪233
  • 致 DBA:为什么你经常犯错,是因为你做的功课不够

    本文就是基于这方面的考虑,根据自己在 DBA 这个职业上走过的弯路,总结一些方法给 DBA 的同行。希望本文能给同行 DBA 或者运维的朋友们带来一些改变,让大...

    飞鸿无痕
  • CentOS7下gogs安装总结

    七、使用浏览器访问http://IP:3000,完成配置安装,建议使用SQLite:

    botkenni
  • 美国参众两院议员中有28名罪犯?亚马逊AI人脸识别系统遭质疑

    今年 5 月,亚马逊因为将 Rekognition AI 服务出售给美国地方警察而引起了争议,美国公民自由联盟(ACLU)一直要求该公司停止授权政府部门使用这项...

    机器之心

扫码关注云+社区

领取腾讯云代金券