iOS后台定位相关问题调研

导言

一般普通的定位需求, 苹果自家提供的 CLLocationManager 框架便可以满足. 但如果需求是获取持续的用户定位信息, CLLocationManager 策略和性能就很不理想. 而由Transistor Software 基于 CLLocationManagerCMMotionActivtyManager (在iOS设备上) 研发的 Background Geolocation SDK, 就有着非常优异的表现.

比较

在持续定位的需求上, 我们最关心,最敏感的部分就是 定位精度 以及 电池的使用效率.

定位精度

CLLocationManager 的定位策略不仅仅是纯粹的GPS定位, 而是称为A-GPS的辅助GPS定位系统, 是一种将基站定位、WIFI定位、GPS定位混合使用的技术. 可以通过设置desiredAccuracy枚举来选择不同的精度, 从三千米误差范围到米级误差范围, 定位越精确, 耗费的时间, 电力资源也就越大.

Background Geolocation 的定位实现是基于 CLLocationManager 与 陀螺仪、加速计、磁力计 的. 但是由于 CLLocationManager 只对距离敏感, 即它被动回调的时机是只有大于设定的距离才会触发(distanceFilter).

Background Geolocation 则是会根据时间、距离、状态这些因素综合考量来触发回调, 通知App当前的位置信息.

例如当distanceFilter设定为10米时(最精确值), 我在10分钟内围绕着中心点连续走了5米, 那么 CLLocationManager 不会触发位置变化回调.

而使用Background Geolocation, 在一定时间内(10s), 哪怕用户只移动了1米, App也会收到位置更新回调.
16655624173884

如图, 可以看到在移动10米甚至静止的情况下 都会有位置更新的记录.

因为这样的触发时机的差异, 就会使 Background Geolocation 有着更好的相对定位准确度.

电池使用效率

在高精度情况下, CLLocationManager 通过不考虑时间这一变量, 紧紧只关心距离更改来尽可能的降低回调时机, 进而达到"貌似"有效的电量优化, 即便如此, CLLocationManager还是在频繁的监控用户的位置(只是不回调而已), 所以一旦开启 CLLocationManager "实时监控", 其耗电量是巨大的.

Background Geolocation 则通过对动作监测, 如陀螺仪、加速计、磁力计的数据来做为状态切换的依据. 在iPhone 5s以上的设备, 它通过监测 CMMotionActivtyManager 的API来获取这些信息, 在其他设备上, 则通过Background Geolocation自己编写的代码来监测.

Background Geolocation 监测到用户进入still状态时, 它会关闭定位服务来节省电力, 当用户切换到移动状态时on_foot, running, in_vehicle等, 则会开启定位服务, 来获取位置信息. 当用户处在相同移动状态一段时间时, Background Geolocation 还会动态调整定位服务的精确度来进一步降低电量开销.

16655624532307

为了得到尽可能明确的耗电量信息, 我做了严苛的对照实验组. 并进行了多次实验.
标准如下:

  1. 前台使用: 只开启了 Background Geolocation Flutter demo这个APP, 屏幕亮度最高, 永不锁屏, 未接打电话等其他会严重损害电量的行为. 平均移动距离 0.3km ± 0.03km 每小时.
  2. 后台使用: 只开启了 Background Geolocation Flutter demo这个APP, 未接打电话等其他会严重损害电量的行为. 平均移动距离 0.3km ± 0.03km 每小时.
  3. 设备为iPhone7Plus. 电池最大容量100%(刚更换2个月), 2900mAh.
  4. 实验了5次, 取平均值. 最终得到使用效果如下图.

16655624614537

  • 为了标准化, 将前台时的屏幕亮度设置为最高, 用不锁屏. 这是尽量避免不明确参数. 正常用户使用时基本不可能这样设置, 再加上都是前后台混合使用, 这样来看, Background Geolocation 在电池使用效率上 是非常优益的.

其他

项目管理角度

Background Geolocation 是不开源的跨平台SDK, github上有基于flutter, React Native, Cordova等跨平台方案的demo, 这意味着iOS和Android共同开发成本很低, 遇到问题可以互相交流, 非常适合小型公司. 同时其开发者 Christopher Scott 是一个研究定位问题近十年的专家, Background Geolocation SDK 已经发布六年有余, 期间他一直对 Background Geolocation SDK 做场景测试, 文档详实.

价格

Background Geolocation 在iOS上不收费, 在Android平台上, debug模式下可以正常使用, release模式下需要购买证书签名才可以使用, 证书价格是300$起步.

关于后台保活

Background Geolocation 在iOS上实现了一个preventSuspend的模式来保证App在进入后台后可以无限期存活, 不清楚是基于哪种方案, 但是确实非常省电.

关于App被杀掉后的位置信息获取

当APP被杀后, 可以通过两种方式来获取位置信息.

  1. 设置地理围栏, 进入或者退出时可以唤起APP 来处理当前的位置信息, 同时可以与服务器进行信息同步, 苹果的建议是频率不要高于5分钟一次.

    Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification. It should not expect notifications more frequently than once every five minutes. If the device is able to retrieve data from the network, the location manager is much more likely to deliver notifications in a timely manner.

  2. 静默推送唤醒APP. 此方法唤起APP后, 大约有10s~30s的时间来进行获取位置信息, 同步服务器等操作. 但是苹果对这个有限制, 一小时只能发几条.

    Background update notifications are not meant as a way to keep your app awake in the background beyond quick refresh operations, nor are they meant for high priority updates. APNs treats background update notifications as low priority and may throttle their delivery altogether if the total number becomes excessive. The actual limits are dynamic and can change based on conditions, but try not to send more than a few notifications per hour.

相关

Github: https://github.com/transistorsoft
官网: https://www.transistorsoft.com
开发者领英: https://www.linkedin.com/in/christocracy/?originalSubdomain=ca
Apple关于静默推送的文档 https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html
Apple关于地理围栏监控的文档 https://developer.apple.com/documentation/corelocation/cllocationmanager/1423531-startmonitoringsignificantlocati
定位相关Blog https://sunxichun.github.io/2018/12/30/iOSLocation/

订阅评论
提醒
guest
0 评论
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x