日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

NSURLSession的应用

發(fā)布時間:2023/12/13 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NSURLSession的应用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

iOS7以后發(fā)布了NSURLSession用來替換NSURLConnection,NSURLSession使用方式有以下兩種:
1.block方式
(1)創(chuàng)建的步驟
獲取單例會話對象
創(chuàng)建URL對象
隱含創(chuàng)建request
創(chuàng)建NSURLSessionDataTask

// 1.獲取會話對象 NSURLSession *session = [NSURLSession sharedSession]; //另外一種生成默認(rèn)的GET請求的方法 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video"];NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];NSLog(@"%@", dict);

(2)使用NSURLSessionDataTask
創(chuàng)建session
創(chuàng)建URL
創(chuàng)建URLRequest
創(chuàng)建NSURLSessionDataTask

// 2.創(chuàng)建的一URL地址 NSURL *url = [NSURL URLWithString:@"http://192.168.15.172:8080/MJServer/login"]; // 3.創(chuàng)建一個請求對象 NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"POST"; request.HTTPBody = [@"username=123&pwd=123" dataUsingEncoding:NSUTF8StringEncoding];NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];NSLog(@"%@", dict); }];[task resume];

(3)使用NSURLSessionDownloadTask

// 1.得到session對象
NSURLSession *session = [NSURLSession sharedSession];
// 2.創(chuàng)建URL對象

NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/test.mp4"];NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {//NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];NSString *filePath = [caches stringByAppendingPathComponent:response.suggestedFilename];NSFileManager *fileManager = [NSFileManager defaultManager];[fileManager moveItemAtPath:location.path toPath:filePath error:nil];}];

2.代理方式

  • (void)downLoadTask2 {
    NSURLSessionConfiguration *cfg = [NSURLSessionConfiguration defaultSessionConfiguration];
    // 使用配置對象獲取會話對象
    NSURLSession *session = [NSURLSession sessionWithConfiguration:cfg delegate:self delegateQueue:[NSOperationQueue mainQueue]];

    // 創(chuàng)建一個URL
    NSURL *url = [NSURL URLWithString:@”http://localhost:8080/MJServer/resources/test.mp4“];
    NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url];

    [task resume];

}
- (void)URLSession:(NSURLSession )session downloadTask:(NSURLSessionDownloadTask )downloadTask didFinishDownloadingToURL:(NSURL *)location {
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [caches stringByAppendingPathComponent:downloadTask.response.suggestedFilename];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager moveItemAtPath:location.path toPath:filePath error:nil];
}

  • (void)URLSession:(NSURLSession )session downloadTask:(NSURLSessionDownloadTask )downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
    double progress = (double)totalBytesWritten / totalBytesExpectedToWrite;
    NSLog(@”已經(jīng)下載了:%f”, progress);

}

總結(jié)

以上是生活随笔為你收集整理的NSURLSession的应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。