丢掉iOS系统提供的后台多任务运行机制不管,iOS在后台运行的总体可分为:真后台(系统通知,系统应用,特殊应用[audio ,location,voip,Newsstand, Exernal Accessory]) 和 伪后台(申请时间,background transfer service,或其他方法)
1 .系统通知:真正的后台,不会被杀
2. 系统应用:内存不足时会被杀
3. 特殊应用:Info.plist中添加UIBackgroundModes键值设置 成audio ,location,voip类型应用,但审核不好通过
除了添加键值的方法,IOS还提供了两种途径使应用程序在后台工作:
Task completion—应用程序可以向系统申请额外的时间去完成给定的任务
Local notifications—应用程序可以预先安排时间执行local notifications 传递
4.申请时间:应用程序可以请求一个有限的时间内完成一些重要任务 最多大约为10分钟
app suspend前系统会调用 beginBackgroundTaskWithExpirationHandler
UIApplication的backgroundTimeRemaining属性包含程序运行的总时间
5.background transfer service :ios7.0之后系统提供的服务 使用NSURLSession
使用案例:IOS开发Swift用NSURLSessionDownloadTask:http://blog.csdn.net/casun_li/article/details/51063762
IOS开发Swift使用NSURLSessionUploadTask实现后台上传功能 NSURLSession学习笔记(二)Session Task
NSURLSession学习笔记(三)Download Task
好像是官方demo: https://github.com/lcbullock/iOS7-Background-Transfer-Service
6. 其他方法:
a. 安排Local Notification的传递
UILocalNotification类提供了一种方法来传递local notifications。和push notifications需要设置remote server不同,local notifications 在程序中安排并在当前的设备上执行。满足如下条件可以使用该能力:
1、一个基于时间的程序,可以在将来特定的时间让程序post 一个alert,比如闹钟
2、一个在后台运行的程序,post 一个local notification去引起用户的注意
b.
后台获取(Background Fetch)
推送唤醒(静默推送,Silent Remote Notifications)
参考文档:
http://blog.csdn.net/zhchaoo/article/details/8693573
http://blog.csdn.net/songhongri/article/details/11865391
http://blog.csdn.net/diyagoanyhacker/article/details/7071055
http://blog.csdn.net/junjun150013652/article/details/38616893
http://www.cocoachina.com/bbs/read.php?tid-206160-fpage-187-page-1.html