一、远程获取:(只能用于小量数据的获取)
1) info.plist UIBackgroundModes + background fetch
2) [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
设置为UIApplicationBackgroundFetchIntervalMinimum的话,系统会尽可能多尽可能快地为你的应用进行后台获取
3) -application:performFetchWithCompletionHandler: 拿到获取的数据,交给相关的controller处理(更新UI)
如何调试后台更新?
二、推送唤醒: 远端推送后,系统唤醒后台app,可以执行代码准备数据和UI,然后提示用户有推送;进入应用后没有任何加载
1) Info.plist UIBackgroundModes remote-notification
2) payload加入content-available ,并置为1
aps {
content-available: 1
alert: {...}
}
3) -application:didReceiveRemoteNotification:fetchCompletionHandle:
推送屏幕 : 频率在一小时内个位数次的推送
使用场景: 用户感兴趣的内容发生更新
三、后台传输 (大文件的下载) Background Transfer Service
NSURLSession
1) 创建和配置session
- (NSURLSession *)backgroundSession
{
//Use dispatch_once_t to create only one background session. If you want more than one session, do with different identifier
static NSURLSession *session = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.yourcompany.appId.BackgroundSession"];
session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
});
return session;
}
2) 向session加入task (NSURLSessionTask对象)
- (void) beginDownload
{
NSURL *downloadURL = [NSURL URLWithString:DownloadURLString];
NSURLRequest *request = [NSURLRequest requestWithURL:downloadURL];
self.session = [self backgroundSession];
self.downloadTask = [self.session downloadTaskWithRequest:request];
[self.downloadTask resume];
}
3) 后台传输结束时调用:
-application:handleEventsForBackgroundURLSession:completionHandler:
下面回调方法可以跟踪传输状态:
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location;
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error;
后台传输限制:
后台传输只会通过wifi来进行
离散式的下载
1) info.plist UIBackgroundModes + background fetch
2) [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
设置为UIApplicationBackgroundFetchIntervalMinimum的话,系统会尽可能多尽可能快地为你的应用进行后台获取
3) -application:performFetchWithCompletionHandler: 拿到获取的数据,交给相关的controller处理(更新UI)
如何调试后台更新?
二、推送唤醒: 远端推送后,系统唤醒后台app,可以执行代码准备数据和UI,然后提示用户有推送;进入应用后没有任何加载
1) Info.plist UIBackgroundModes remote-notification
2) payload加入content-available ,并置为1
aps {
content-available: 1
alert: {...}
}
3) -application:didReceiveRemoteNotification:fetchCompletionHandle:
推送屏幕 : 频率在一小时内个位数次的推送
使用场景: 用户感兴趣的内容发生更新
三、后台传输 (大文件的下载) Background Transfer Service
NSURLSession
1) 创建和配置session
- (NSURLSession *)backgroundSession
{
//Use dispatch_once_t to create only one background session. If you want more than one session, do with different identifier
static NSURLSession *session = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.yourcompany.appId.BackgroundSession"];
session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
});
return session;
}
2) 向session加入task (NSURLSessionTask对象)
- (void) beginDownload
{
NSURL *downloadURL = [NSURL URLWithString:DownloadURLString];
NSURLRequest *request = [NSURLRequest requestWithURL:downloadURL];
self.session = [self backgroundSession];
self.downloadTask = [self.session downloadTaskWithRequest:request];
[self.downloadTask resume];
}
3) 后台传输结束时调用:
-application:handleEventsForBackgroundURLSession:completionHandler:
下面回调方法可以跟踪传输状态:
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location;
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error;
后台传输限制:
后台传输只会通过wifi来进行
离散式的下载