iOS Development Network programming--6, nsurlsessionconfiguration notes
Source: Internet
Author: User
<span id="Label3"></p><p><p></p></p><p>The Nsurlsessionconfiguration object is used to initialize the nsurlsession object.<br><br>Expand the selection of nsmutableurlrequest-related options in the request level, and we can see how nsurlsessionconfiguration provides considerable control and flexibility for how the session generates the Request. From network access performance, to cookies, security, caching policies, Custom protocols, Startup event settings, and several new properties for mobile device optimization, you'll find that you've been looking for exactly nsurlsessionconfiguration.<br><br>The sessions replicate their configuration at initialization time, and Nsurlsession has a read-only configuration property that makes the changes on the configuration object invalid for this Session's policy. The configuration is read once during initialization and will not change after That.<br><br>-construction method<br><br>Nsurlsessionconfiguration has three class constructors, which is a good illustration of how nsurlsession is designed for different use Cases.<br><br>+ "defaultsessionconfiguration" returns the standard configuration, which is actually the same as the Nsurlconnection network protocol stack, with the same shared nshttpcookiestorage, Share Nsurlcache and shared Nsurlcredentialstorage.<br>+ "ephemeralsessionconfiguration" Returns a preset configuration with no persistent stored cache, cookie or Certificate. This is ideal for implementing functions like "secret browsing" Functionality.<br>+ "backgroundsessionconfiguration": What makes it unique is that it creates a background session. A background session differs from a regular, normal session, and it can even run uploads and download tasks in the event that the application hangs, exits, Crashes. The identifier specified at initialization is used to provide context to any daemon that may resume background transfers outside the PROCESS.<br><br>To see more information about background sessions, You can view WWDC session 204: "what's New with multitasking"<br><br>Properties of The-nsurlsessionconfiguration<br><br>Nsurlsessionconfiguration has 20 properties. Mastering the usefulness of these attributes will enable the application to take full advantage of its network Environment.<br><br>The most important attributes:<br># Replace Forhttpheaderfield in request to tell the server additional information about the client<br>"httpadditionalheaders" Specifies a default set of data headers that can be set for outbound Requests. This is useful for sharing information across sessions, such as content types, languages, user agents, and Identity Authentication.<br><br># WebDAV Authentication<br>NSString *userpasswordstring = [nsstring stringwithformat:@ "%@:%@", user, password];<br>NSData * Userpassworddata = [userpasswordstring datausingencoding:nsutf8stringencoding];<br>NSString *base64encodedcredential = [userpassworddata base64encodedstringwithoptions:0];<br>NSString *authstring = [nsstring stringwithformat:@ "Basic:%@", base64encodedcredential];<br><br># Setting the client type<br>NSString *useragentstring = @ "iPhone applewebkit";<br><br>Configuration. Httpadditionalheaders = @{@ "Accept": @ "application/json",<br>@ "accept-language": @ "en",<br>@ "Authorization": authstring,<br>@ "user-agent": useragentstring};<br><br>The Networkservicetype (network service Type) distinguishes between standard network traffic, voip, voice, video, and traffic used by a background process. Most applications do not need to set this<br><br>"allowscellularaccess (allow Cellular access)" and "discretionary (discretionary)" are used to conserve bandwidth over cellular connections. It is recommended that you use the Discretionary property instead of the Allowscellularaccess property when using background transfers, as it takes WiFi and power availability into account<br><br>"timeoutintervalforrequest" and "timeoutintervalforresource" Specify the request and the time-out interval for the Resource. Many developers tried to use timeOutInterval to limit the total time it took to send a request, but this misunderstood Timeoutinterval's meaning: the time between Messages. Timeoutintervalforresource actually provides the overall timeout feature, which should only be used for background transfers, not what the user might actually want to wait for<br><br>"httpmaximumconnectionsperhost" is a new configuration option for the URL loading system in the Foundation Framework. It has been used for nsurlconnection to manage private connection Pools. Now with nsurlsession, developers can limit the number of connections to specific hosts when needed<br><br>"httpshouldusepipelining" also appears in nsmutableurlrequest, which can be used to open the HTTP pipeline, which can significantly reduce the load time of the request, but is disabled by default because it is not widely supported by the server<br><br>"sessionsendslaunchevents" is another new property that specifies whether the session should start from the background<br><br>"connectionproxydictionary" Specifies the proxy server in the session Connection. similarly, Most consumer-facing applications do not require a proxy, so there is basically no need to configure this property<br>More information about connection agents can be found in Cfproxysupport Reference.<br><br>"Cookie policies"<br>-"httpcookiestorage" is the cookie store used by the Session. by default, Nshttpcookieshorage + sharedhttpcookiestorage will be used, which is the same as Nsurlconnection<br>-"httpcookieacceptpolicy" determines the condition that the session should accept cookies issued from the server<br>-"httpshouldsetcookies" Specifies whether the request should use a cookie for session Httpcookiestorage<br><br>"Security policies"<br>Urlcredentialstorage is the certificate store used by the Session. by default, Nsurlcredentialstorage + sharedcredentialstorage will be used, which is the same as Nsurlconnection<br><br>"tlsmaximumsupportedprotocol" and "tlsminimumsupportedprotocol" determine if a sslprotocol version of the session is supported<br><br>"Caching policies"<br>Urlcache is the cache used by the Session. by default, Nsurlcache + Sharedurlcache will be used, which is the same as Nsurlconnection<br>REQUESTCACHEPOLICY specifies when the cached response for a request should be Returned. This is equivalent to The-cachepolicy method of Nsurlrequest.<br><br>"Custom protocols"<br>Protocolclasses is a specific session array that registers the Nsurlprotocol class</p><p><p>iOS Development Network programming--6, nsurlsessionconfiguration notes</p></p></span>
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