精准的控制任務的取消, 掛起, 恢復
#pragma mark --- 請求數據 request --- session ---- sessionDataTask
- (void)loadData
{
// 1. 創建url
NSString *urlString = [NSString stringWithFormat:@"url"];
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:urlString];
// 2. 創建請求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 3. 創建會話 (使用單例初始化, 啟動任務)
NSURLSession *session = [NSURLSession sharedSession];
// 會話創建任務
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (!error) {
NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", dataStr);
} else {
NSLog(@"error is %@", error.localizedDescription);
}
}];
// 恢復線程, 啟動任務
[dataTask resume];
}
#pragma mark ----- 文件上傳
- (void)upDataFile
{
/**
* 文件上傳的時候需要設置請求頭中Content-Type類型, 必須使用URL編碼,
application/x-www-form-urlencoded:默認值,發送前對所有發送數據進行url編碼,支持浏覽器訪問,通常文本內容提交常用這種方式。
multipart/form-data:多部分表單數據,支持浏覽器訪問,不進行任何編碼,通常用於文件傳輸(此時傳遞的是二進制數據) 。
text/plain:普通文本數據類型,支持浏覽器訪問,發送前其中的空格替換為“+”,但是不對特殊字符編碼。
application/json:json數據類型,浏覽器訪問不支持 。
text/xml:xml數據類型,浏覽器訪問不支持。
multipart/form-data 必須進行設置,
*/
// 1. 創建URL
NSString *urlStr = @"url";
urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:urlStr];
// 2. 創建請求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 設置請求的為POST
request.HTTPMethod = @"POST";
// 3.構建要上傳的數據
NSString *path = [[NSBundle mainBundle] pathForResource:@"123" ofType:@"123"];
NSData *data = [NSData dataWithContentsOfFile:path];
// 設置request的body
request.HTTPBody = data;
// 設置請求 Content-Length
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)data.length] forHTTPHeaderField:@"Content-Length"];
// 設置請求 Content-Type
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",@"Xia"] forHTTPHeaderField:@"Content-Type"];
// 4. 創建會話
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:data completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (!error) {
// 上傳成功
}else {
// 上傳失敗, 打印error信息
NSLog(@"error --- %@", error.localizedDescription);
}
}];
// 恢復線程 啟動任務
[uploadTask resume];
}
#pragma mark --- 文件下載
/**
* 使用NSURLSessionDownloadTask下載文件過程中注意:
下載文件之後會自動保存到一個臨時目錄中, 需要自己將文件重新放到其他指定的目錄中
*/
- (void)downLoadFile
{
// 1. 創建url
NSString *urlStr =[NSString stringWithFormat:@"%@", @"url"];
urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *Url = [NSURL URLWithString:urlStr];
// 創建請求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:Url];
// 創建會話
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDownloadTask *downLoadTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (!error) {
// 下載成功
// 注意 location是下載後的臨時保存路徑, 需要將它移動到需要保存的位置
NSError *saveError;
// 創建一個自定義存儲路徑
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *savePath = [cachePath stringByAppendingPathComponent:@"fileName"];
NSURL *saveURL = [NSURL fileURLWithPath:savePath];
// 文件復制到cache路徑中
[[NSFileManager defaultManager] copyItemAtURL:location toURL:saveURL error:&saveError];
if (!saveError) {
NSLog(@"保存成功");
} else {
NSLog(@"error is %@", saveError.localizedDescription);
}
} else {
NSLog(@"error is : %@", error.localizedDescription);
}
}];
// 恢復線程, 啟動任務
[downLoadTask resume];
}
#pragma mark -- 取消下載
-(void)cancleDownLoad
{
[_downloadTask cancel];
}
#pragma mark --- 掛起下載
- (void)suspendDownload
{
[_downloadTask suspend];
}
#pragma mark ---- 恢復繼續下載
- (void)resumeDownLoad
{
[_downloadTask resume];
}
#pragma mark ---- downLoadTask 代理方法
// 下載過程中 會多次調用, 記錄下載進度
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
// 記錄下載進度
}
// 下載完成
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
NSError *error;
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *savePath = [cachePath stringByAppendingPathComponent:@"savename"];
NSURL *saveUrl = [NSURL fileURLWithPath:savePath];
// 通過文件管理 復制文件
[[NSFileManager defaultManager] copyItemAtURL:location toURL:saveUrl error:&error];
if (error) {
NSLog(@"Error is %@", error.localizedDescription);
}
}
// 當調用恢復下載的時候 觸發的代理方法 [_downLoadTask resume]
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
{
}
#pragma mark --- 任務完成, 不管是否下載成功
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
}
#pragma mark --- session 後台下載完成 之後的操作 (本地通知 或者 更新UI)
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
{
AppDelegate *appdelgate = (AppDelegate *)[UIApplication sharedApplication].delegate;
if (appdelgate.backgroundSessionCompletionHandler) {
void (^completionHandle)() = appdelgate.backgroundSessionCompletionHandler;
appdelgate.backgroundSessionCompletionHandler = nil;
completionHandle();
}
}
NSURLSession 支持程序的後台下載和上傳, 蘋果官方將其稱之進程之外的上傳和下載, 這些任務都交給後台守護線程完成, 而不是應用本身, 及時文件在下載和上傳過程中崩潰了也可以繼續運行(如果用戶強制關閉程序的話, NSURLSession會斷開連接)
*
* 為了提高用戶體驗, 下載過程中進度條會一直刷新進度,
當程序進入後台後, 事實上任務是交給iOS系統來調度的, 具體什麼時候下載完成就不知道了,
如果下載完成之後, 文件下載進度應該在100位置, 由於程序已經在後台無法更新程序UI,
此時通過應用程序代理 (AppDelegate)方法進行UI更新
當NSURLSession在後台開啟幾個任務之後, 如果有其他幾個任務完成後系統會調用應用程序的
-(void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier
completionHandler:(void (^)())completionHandler代理方法
此方法會包含一個competionHandler (此操作表示應用中完成所有處理工作), 通常我們會保存此對象,
直到最後一個任務完成, 此時重新通過會話標識(sessionConfig中設置的)找到相應的會話並調用NSURLSession的
-(void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session代理方法, 在這個方法中通常可以
進行UI更新,並調用completionHandler通知系統已經完成所有的操作
#pragma mark --- 後台下載
- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler
{
self.backgroundSessionCompletionHandler = completionHandler;
}
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
{
AppDelegate *appdelgate = (AppDelegate *)[UIApplication sharedApplication].delegate;
if (appdelgate.backgroundSessionCompletionHandler) {
void (^completionHandle)() = appdelgate.backgroundSessionCompletionHandler;
appdelgate.backgroundSessionCompletionHandler = nil;
completionHandle();
}
}