Learning Nsurlsession (1)

Source: Internet
Author: User

Nsurlsession in iOS7, the launch of this. It is mainly used to replace Nsurlconnection.

Nsurlsession is a group of interdependent classes, most of which have the same components as Nsurlrequest,nsurlcache in Nsurlconnection. The difference between nsurlsession is that it replaces nsurlconnection with Nsurlsession and Nsurlsessionconfiguration, and 3 sub-classes of Nsurlsessiontask: Nsurlsessiondatatask, Nsurlsessionuploadtask, and Nsurlsessiondownloadtask.

About the legend:

The nsurlsession is comprised of nsurlsessionconfiguration and an optional agent (optional delegate). Then create a session with Nsurlsessiontask based on your network needs.

1, Nsurlsessionconfiguration

Brief introduction
    • Nsurlsessionconfiguration is mainly used to configure Nsurlsession session properties, which have three main modes of operation:

      • Default mode (defaultsessionconfiguration): Use the hard disk to cache the data, you can use the cached Cache,cookie, authentication.

      • In-time mode (ephemeralsessionconfiguration): temporary session configuration, compared to the default configuration, this configuration will not cache, cookies, etc. exist locally, only in memory, so when the program exits, all the data will disappear

      • Background mode (backgroundsessionconfiguration): In the background to complete the upload download, create a configuration object, you need to give a NSString ID used to track the completion of the session is which. This configuration is used when making remote push notifications or when the application hangs.

Common Properties
    • Identifie: Set the identifier for a background task

    • RequestCachePolicy: Setting the Cache policy

    • Timeoutintervalforrequest: Set Request Timeout duration

    • Networkservicetype: Setting the Network Service type

    • Httpadditionalheaders: Setting the head parameters

    • Allowscellularaccess: Set whether to request the network (using cellular data) in a non-wireless environment

    • Httpmaximumconnectionsperhost: Set the number of host network connections

    • The Discretionary:discretionary property is yes when the program chooses the best network connection configuration by the system itself when it is running in the background, which can save bandwidth through the cellular connection. When transferring data in the background, it is recommended to use discretionary properties, not allowsCellularAccess attributes, because it takes into account WiFi and power availability. This flag allows the system to optimize performance for assigned tasks. This means that the device only transmits data over WiFi when the device has enough power. If the battery is low, or if there is only one cellular connection, the transfer task will not run. Background transfers always run in discretionary mode.

Common methods
    • Sethttpadditionalheaders: Set head/Parameters

Set all requests to receive JSON data only;

Set the header parameters required by the network request API;

[Configuration sethttpadditionalheaders:@{@ "Accept": @ "Application/json" @ " apikey": @ " 159d95a8c050074fe3ca0df0d901fc62 "}];

2. Nsurlsession Network Session

    • Nsurlsession is designed to replace Nsurlconnection. All of sessions's work is done through the Nsurlsessiontask object. You can create a task with block,delegate, or by mixing them.

Learning Nsurlsession (1)

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.