nsurlsessionconfiguration-0202-Network

Source: Internet
Author: User

1 //2 //VIEWCONTROLLER.M3 //02-nsurlsessionconfiguration4 //5 #import "ViewController.h"6 7 @interfaceViewcontroller ()8 9 @endTen  One @implementationViewcontroller A  -- (void) Viewdidload { - [Super Viewdidload]; the     //additional setup after loading the view, typically from a nib. -      -      -     //Requirement: Use Nsurlsessionconfiguration to configure session +     /* - the flow of network requests: + 1. Construct Nsurl Connection Address A 2. Constructs the Nsurlrequest request object, which contains the request header and the request body information.  at 3. Construction Nsurlsessionconfiguration - 4. Constructing the Nsurlsession Session object - 5. Create a request task - 6. Send a network request -       -      */ in      -Nsurl *url = [Nsurl urlwithstring:@"Http://piao.163.com/m/cinema/list.html?app_id=1&mobileType=iPhone&ver=2.6&channel=appstore &deviceid=9e89cb6d-a62f-438c-8010-19278d46a8a6&apiver=6&city=110000"]; to      +     //Setting the Configuration object - //RequestCachePolicy: Setting the cache policy the //Networkservicetype: Set the type of Network service: network traffic, Internet telephony, voice, video. * //timeoutintervalforrequest: Setting the time-out period $ //httpadditionalheaders: Setting the request headerPanax Notoginseng //discretionary: For background requests, it takes into account the availability of WiFi and power - //allowscellularaccess: Is it allowed to use cellular data the      +Nsurlsessionconfiguration *config =[Nsurlsessionconfiguration defaultsessionconfiguration]; A      theConfig.timeoutintervalforrequest = the; +Config.allowscellularaccess =YES; -      $     //to create a session by using a configuration object $     //the process of listening to data download through Proxy protocol method -     /* - Agreement Inheritance Relationship: 3 compliance with 2,2 compliance 1, so 3 contains all 1, 2 method declarations in the Protocol the 1.NSURLSessionDelegate - 2.NSURLSessionTaskDelegateWuyi 3.NSURLSessionDataDelegate the      */ -Nsurlsession *session = [Nsurlsession sessionwithconfiguration:configDelegate: Self delegatequeue:[nsoperationqueue mainqueue]]; Wu      -     //The block can no longer be used to listen after the data is loaded using the proxy protocol.  AboutNsurlsessiontask *task =[Session Datataskwithurl:url]; $      - [Task resume]; -      -      A      + } the  - #pragmaMark-nsurlsessiondelegate $  the //This protocol method is called when a response header is received the- (void) Urlsession: (Nsurlsession *) session Datatask: (Nsurlsessiondatatask *) datatask didreceiveresponse: (NSURLResponse *) Response Completionhandler: (void(^) (nsurlsessionresponsedisposition)) Completionhandler { the      theNshttpurlresponse *httpresponse = (Nshttpurlresponse *) response; -      inNSLog (@"Statuscode:%li", Httpresponse.statuscode); the      theNSLog (@"%@", HttpResponse); About      the      the     //can continue loading and must be allowed before the response body data is loaded the Completionhandler (nsurlsessionresponseallow); +      -      the     /*nsurlsessionresponsecancel = 0, equivalent to [task Cancel] Nsurlsessionresponseallow = 1 Continue normal transmission nsurlsessionresponsebecomedownload = 2, turn Datatask into Downloadtask nsurlsessionrespo Nsebecomestream = 3 turn into StreamtaskBayi     */ the      the      - } -  the //This protocol method is called by the data in the response body each time a packet is received, and this protocol method may be called multiple times.  the- (void) Urlsession: (Nsurlsession *) session Datatask: (Nsurlsessiondatatask *) datatask didreceivedata: (NSData *) Data { the      theNSLog (@"Data:%li", data.length); -      the } the  the //This protocol method is declared in Nsurlsessiontaskdelegate, which is called when the task is loaded and the data is now transferred. 94- (void) Urlsession: (Nsurlsession *) session Task: (Nsurlsessiontask *) Task Didcompletewitherror: (Nserror *) Error { the      theNSLog (@"Network Request Complete"); the     98 } About  - @end

nsurlsessionconfiguration-0202-Network

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.