iOS7程序后台运行

介绍

这次 iOS7 对程序后台运行进行了加强,但是仅仅是加强而已,要想像 Android 程序那样自由当然就别想了,苹果这么做主要还是出于电池使用时间考虑,但是这次的加强对大部分程序基本够用。

在介绍之前, 我们先回顾一下在 iOS7 之前的后台运行相关的知识。在 iOS7 之前(iOS4 之后)主要有三类的应用程序能够后台运行:

  1. 音频播放
  2. 后台定位服务
  3. IP 电话

除了这三种应用,其他程序只能是在进入后台之前向系统请求一个额外的运行时间(最长为 10 分钟),并在该时间内来进行后台运行操作,如保存用户信息,上传或下载数据,进行视频编码等操作。

- (void)applicationDidEnterBackground:(UIApplication *)application
{
	static UIBackgroundTaskIdentifier task;
	task = [application beginBackgroundTaskWithExpirationHandler:^{
     task = UIBackgroundTaskInvalid;
 	};
	// 执行后台操作
	[application endBackgroundTask:task];
}

这次 iOS7 支持了两种新的程序后台运行模式:

  • 需要定期请求数据的程序可以在系统中注册,这样程序就可以在后台被定期唤醒来下载新的数据。这种情况需要在程序的 Info.plist 文件中 UIBackgroundModes 项增加 fetch 类型,同时通过 setMinimumBackgroundFetchInterval: 方法来设置程序定期获取数据的最小时间间隔。你需要实现 application:performFetchWithCompletionHandler: 代理方法并在该方法内执行下载操作。

  • 程序还可以通过后台消息推送服务来通知用户有新的内需可以下载,同时激活后台下载操作。这种需要在 UIBackgroundModes 项中增加 remote-notification 值,同时你需要实现 AppDelegate 方法 application:didReceiveRemoteNotification:fetchCompletionHandler: 来执行你的下载操作。

不管是支持 fetch 或 remote-notification 后台运行模式的程序,都有可能被系统在合适的时候启动或从后台挂起状态移除调。在 fetch 模式下,系统会利用有效的信息来决定启动或激活程序的最佳时期。例如:系统可能会在网络状况良好或者设备刚解锁的时候让程序执行 fetch 操作。支持 remote-notifiaction 的程序,可以在接收到推送消息的时候被唤醒,但在用户接收到推送消息之前,程序可以通过定期获取的形式下载最新内容,并在随后的推送消息之前就已经准备好将内容展现给用户。

为了执行后台下载操作,程序应该使用新增的 NSURLSession 类,该类在之前的 NSURLConnectoin 的基础上提供了更简洁、基于任务的接口来启动并执行 NSURLRequest 对象。一个 NSURLSession 对象可以启动多个下载或上传任务,并在代理方法里面来处理来自服务器的认证请求。

实现

现在我们来实现 fetch 和 remote-notifiaction 两种后台运行。

1. 设置

在 Xcode5.0 里面 Capabilities 下可以直接通过勾选的方式选择应用需要支持的后台运行的类型(可多选哦),我们选中 Background fetch 和 Remote notification 两项。并在程序的 Info.plist 文件中的 Required background modes 中添加 fetch 和 remote-notification 两项。

Multitasking SettingMultitasking Setting

2. Background Fetch

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [application setMinimumBackgroundFetchInterval:
     UIApplicationBackgroundFetchIntervalMinimum];
    return YES;
}

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    NSURL *url = [NSURL URLWithString:@"http://127.0.0.1:3000/update.do"];
    NSURLSession *updateSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    [updateSession dataTaskWithHTTPGetRequest:url
                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                NSDictionary *messageInfo = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                                NSLog(@"messageInfo:%@",messageInfo);
                                completionHandler(UIBackgroundFetchResultNewData);
                            }];
}

首先在 application:didFinishLaunchingWithOptions: 中设置 minimun background fetch interval 类型为 UIApplicationBackgroundFetchIntervalMinimum(默认为 UIApplicationBackgroundFetchIntervalNever),然后实现代理方法 application:performFetchWithCompletionHandler: 中实现数据请求。

为了测试程序后台运行,我们可以新建一个 Scheme,选中 Background Fetch(Launch due to a background fetch event),然后在该 Scheme 下运行程序,程序并不会启动,但是你能看到它给后台发了请求。

New SchemeNew Scheme

Remote Notifications

类似要实现 remote-notification 模式,需要在原来支持 push 的条件下实现 application:didReceiveRemoteNotification:fetchCompletionHandler: 代理方法,程序在后台收到 payload 中包含 "content-available = 1" 的推送消息时,会执行该代理方法。(因为模拟器无法模拟消息推送,iPad 版本的 iOS7 还没提供下载,所以我暂时没法亲测)。

总结

总的来说实现上没有什么太复杂的东西,关键是你怎么样将这两种新的后台运行模式应用到你的程序中。

Posted by XiaoYi_HD - 6月 23 2013
如需转载,请注明: 本文来自 Esoft Mobile

原文地址:https://www.cnblogs.com/tracy-e/p/3151457.html