iOS NSURLSession的使用,iosnsurlsession

來源:互聯網
上載者:User

iOS NSURLSession的使用,iosnsurlsession

1.NSURLSessionDataTask

 1     // 確定URL 2     NSString *urlStr = @"http://localhost/試試看"; 3     urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 4     NSURL *url = [NSURL URLWithString:urlStr]; 5      6     // 確定request 7     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:2.0f]; 8      9     // 確定會話10     NSURLSession *session = [NSURLSession sharedSession];11     12     // 由會話產生任務13     NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {14         NSLog(@"response-- %@",response);15         NSLog(@"data--%@",data);16         17         // 重新整理UI,一定要返回主線程去重新整理18         dispatch_async(dispatch_get_main_queue(), ^{19             [self.imageView  setImage:[UIImage imageWithData:data]];20         });21 22     }];23     24     [dataTask resume];

 

2.NSURLSessionDownloadTask實現簡單的下載功能

 1     // 確定URL 2     NSString *urlStr = @"http://localhost/試試看"; 3     urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 4     NSURL *url = [NSURL URLWithString:urlStr]; 5      6     // 確定request 7     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:2.0f]; 8      9     // 確定會話10     NSURLSession *session = [NSURLSession sharedSession];11     12     // 由會話產生任務13     NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {14         // 從tmp中拷貝檔案到cache15         NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];16         NSURL *cachePathURL = [NSURL fileURLWithPath:cachePath];17         NSString *fileNameByMD5 = [[[response URL] absoluteString] MD5];18         NSURL *fileURL = [cachePathURL URLByAppendingPathComponent: fileNameByMD5];19 20         NSFileManager *mgr = [NSFileManager defaultManager];21         [mgr moveItemAtURL:location toURL:fileURL  error:NULL];22         23     }];24     25     [downloadTask resume];
 1 - (NSString *)MD5 2 { 3     const char *cStr = [self UTF8String]; 4     unsigned char digest[CC_MD5_DIGEST_LENGTH]; 5  6     CC_MD5(cStr, strlen(cStr), digest); 7  8     NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; 9 10     for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {11         [result appendFormat:@"%02x", digest[i]];12     }13 14     return result;15 }

3.NSURLSessionDownloadTask實現複雜的下載功能:斷點續傳\後台下載

http://blog.csdn.net/majiakun1/article/details/38133789

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.