码迷,mamicode.com
首页 > 移动开发 > 详细

iOS8后台定位

时间:2015-08-17 12:02:13      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:后台定位   ios   

iOS8后台定位

测试环境

Xcode6.3.2,模拟器版本系统8.3,真机系统版本6.1.3

测试条件

  1. 设置BackgroundModes,使用requestWhenInUseAuthorization获取权限
  2. 设置BackgroundModes,使用requestAlwaysAuthorization获取权限
  3. 不设置BackgroundModes,使用requestWhenInUseAuthorization获取权限
  4. 不设置BackgroundModes,使用requestAlwaysAuthorization获取权限

测试说明:过程中发现同时使用requestAlwaysAuthorization和requestWhenInUseAuthorization请求权限的情况下,app会优先执行顺序靠前的请求,而忽略掉后面的.例如同时使用:

    [_manager requestWhenInUseAuthorization];
    [_manager requestAlwaysAuthorization];

安装应用后弹出框为requestWhenInUseAuthorization的相关提示,进入系统"设置->隐私->位置"也仅为"使用期间".故测试代码均指测试了单一语句.

配置测试工程

新建一个名为BackgroundLocation的测试工程,在TARGETS->Capabilities中找到BackgroundModes,点击开启后勾选下面的Location updates.之后的测试中将会测试开启和关闭这个选项的情况.

测试条件1

安装app,系统提示:技术分享

技术分享

点击Allow,系统开始输入打印日志:

技术分享

 技术分享即时将程序最小化到后台后者锁屏,依然有打印输出.但是系统会有如下图提示,锁屏时导航栏也会变色: 技术分享技术分享

技术分享

技术分享

测试条件2

安装app,系统提示:

技术分享

技术分享

点击允许,开始输入打印日志.将程序最小化到后台或者锁屏,依然有打印输出,但是系统界面一切正常,没有条件1中的提示.

测试条件3

系统提示框同条件1,但最小化和锁屏后不再有打印输出,说明进入后台后没有继续进行定位.

测试条件4

系统提示框同条件2,但最小化和锁屏后依然有打印输出,系统界面也都一切正常,没有条件1中的隐私提醒.

测试总结

  1. 由条件3和条件4的现象可以推断出NSLocationWhenInUseUsageDescription只在用户正在使用该App时可用,即应用处于激活状态时可用.而NSLocationAlawaysUsageDescription在应用进入后台后也能继续定位.
  2. 由条件1和条件2的现象可以推断iOS对用户隐私控制的确实严格,BackgroundMode保证了即便是NSLocationWhenInUseUsageDescription的应用也能实现后台定位了.

但通过对比条件2和条件4发现两者好像并没有什么区别,那BackgroundMode究竟有什么用呢?

在官方文档(https://developer.apple.com/library/prerelease/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH4-SW1)中我找到了这样一段话:

Enabling this mode does not prevent 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.

大意为:开启UIBackgroundModes-location updates并不能阻止系统挂起你的app,但是它告诉系统应该在有新的地址位置更新时唤醒app,通过这种方式不论在什么时候你的app都能处理位置更新.

但是,然并卵,模拟器测试一直没有成功,等待真机测试中.

继续查阅文档:https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW1:

//Using the Standard Location Service in the Background
The system delivers location updates to background location apps when the app is in the foreground, is running in the background, or is suspended. In the case of a suspended app, the system wakes up the app, delivers the update to the location manager’s delegate, and then returns the app to the suspended state as soon as possible. While it is running in the background, your app should do as little work as possible to process the new location data.

https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW8:

Note: If your app is terminated either by a user or by the system, the system doesn’t automatically restart your app when new location updates arrive. A user must explicitly relaunch your app before the delivery of location updates resumes. The only way to have your app relaunched automatically is to use region monitoring or the significant-change location service.

注意:If your app is terminated either by a user or by the system, the system doesn’t automatically restart your app when new location updates arrive!!!

然并卵,模拟器依旧没有测试成功.求成功案例!!求成功案例!!求成功案例!!

版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS8后台定位

标签:后台定位   ios   

原文地址:http://blog.csdn.net/qq329735967/article/details/47722949

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!