最近终于回归iOS了,一会来就碰到了定位相关的功能要求,客户要求即使应用挂起也可以执行GPS定位,并在位置发生变化时根据你的需要唤醒应用。
稍微研究了一下,实现方式跟音频后台播放类似,在应用的plist里添加后台运行设定即可。
Apple官方说明原文:
An app that provides continuous location updates to the user (even when in the background) can enable background location services by including the UIBackgroundModes key (with the location value) in its Info.plist file. The inclusion of this value in the UIBackgroundModes key does not preclude the system from suspending the app, but it does tell the system that it should wake up the app whenever there is new location data to deliver. Thus, this key effectively lets the app run in the background to process location updates whenever they occur.
实现方法是,在应用的info.plist里找到【Required background modes】项,在其中添加如下item: 【App registers for location updates】。