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
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)