iOS development-nsurlsession

Source: Internet
Author: User
<span id="Label3"></p><p><p> <!--?xml version="1.0" encoding="UTF-8" standalone="no"?--></p></p>Nsurlsession<p><p></p></p>Nsurlsession is a nsurlconnection alternative, providing an optional, diverse strategy for nsurlrequest access to the network;<span style="color: #008400; font-family: Menlo;"><span style="color: #008400; font-family: Menlo;">a</span></span>The nsurlsession will correspond to a delegate,delegate that will be invoked in the Nsurlsession declaration cycle, such as server authentication, determining whether the resource is downloaded, etc. Nsurlsession is<span style="color: #800000;"><span style="color: #800000;">Thread-safe Classes</span></span>The default nsurlsession takes the system-provided delegate and can replace the previous +[nsurlconnection sendAsynchronousRequest:queue:completionHandler:] function Nsurlsession will create the Nsurlsessiontask class, which is similar to nsurlconnection, but provides more control and uniform proxy Mode. Nsurlsessiontask<span style="color: #800000;"><span style="color: #800000;">Nsurlsessiontask is created by default is the suspended state (hang), you need to call resume before executing;</span></span>Several subclasses of Nsurlsessiontask are used to syntactically differentiate between data and file Downloads.<span style="color: #800000;"><span style="color: #800000;">Nsurlsessiondatatask</span></span>When a class receives a resource, it calls URLSession:dataTask:didReceiveData multiple times, which is often used to accept data that is immediately used by the User.<span style="color: #800000;"><span style="color: #800000;">Nsurlsessionuploadtask</span></span>and nsurlsessiondatatask of the structure of different, the task will be uploaded by-urlsession:task:neednewbodystream: the way to accurately locate the file or data, to fill in the uploaded body.<span style="color: #800000;"><span style="color: #800000;">Nsurlsessiondownloadtask</span></span>Writes the data of the response directly to a temporary file, and when finished, delegate sends a URLSession:downloadTask:didFinishDownloadingToURL, which allows the temporary file to be moved to the appropriate location in the application Sandbox. Or read the file directly; if canceled, Nsurlsessiondownloadtask will produce a temporary data that can then be used to restore the original download Task. After iOS9, the Nsurlsessionstream can be used for TCP/IP connections, the corresponding port of the host is connected through a secure handshake and proxy, and the task of data can be implemented with Nsurlsessionstream. Appropriate configuration of the nsurlsessionconfiguration is required, depending on RFC2817 and RFC6455, Upgrade:header. Http://www.ietf.org/rfc/rfc2817.txt<br class="Apple-interchange-newline">The Nsurlsession class provides http-related interfaces, an API that provides an Authentication-rich proxy method that provides the ability for the app to download background downloads (even If the app is suspended), and the app can create a series of sessions through the Nsurlsession Interface. Each session includes a data-related task group; for example, If you are writing a Web browser, your app may have a single page or a new session in each window, and in each session your app will add a series of tasks, each of which represents a unique URL Request. With these conversations, you can schedule three types of tasks: data task (download), download task (upload), and upload Task. Like most network layer apis, nsurlsession is asynchronous. Data can be returned in the following ways: 1, the completion of the block to return data, 2, when the data received, through the delegate callback, 3, the completion of the download, through the delegate callback; Nsurlsession provides status and progress properties, while supporting the cancellation, restart, Recovery and suspend operations, as well as pending, canceled, and failed DOWNLOADS. 1, nsurlsession   Session class 2, Nsurlsessionconfiguration configuration class, used to initialize session3, nsurlsessiontask   Task base class 4, Nsurlsessiondatatask Data Task class 5, Nsurlsessionuploadtask upload task class 6, nsurlsessiondownloadtask Download Task Class Nsurlsession provides four protocols, Control task 1, Nsurlsessiondelegate handle Session level event 2, nsurlsessiontaskdelegate handle all Task level event 3, Nsurlsessiondatadelegate Handle Task-level events, for data and upload two tasks 4, nsurlsessiondownloaddelegate for tasks at task level, for download Tasks nsurlsession Also used a lot of nsurlconnection, nsurldownload inside the class. 1, Nsurl  url class 2, nsurlrequest encapsulates the URL request data, including url, request method, etc. 3, Nsurlresponse encapsulates the data returned by the server, including MIME type and length 4, Nshttpurlresponse IncreaseAdded a reply specifically for HTTP requests, such as response header (response Header) 5, Nscacheurlresponse encapsulates a nsurlresponse, and specific server response data for caching   Nscopying Related If you return the same class to a session or task, if you return a new class to the configuration, you can modify;  to redirect some protocols, like http, to provide a way for the server to tell the app The link has changed to a new url. When a redirect occurs, the Url-loading class asks the app via delegate, redirects, returns the redirected response, or returns the error directly.     examples  <p><p>1. Loading local files via URL</p></p><pre>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Testfileindisk {nsstring</span></span>* Path = [[nsbundle mainbundle] pathforresource:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Ios_url</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>OfType:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">PNG</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; Nsurl</span></span>* URL =<span style="color: #000000;"><span style="color: #000000;">[nsurl fileurlwithpath:path]; [[[nsurlsession sharedsession] Datataskwithurl:url completionhandler:</span></span>^ (nsdata * _nullable data, nsurlresponse * _nullable response, Nserror *<span style="color: #000000;"><span style="color: #000000;">_nullable Error) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">NSLog (@ "data%@", [data description]);</span></span>NSLog (<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">response%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [response description]); NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">error%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [error description]); UIImage</span></span>* Image =<span style="color: #000000;"><span style="color: #000000;">[UIImage imagewithdata:data]; Dispatch_async (dispatch_get_main_queue (),</span></span>^<span style="color: #000000;"><span style="color: #000000;">{[self.imageview setimage:image]; }); }] [resume];}</span></span></pre><p><p></p></p><p><p>2. Loading web files via URLs</p></p><pre>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Testurl {nsurl</span></span>* url = [nsurl urlwithstring:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">http://www.baidu.com/</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>];<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">this is different from https.</span></span>[[[nsurlsession sharedsession] datataskwithurl:url completionhandler:^ (nsdata * _nullable data, NSURLResponse * _ Nullable response, Nserror *<span style="color: #000000;"><span style="color: #000000;">_nullable Error) {NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">string%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]); NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">response%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [response description]); NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">error%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [error description]); UIImage</span></span>* Image =<span style="color: #000000;"><span style="color: #000000;">[UIImage imagewithdata:data]; Dispatch_async (dispatch_get_main_queue (),</span></span>^<span style="color: #000000;"><span style="color: #000000;">{[self.imageview setimage:image]; }); }] [resume];}</span></span></pre><p><p></p></p><p><p>Examples In:</p></p><p><p>Https://github.com/loyinglin/LearnNSURLSession</p></p><span class="s1"><span class="s1">Reference</span></span><p class="p1"><p class="p1"><span class="s1">Usingnsurlconnection</span></p></p>Encoding URL Data life Cycle of a URL Session<p><p></p></p><p><p>iOS development-nsurlsession</p></p></span>

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.