In IOS7 we can use the Nsurlsession class to make URL requests. If the earlier version uses Nsurlconnection, look at some of the related classes in iOS about URLs.
The Urlloading section is the main class we use, and the other five are helper classes. involved in all aspects. protocol support, encryption, cookie management, caching, configuration.
The behavior of a task in nsurlsession depends on three things: the type of the session (as determined by the incoming configuration object), the type of the task, and whether the application is in the foreground when the task is created.
type of Session
1. The default session uses DiskCache and saves the certificate in user keychain.
2. Short session does not have any data on disk, all related things in RAM, when the session is invalid, the content is automatically cleared.
3. Background session, similar to the default session, except for a separate process to handle all data transfer. There are some restrictions.
Type of Task
In one session, Nsurlsession supports three types of tasks: Data tasks, download tasks, upload tasks
Data task: Use the NSData to send and receive information for frequent, small-volume interactions. The data can be sent to the app when the data arrives, or callback when the data is received.
Download task: Receives data in the form of a file. And when the app is not running, background downloads are supported.
Upload task: Sends data (usually in the form of a file). and when the app is not running, background uploads are supported.
Create and configure a session
Nsurlsession offers a number of configuration options:
Specify storage cache, cookies, credentials location
Auth, associated with a special request (task), or a set of request (session)
File upload and download, separate from metadata.
The maximum number of connections per host.
Time-out for each resource,
Minimum and maximum TLS version support
Custom Proxy Dictionaries
Control Cookie Policy
Controlling HTTP pipelining Behavior
Because most of the settings are contained in a separate configuration object, you can reuse the configuration. When you instantiate a Session object, you specify:
A configuration object to manage the behavior of the session and task.
Optionally, a delegate object to process incoming data when it receives information and handles other events for session and task. For example, the service-side authentication, determines if I a resource load is converted into download, and so on.
If you do not provide a delegate, Nsurlsession uses the system provided. In this case, you can gladly use the Nsurlsession sendAsynchronousRequest:queue:completionHandler: method.
Note: If your app needs to perform a background transfer, you must provide a custom delegate.
After you instantiate the Seesion object, you cannot change the configuration or delegate without creating a new session.
Here's how to create normal, ephemeral, and background sessions.
self.completionhandlerdictionary = [nsmutabledictionary dictionarywithcapacity:0]; nsurlsessionconfiguration *backgroundconfigobject = [nsurlsessionconfiguration backgroundsessionconfiguration: @ "Mybackgroundsessionidentifier"]; nsurlsessionconfiguration *defaultconfigobject = [nsurlsessionconfiguration Defaultsessionconfiguration]; nsurlsessionconfiguration *ephemeralconfigobject = [nsurlsessionconfiguration Ephemeralsessionconfiguration]; nsurlcache *mycache = [[nsurlcache alloc] initwithmemorycapacity: 16384 Diskcapacity: 268435456 diskpath: cachepath];d efaultconfigobject.urlcache = mycache; Defaultconfigobject.requestcachepolicy = nsurlrequestuseprotocolcachepolicy;self.defaultsession = [nsurlsession sessionwithconfiguration: defaultconfigobject delegate: self delegatequeue: [nsoperationqueue mainqueue]];self.backgroundsession = [nsurlsession sessionwithconfiguration: backgroundconfigobject delegate: self delegatequeue: [nsoperationqueue mainqueue]]; Self.ephemeralsession = [nsurlsession sessionwithconfiguration: ephemeralconfigobject delegate: self delegateQueue: [NSOperationQueue mainQueue]];
In addition to the background configuration, you can re-use the session Configuraion object to create another session.
Because it is a deep copy.
About iOS Nsurlsession