Use of IOS nsurlsession

Source: Internet
Author: User

1.NSURLSessionDataTask

1     //Determine URL2NSString *urlstr =@"http://localhost/try it.";3Urlstr =[Urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];4Nsurl *url =[Nsurl Urlwithstring:urlstr];5     6     //Determine request7Nsmutableurlrequest *request = [Nsmutableurlrequest requestwithurl:url cachepolicy:0timeOutInterval:2.0f];8     9     //Determine the sessionTenNsurlsession *session =[Nsurlsession sharedsession]; One      A     //task generated by session -Nsurlsessiondatatask *datatask = [Session datataskwithrequest:request completionhandler:^ (NSData *data, NSURLResponse *response, Nserror *error) { -NSLog (@"response--%@", response); theNSLog (@"data--%@", data); -          -         //Refresh UI, be sure to return to the main thread to refresh -Dispatch_async (Dispatch_get_main_queue (), ^{ + [Self.imageview setimage:[uiimage imagewithdata:data]; -         }); +  A     }]; at      -[Datatask resume];

2.NSURLSessionDownloadTask implementation of simple download function

1     //Determine URL2NSString *urlstr =@"http://localhost/try it.";3Urlstr =[Urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];4Nsurl *url =[Nsurl Urlwithstring:urlstr];5     6     //Determine request7Nsmutableurlrequest *request = [Nsmutableurlrequest requestwithurl:url cachepolicy:0timeOutInterval:2.0f];8     9     //Determine the sessionTenNsurlsession *session =[Nsurlsession sharedsession]; One      A     //task generated by session -Nsurlsessiondownloadtask *downloadtask = [Session downloadtaskwithrequest:request completionhandler:^ (NSURL * Location, Nsurlresponse *response, Nserror *error) { -         //copy files from tmp to cache theNSString *cachepath =[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject]; -Nsurl *cachepathurl =[Nsurl Fileurlwithpath:cachepath]; -NSString *FILENAMEBYMD5 =[[[ response URL] absolutestring] MD5]; -Nsurl *fileurl =[Cachepathurl urlbyappendingpathcomponent:filenamebymd5]; +  -Nsfilemanager *mgr =[Nsfilemanager Defaultmanager]; + [Mgr Moveitematurl:location tourl:fileurl error:null]; A          at     }]; -      -[Downloadtask resume];
1-(NSString *) MD52 {3     Const Char*cstr =[self utf8string];4UnsignedCharDigest[cc_md5_digest_length];5 6 cc_md5 (CStr, strlen (CSTR), digest);7 8nsmutablestring *result = [nsmutablestring stringwithcapacity:cc_md5_digest_length *2];9 Ten      for(inti =0; i < cc_md5_digest_length; i++) { One[Result AppendFormat:@"%02x", Digest[i]]; A     } -  -     returnresult; the}

3.NSURLSessionDownloadTask implementation of complex download features: Breakpoint resume \ Background Download

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

Use of IOS nsurlsession

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.