IOS NSURLSession, iosnsurlsession

Source: Internet
Author: User

IOS NSURLSession, iosnsurlsession

1. NSURLSessionDataTask

1 // determine URL 2 NSString * urlStr = @ "http: // localhost/try"; 3 urlStr = [urlStr encoding: NSUTF8StringEncoding]; 4 NSURL * url = [NSURL URLWithString: urlStr]; 5 6 // confirm request 7 NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url cachePolicy: 0 timeoutInterval: 2.0f]; 8 9 // determine session 10 NSURLSession * session = [NSURLSession sharedSession]; 11 12 // task generated by session 13 NSURLSessionDataTask * dataTask = [session dataTaskWithRequest: request completionHandler: ^ (NSData * data, NSURLResponse * response, NSError * error) {14 NSLog (@ "response -- % @", response); 15 NSLog (@ "data -- % @", data); 16 17 // refresh the UI, be sure to return to the main thread to refresh 18 dispatch_async (dispatch_get_main_queue (), ^ {19 [self. imageView setImage: [UIImage imageWithData: data]; 20}); 21 22}]; 23 24 [dataTask resume];

 

2. NSURLSessionDownloadTask implements simple download Functions

1 // determine URL 2 NSString * urlStr = @ "http: // localhost/try"; 3 urlStr = [urlStr encoding: NSUTF8StringEncoding]; 4 NSURL * url = [NSURL URLWithString: urlStr]; 5 6 // confirm request 7 NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url cachePolicy: 0 timeoutInterval: 2.0f]; 8 9 // determine session 10 NSURLSession * session = [NSURLSession sharedSession]; 11 12 // task generated by session 13 NSURLSessionDownloadTask * downloadTask = [session downloadTaskWithRequest: request completionHandler: ^ (NSURL * location, NSURLResponse * response, NSError * error) {14 // copy the file from tmp to cache15 NSString * cachePath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) lastObject]; 16 NSURL * cachePathURL = [NSURL fileURLWithPath: cachePath]; 17 NSString * response = [[[response URL] absoluteString] MD5]; 18 NSURL * fileURL = [cachePathURL expires: 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 implements complex download functions: resumable Download \ background download

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.