ios-Network Fundamentals

Source: Internet
Author: User

/** Objective: To load data from the Web server, can guarantee the content inside the application, often new, update!   Data format for network transmission: * JSON {} dictionary [] array similar to fast wrapping array and dictionary in OC * XML with < tag ></Mark end > Paired appearance Format parsing: * * JSON parsing Nsarray *array = [Nsjsonserialization jsonobjectwithdata:data Options:nsjsonreadingallowfrag     ments Error:nil]; * * XML parsing*///1. Instantiating the parserNsxmlparser *parser =[[Nsxmlparser alloc] initwithdata:data];//2. AgentParser.Delegate=Self ;//3. Parser parsing[parser parse];//Parsing Steps/** 1. The document starts ======================================== 2\3\4 loop execution until the end of the parsing document is 2. Start node 3. Node content 4. Complete node ==================== ==================== 5. Document Completion 6. Error*///The most easy-to-use XML format//in real-world development, both XML and JSON are provided by the company's background programmers.//Reference URL:Http://ebookservice2.oss.aliyuncs.com/upload/xml/hotpaihang-1.xml<book pages=" A"index="1"><itemID="1558"title="wrong in the big club: hubby Don't touch me"tag="Urban Romance"Source="/upload/file/2013_07_4/2013718101155179.zip"Author="the only mystery of the butterflies"Updatetime="2013-6-21 11:06:17"summary="the wedding night ."clicks="90679"words=""logo="/upload/pic/2013_06_5/20136211172503.png"jx="False"/></book>/** ======================================================================= Network Access common method Get & POST * * Get Direct data, will be stitching parameters in the URL, high efficiency, high security direct access to network data * * Post to the data body submitted to the server, and then obtain the results, the efficiency is slightly lower, security is not good with post, user login, upload files about the user registration login part of the processing tips: * User's real password, It is valid when registering or logging in, and in addition, it should be passed with an encrypted password. "Add salt!" "Network access Step 1. Nsurl 2. Nsurlrequest 3. Nsurlconnection Synchronous/Asynchronous Methods*//** ======================================================================= nsurlsession If you simply get the data,  Direct use of nsurlconnection can now achieve the ability to continue the breakpoint, do very good! Use Step 1 of the nsurlsession. Nsurl 2. Nsurlrequst 3.  Nsurlsession nsurlsession *session = [Nsurlsession sharedsession]; 4. Nsurlsessiontask (Datatask, Downloadtask, uploadtask) Nsurlsessiondatatask + XML, JSON, HTML hint: After the task is established, the default is suspended, you need to To start a task using resume. */Nsurlsessiondatatask*task = [Session datataskwithrequest:request completionhandler:^ (NSData *data, Nsurlresponse *response, NSError *error) {        //Block code for network data task completionNSString *html =[[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; [_webview loadhtmlstring:html Baseurl:url];}];//Start (Resume) a task[Task resume];//Download the code for the task//1. URL//Note: In developing a network application, to avoid unnecessary errors, all URL strings need to be added with a percent sign//before converting, get the file name in advanceNSString *filename =[URLString lastpathcomponent];urlstring=[URLString stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; Nsurl*url =[Nsurl urlwithstring:urlstring];//2. Session//2.1 session configuration, using default configuration//nsurlsessionconfiguration *configure = [nsurlsessionconfiguration backgroundsessionconfiguration:@ "identifier of the background task"]; Nsurlsessionconfiguration *configure =[Nsurlsessionconfiguration defaultsessionconfiguration]; Nsoperationqueue*queue =[[Nsoperationqueue alloc] Init];_session= [Nsurlsession sessionwithconfiguration:configureDelegate: Self delegatequeue:queue];//3. Download Tasks//If you want the agent to participate in monitoring the download progress, when you instantiate the download task, you cannot specify the block code after completion_downloadtask =[_session Downloadtaskwithurl:url];//4. Start a task[_downloadtask resume];// ==================================================================//The download process is controlled through the proxy method//there are altogether three proxy methods

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.