Nsurlsession--memo

Source: Internet
Author: User
<span id="Label3"></p><span style="color: #33cccc;"><span style="color: #33cccc;">nsurlsession</span></span><p><p><span style="color: #000000;">Nsurlsession is <span style="color: #ff0000;">iOS7 out of the API</span>, in its former common native network library is nsurlconnection, but because connection is not very convenient to use, so we have been inclined to afnetworking, Afnetworking is really powerful but we should not forget nsurlsession because <span style="color: #ff0000;">nsurlsession is also very convenient to Use. </span></span></p></p><span style="color: #000000;"><span style="color: #000000;">nsurlsession Three modes of operation:</span></span> <ul> <ul> <li><span style="color: #ff0000;">Default session Mode: This mode is a persistent policy that uses disk caching to authenticate authorization with a certificate saved in user Keychain.</span></li> <li><span style="color: #ff0000;">Instantaneous session mode: This mode does not use the disk to cache any data, all caches, certificates, cookies, etc. are stored in ram, so when the program exits, these caches will be cleaned out.</span></li> <li><span style="color: #ff0000;">Background session mode: This mode in the background to complete the upload download function, you need to create a configuration object</span></li> </ul> </ul>Nsurlsession's three Missions: <ul> <ul> <li><span style="color: #ff0000;">Request Data Task</span></li> <li><span style="color: #ff0000;">Uploading a file task</span></li> <li><span style="color: #ff0000;">Download a file task</span></li> </ul> </ul><p><p></p></p><span style="color: #993366;"><span style="color: #993366;">Configuration class: nsurlsessionconfiguration</span></span><pre class="brush:swift;gutter:true;"><pre class="brush:swift;gutter:true;">Let configuration = nsurlsessionconfiguration.defaultsessionconfiguration () configuration.allowscellularaccess = Trueconfiguration.discretionary = Trueconfiguration.timeoutintervalforrequest = 30.0configuration.timeoutintervalforresource = 60.0configuration. Httpadditionalheaders = ["Accept": "application/json"]configuration. Httpmaximumconnectionsperhost = 5</pre></pre>Initialize the class Method: <ul> <ul> <li><span style="color: #ff0000;">Nsurlsessionconfiguration.defaultsessionconfiguration (): Default operating mode</span></li> <li><span style="color: #ff0000;">Nsurlsessionconfiguration.ephemeralsessionconfiguration (): instantaneous operation mode</span></li> <li><span style="color: #ff0000;">Nsurlsessionconfiguration.backgroundsessionconfiguration (identifier:string): background Work mode (iOS7)</span></li> <li><span style="color: #ff0000;">Nsurlsessionconfiguration.backgroundsessionconfigurationwithidentifier (identifier:string): background Work mode (iOS8)</span></li> </ul> </ul>Common Properties: <ul> <ul> <li><span style="color: #ff0000;">Allowscellularaccess: whether to allow the use of cellular network Connections.</span></li> <li><span style="color: #ff0000;">Discretionary: whether to allow free configuration, when set to true by the system to select the best network Connection. It is recommended to set to true when uploading and downloading tasks because the system uses the optimal mode for tasks (including WiFi and power).</span></li> <li><span style="color: #ff0000;">timeoutintervalforrequest: Specifies the request time-out period, which expires if the request is not issued within the specified Time.</span></li> <li><span style="color: #ff0000;">timeoutintervalforresource: Specifies the request time-out period, which times out if the requested resource cannot be found within the specified Time.</span></li> <li><span style="color: #ff0000;">Httpadditionalheaders: sets the additional request header Configuration.</span></li> <li><span style="color: #ff0000;">Httpmaximumconnectionsperhost: limit the maximum number of network connections</span></li> </ul> </ul><p><p></p></p><span style="color: #993366;"><span style="color: #993366;">nsurlsession</span></span><pre class="brush:swift;gutter:true;"><pre class="brush:swift;gutter:true;">Let session = Nsurlsession.sharedsession () Let session = Nsurlsession (configuration:configuration) Let session = Nsurlsession (configuration:configuration, delegate:self, delegatequeue:nsoperationqueue ())</pre></pre>Three initialization methods: <ul> <ul> <li class="brush:swift;gutter:true;"><span style="color: #ff0000;">Nsurlsession.sharedsession (): Gets the global shared session that will use the global cache, cookie, and Certificate.</span></li> <li class="brush:swift;gutter:true;"><span style="color: #ff0000;">Nsurlsession (configuration:configuration): creates a session class from the configuration Object.</span></li> <li class="brush:swift;gutter:true;"><span style="color: #ff0000;">Nsurlsession (configuration:configuration, delegate:self, Delegatequeue:nsoperationqueue ()): similar to the second method, Additional agent and agent threads are set to respond to various events</span></li> </ul> </ul><p class="brush:swift;gutter:true;"><p class="brush:swift;gutter:true;"></p></p><span style="color: #993366;"><span style="color: #993366;">Nsurlsessiontask</span></span><span style="color: #000000;"><span style="color: #000000;">Nsurlsessiontask is an abstract class that is typically used by its three subclasses:</span></span> <ul> <ul> <li><span style="color: #ff0000;">Nsurlsessiondatatask: most commonly used to get JSON Data.</span></li> <li><span style="color: #ff0000;">Nsurlsessionuploadtask: mainly used for uploading files.</span></li> <li><span style="color: #ff0000;">Nsurlsessiondownloadtask: used primarily for downloading Files.</span></li> </ul> </ul><p><p></p></p><span style="color: #800000;"><span style="color: #800000;">Nsurlsessiondatatask</span></span><span style="color: #339966;"><span style="color: #339966;">Create by URL</span></span><pre class="brush:swift;gutter:true;"><pre class="brush:swift;gutter:true;">Let session = Nsurlsession (configuration:configuration, delegate:self, delegatequeue:nsoperationqueue ()) let URL = Nsurl (string: "some request Path") let task = Session.datataskwithurl (url!)</pre></pre><pre class="brush:swift;gutter:true;"><pre class="brush:swift;gutter:true;">/*-------------------------------split line-------------------------------*/</pre></pre><pre class="brush:swift;gutter:true;"><pre class="brush:swift;gutter:true;">Let session = Nsurlsession (configuration:configuration, delegate:self, delegatequeue:nsoperationqueue ()) Let url = NSUR L (string: "some Request Path") Let task = Session.datataskwithurl (url!) {(data, response, Error) in//request after action}</pre></pre><span style="color: #339966;"><span style="color: #339966;">Create with URLRequest</span></span><pre class="brush:swift;gutter:true;"><pre class="brush:swift;gutter:true;"> Let session = Nsurlsession (configuration:configuration, delegate:self, delegatequeue:nsoperationqueue ()) let URL = Nsurl (string: "some request Path") let request = Nsurlrequest (url:url!) Let task = Session.datataskwithrequest (request)/ *------------------------------- Split line-------------------------------*/<br><br> Let session = Nsurlsession (configuration:configuration, delegate:self, delegatequeue:nsoperationqueue ()) let URL = Nsurl (string: "some request Path") let request = Nsurlrequest (url:url!) Let task = Session.datataskwithrequest (request) {(data, response, error) in //request complete operation</pre></pre><p><p></p></p><span style="color: #800000;"><span style="color: #800000;">Nsurlsessionuploadtask</span></span><pre class="brush:swift;gutter:true;"><pre class="brush:swift;gutter:true;"> Let task = session.uploadtaskwithstreamedrequest (request) let task = Session.uploadtaskwithrequest (request, Fromdata:somedata) let task = session.uploadtaskwithrequest (request, Fromfile:fileurl) let task = Session.uploadtaskwithrequest (request, fromdata:somedata) {(data, response, error) in //upload complete operation } let Task = Session.uploadtaskwithrequest (request, Fromfile:fileurl) {(data, response, error) in //upload complete operation }</pre></pre><p><p></p></p><span style="color: #800000;"><span style="color: #800000;">Nsurlsessiondownloadtask</span></span><span style="color: #339966;"><span style="color: #339966;">Create with URLRequest</span></span><pre class="brush:swift;gutter:true;"><pre class="brush:swift;gutter:true;"> Let Downloadtask = session.downloadtaskwithrequest (request) let downloadtask = Session.downloadtaskwithrequest ( Request) {(url, response, Error) in //download complete operation }</pre></pre> <span style="color: #339966;"><span style="color: #339966;">Create by URL</span></span><pre class="brush:swift;gutter:true;"><pre class="brush:swift;gutter:true;"> Let Downloadtask = Session.downloadtaskwithurl (url!) Let Downloadtask = Session.downloadtaskwithurl (url!) {(url, response, Error) in //download complete operation }</pre></pre><span style="color: #339966;"><span style="color: #339966;">continue to download</span></span><pre class="brush:swift;gutter:true;"><pre class="brush:swift;gutter:true;"> Let Downloadtask = Session.downloadtaskwithresumedata (resumedata) let downloadtask = Session.downloadtaskwithresumedata (resumedata) {(url, response, error) in //download complete operation }</pre></pre><p><p></p></p><p><p>Nsurlsession--memo</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.