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