NSURLSessionConfiguration
創(chuàng)建 NSURLSessionConfiguration 對(duì)象的三種方式:
// 默認(rèn)會(huì)話配置
NSURLSessionConfiguration *defaultConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
// 短暫會(huì)話配置,無緩存(caches),cookies 和證書(credentials)
NSURLSessionConfiguration *ephemeralConfig = [NSURLSessionConfiguration ephemeralSessionConfiguration];
// 后臺(tái)會(huì)話配置
NSURLSessionConfiguration *backgroundConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.Apple.BackgroundDownload.BackgroundSession"];
NSURLSession
創(chuàng)建 NSURLSession 對(duì)象的三種方式:
// 1. 單例類會(huì)話,便捷創(chuàng)建方法
NSURLSession *sharedSession = [NSURLSession sharedSession];
// 2. sessionWithConfiguration:
// 一般用 Block 處理服務(wù)器回調(diào)時(shí)使用
// 默認(rèn)會(huì)話
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultConfig];
// 短暫會(huì)話
NSURLSession *ephemeralSesson = [NSURLSession sessionWithConfiguration:ephemeralConfig];
// 后臺(tái)會(huì)話
NSURLSession *backgroundSession = [NSURLSession sessionWithConfiguration:backgroundConfig];
// 3. sessionWithConfiguration:delegate:delegateQueue:
// 需要實(shí)現(xiàn)委托協(xié)議或者指定線程的創(chuàng)建方法
NSOperationQueue *operationQueue = [NSOperationQueue mainQueue];
// 默認(rèn)會(huì)話
NSURLSession *defalutSession1 = [NSURLSession sessionWithConfiguration:defaultConfig delegate:self delegateQueue:operationQueue];
// 短暫會(huì)話
NSURLSession *ephemeralSesson1 = [NSURLSession sessionWithConfiguration:ephemeralConfig delegate:self delegateQueue:operationQueue];
// 后臺(tái)會(huì)話
NSURLSession *backgroundSession1 = [NSURLSession sessionWithConfiguration:backgroundConfig delegate:self delegateQueue:operationQueue];
NSURLSessionTask
創(chuàng)建 NSURLSessionTask 對(duì)象有四種種方式:
- 數(shù)據(jù)任務(wù)(NSURLSessionDataTask)
- 上傳任務(wù)(NSURLSessionUploadTask)
- 下載任務(wù)(NSURLSessionDownloadTask)
- 流任務(wù)(NSURLSessionStreamTask):WWDC2015新增熏迹,提供 TCP/IP 連接接口伦腐。
NSURLSessionDelegate 委托協(xié)議
示例代碼
注:該項(xiàng)目的 源碼
// *************************************************
// SCViewController.h
#import <UIKit/UIKit.h>
@interface SCViewController : UIViewController
@property (strong, nonatomic) NSURLSessionDownloadTask *resumableTask; // 可恢復(fù)任務(wù)
@property (strong, nonatomic) NSURLSessionDownloadTask *backgroundTask; // 后臺(tái)任務(wù)
@property (strong, nonatomic, readonly) NSURLSession *backgroundSession; // 后臺(tái)會(huì)話
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIProgressView *progressIndicator;
@property (strong, nonatomic) IBOutletCollection(UIBarButtonItem) NSArray *startButtons;
- (IBAction)startCancellable:(id)sender; // 開始任務(wù)
- (IBAction)cancelCancellable:(id)sender; // 取消任務(wù)
- (IBAction)startResumable:(id)sender; // 恢復(fù)任務(wù)
- (IBAction)startBackground:(id)sender; // 后臺(tái)任務(wù)
@end
// *************************************************
// SCViewController.m
#import "SCViewController.h"
#import "SCAppDelegate.h"
@interface SCViewController () <NSURLSessionDownloadDelegate> {
NSURLSessionDownloadTask *cancellableTask; // 可取消任務(wù)
NSURLSession *inProcessSession; // 私有會(huì)話
NSData *partialDownload; // 可恢復(fù)任務(wù)下載的臨時(shí)數(shù)據(jù)
}
@end
@implementation SCViewController
#pragma mark - Lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// 從 nib 文件加載視圖后,進(jìn)行額外的初始化工作
self.progressIndicator.hidden = YES;
self.progressIndicator.progress = 0;
// Make sure that we've attached to the background session
self.backgroundSession.sessionDescription = @"Background session";
}
#pragma mark - Custom Accessors
// 后臺(tái)會(huì)話砌溺,單例類
- (NSURLSession *)backgroundSession
{
static NSURLSession *backgroundSession = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.shinobicontrols.BackgroundDownload.BackgroundSession"];
backgroundSession = [NSURLSession sessionWithConfiguration:config
delegate:self
delegateQueue:nil];
});
return backgroundSession;
}
#pragma mark - IBActions
// 開始任務(wù)
- (IBAction)startCancellable:(id)sender {
if (!cancellableTask) {
if(!inProcessSession) {
// 創(chuàng)建默認(rèn)會(huì)話
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
inProcessSession = [NSURLSession sessionWithConfiguration:sessionConfig
delegate:self
delegateQueue:nil];
inProcessSession.sessionDescription = @"in-process NSURLSession";
}
// Image CreativeCommons courtesy of flickr.com/charliematters
NSString *url = @"http://farm6.staticflickr.com/5505/9824098016_0e28a047c2_b_d.jpg";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
// 創(chuàng)建可取消任務(wù)
cancellableTask = [inProcessSession downloadTaskWithRequest:request];
[self setDownloadButtonsAsEnabled:NO];
self.imageView.hidden = YES;
// 開啟下載任務(wù)
[cancellableTask resume];
}
}
// 取消任務(wù)
- (IBAction)cancelCancellable:(id)sender {
if(cancellableTask) {
// 1. 可取消任務(wù)
[cancellableTask cancel];
cancellableTask = nil;
} else if(self.resumableTask) {
// 2. 可恢復(fù)任務(wù)
[self.resumableTask cancelByProducingResumeData:^(NSData *resumeData) {
// ?? 緩存已下載數(shù)據(jù)
partialDownload = resumeData;
self.resumableTask = nil;
}];
} else if(self.backgroundTask) {
// 3. 后臺(tái)任務(wù)
[self.backgroundTask cancel];
self.backgroundTask = nil;
}
}
// 恢復(fù)任務(wù)
- (IBAction)startResumable:(id)sender {
if(!self.resumableTask) {
if(!inProcessSession) {
// 創(chuàng)建默認(rèn)會(huì)話
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
inProcessSession = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
inProcessSession.sessionDescription = @"in-process NSURLSession";
}
if(partialDownload) {
// 1. 如果存在緩存數(shù)據(jù),繼續(xù)下載此任務(wù)
self.resumableTask = [inProcessSession downloadTaskWithResumeData:partialDownload];
} else {
// 2. 如果不存在緩存,新建可恢復(fù)任務(wù)
// Image CreativeCommons courtesy of flickr.com/charliematters
NSString *url = @"http://farm3.staticflickr.com/2846/9823925914_78cd653ac9_b_d.jpg";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
// 創(chuàng)建可恢復(fù)任務(wù)
self.resumableTask = [inProcessSession downloadTaskWithRequest:request];
}
[self setDownloadButtonsAsEnabled:NO];
self.imageView.hidden = YES;
// 開啟下載任務(wù)
[self.resumableTask resume];
}
}
// 開始后臺(tái)任務(wù)
- (IBAction)startBackground:(id)sender {
// Image CreativeCommons courtesy of flickr.com/charliematters
NSString *url = @"http://farm3.staticflickr.com/2831/9823890176_82b4165653_b_d.jpg";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
// self.backgroundSession 是單例對(duì)象
self.backgroundTask = [self.backgroundSession downloadTaskWithRequest:request];
[self setDownloadButtonsAsEnabled:NO];
self.imageView.hidden = YES;
// 開啟下載任務(wù)
[self.backgroundTask resume];
}
#pragma mark - Private
- (void)setDownloadButtonsAsEnabled:(BOOL)enabled
{
// 更新按鈕 enable 狀態(tài)
for(UIBarButtonItem *btn in self.startButtons) {
btn.enabled = enabled;
}
}
#pragma mark - NSURLSessionDownloadDelegate
// 追蹤下載任務(wù)進(jìn)度佣谐,更新進(jìn)度條
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
double currentProgress = totalBytesWritten / (double)totalBytesExpectedToWrite;
dispatch_async(dispatch_get_main_queue(), ^{
self.progressIndicator.hidden = NO;
self.progressIndicator.progress = currentProgress;
});
}
// 恢復(fù)下載后調(diào)用
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
{
// Leave this for now
}
// 下載任務(wù)成功后調(diào)用
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
// We've successfully finished the download. Let's save the file
// 保存到文件
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *URLs = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSURL *documentsDirectory = URLs[0];
NSURL *destinationPath = [documentsDirectory URLByAppendingPathComponent:[location lastPathComponent]];
NSError *error;
// 先刪除文件中已經(jīng)存在的任何文件
[fileManager removeItemAtURL:destinationPath error:NULL];
BOOL success = [fileManager copyItemAtURL:location toURL:destinationPath error:&error];
if (success) {
// 保存到文件后,更新主線程 UI
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithContentsOfFile:[destinationPath path]];
self.imageView.image = image;
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
self.imageView.hidden = NO;
});
}else {
NSLog(@"Couldn't copy the downloaded file");
}
// ?? 會(huì)話置空方妖,解除循環(huán)引用
if(downloadTask == cancellableTask) {
cancellableTask = nil;
} else if (downloadTask == self.resumableTask) {
self.resumableTask = nil;
partialDownload = nil;
} else if (session == self.backgroundSession) {
// 后臺(tái)會(huì)話
self.backgroundTask = nil;
// Get hold of the app delegate
SCAppDelegate *appDelegate = (SCAppDelegate *)[[UIApplication sharedApplication] delegate];
if(appDelegate.backgroundURLSessionCompletionHandler) {
// Need to copy the completion handler
void (^handler)() = appDelegate.backgroundURLSessionCompletionHandler;
appDelegate.backgroundURLSessionCompletionHandler = nil;
handler();
}
}
}
#pragma mark - NSURLSessionTaskDelegate
// 每一個(gè)任務(wù)結(jié)束的時(shí)候執(zhí)行-NSURLSessionDelegate,不管任務(wù)是不是正常結(jié)束:
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
// 主線程更新 UI
dispatch_async(dispatch_get_main_queue(), ^{
self.progressIndicator.hidden = YES;
[self setDownloadButtonsAsEnabled:YES];
});
}
@end
開啟后臺(tái)下載任務(wù)時(shí)台谍,即使關(guān)閉了應(yīng)用程序,這個(gè)下載任務(wù)依然在后臺(tái)運(yùn)行吁断。
當(dāng)這個(gè)下載任務(wù)完成后趁蕊,iOS將會(huì)重新啟動(dòng)app來讓任務(wù)知道,對(duì)這個(gè)我們不必放在心上仔役。為了達(dá)到這個(gè)目的掷伙,我們可以在app的代理中調(diào)用下面的代碼:
- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler
{
self.backgroundURLSessionCompletionHandler = completionHandler;
}