Ios-three-party Framework afnetworking Basic use

Source: Internet
Author: User
<span id="Label3"></p><p><p>Afnetworking is based on nsurlconnection, Nsoperation developed a three-party framework, mainly for dealing with some of the network requests on the business, the following will be a brief introduction to the framework of commonly used functions, such as file upload, download, parsing.</p></p>I. Get&post requests<p><p>1. First of all, of course, the import header file</p></p><pre><pre><span style="color: #0000ff;">#import</span> <span style="color: #800000;">"</span> <span style="color: #800000;">AFNetworking.h</span> <span style="color: #800000;">"</span></pre></pre><p><p>2. Create a Session Manager (the Manager object created is not a Singleton)</p></p><pre><pre>Afhttpsessionmanager *manager = [afhttpsessionmanager manager]</pre></pre><p><p>3. finally, It's simple, Just call the method directly.</p></p><p><p>Get behind the first parameter, is what we call the request header, but here is a note: usually we use nsurlconnection and nsurlsession when send GET request when all content is written in the request header, that is, the request address to add a large push parameter, And in this method AFN the <span style="background-color: #ffffff; color: #ff9900;">request body information separately</span> , placed on the red background of this area, you can upload the request you want to wrap the body into a dictionary, and then add it as a parameter, there is a good thing <span style="color: #ff9900;">is</span> that when we send a post request, You only need to replace the get in this method with a post</p></p><pre>GET request<br>[manager GET:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">http://***.**.***.***:* ****/login</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>Parameters<span style="background-color: #ff0000;"><span style="background-color: #ff0000;"><# (nullable id) #></span></span>Progress:nil<br>success:^ (nsurlsessiondatatask * _nonnull task,<span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span><span style="color: #000000;"><span style="color: #000000;">_nullable Responseobject) If the <span style="color: #339966;">request succeeds, This block,responseobject is called the response body information, and the information has been serialized internally</span> </span></span>NSLog (<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">%@--%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>, [responseobject<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">],responseobject); } failure:</span></span>^ (nsurlsessiondatatask * _nullable task, Nserror *<span style="color: #000000;"><span style="color: #000000;">_nonnull Error) { <span style="color: #339966;">If the request fails, this block</span> <span style="color: #000000;">error is invoked as the <span style="color: #339966;">reason for the request failure</span> </span> }];<br><span style="color: #ff0000;">The POST request shows that two methods are only one word</span><br></span></span></pre><pre><pre> [manager POST:<span style="color: #800000;">@ "</span>//<span style="color: #800000;"><span style="color: #800000;">***.**.***.***:* * * * * * * *</span>/login</span><span style="color: #800000;">"</span> parameters:<span><# ( Nullable Id) #></span><span style="color: #0000ff;">ID</span><span style="color: #000000;"> _nullable Responseobject) { </span><span style="color: #008000;">//</span><span style="color: #008000;">3. parsing</span> NSLog (<span style="color: #800000;">@ "</span><span style="color: #800000;">%@--%@</span><span style="color: #800000;">"</span><span style="color: #0000ff;">class</span><span style="color: #000000;">],responseobject); </span> = =<span style="color: #000000;"> } failure:</span>^ (nsurlsessiondatatask * _nullable task, nserror *<span style="color: #000000;"> _nonnull error) }];}</span></pre></pre><pre><pre></pre></pre> <span style="color: #000000;"><span style="color: #000000;">Two. AFN Download file<br></span></span><span style="font-size: 14px;"><span style="font-size: 14px;">1. The same simple method call,</span></span> <br><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">1. Create a Session Manager</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span>Afhttpsessionmanager *manager =<span style="color: #000000;"><span style="color: #000000;">[afhttpsessionmanager manager];</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">2. Create a Request object</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span>Nsurlrequest *request = [nsurlrequest Requestwithurl:[nsurl urlwithstring:<span style="color: #800000;"><span style="color: #800000;">@ "add An address to an online photo</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]];</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">3. Create a download task</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #008000;"><span style="color: #008000;">first Parameter: Request Object</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #008000;"><span style="color: #008000;">Second Parameter: Progress callback downloadprogress (get Progress Information)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #008000;"><span style="color: #008000;">Third Parameter: Destination</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008000;"><span style="color: #008000;">targetpath: temporary storage path for files</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">response: response header information</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">return value: nsurl (the process of file clipping has been implemented within afn, but we need to tell him where to cut the File)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">Fourth Parameter: Completionhandler call when the request is complete</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">response: response header information</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">Filep The final directory of the Ath==fullpath file (the return value of the third parameter Callback)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">error: Wrong message</span></span><span style="color: #000000;"><span style="color: #000000;"><br></span></span></pre><pre><pre>[manager downloadtaskwithrequest:<span style="color: #ff9900;"><# (nonnull nsurlrequest *) #></span><br>Progress:<span style="color: #ff9900;"><#^ (nsprogress * _nonnull downloadprogress) downloadprogressblock#></span><br>Destination:<span style="color: #ff9900;"><#^nsurl * _nonnull (nsurl * _nonnull targetpath, nsurlresponse * _nonnull Response) destination#>< /c2></span><br>Completionhandler:<span style="color: #ff9900;"><#^ (nsurlresponse * _nonnull response, nsurl * _nullable filePath, nserror * _nullable error) com pletionhandler#>]</span></pre></pre><span style="color: #ff0000; font-size: 15px;"><span style="color: #ff0000; font-size: 15px;"> <span style="color: #ff0000; font-size: 15px;">Note here that <span style="color: #000000;">we can listen to the download progress in the second parameter callback <span style="color: #ff0000;"> <span style="color: #000000;">( <span style="color: #999999;">callback return parameter</span> </span> </span> </span> <span style="color: #000000;"> <span style="color: #999999;">downloadprogress parameters </span>)<br></span></span></span></span><pre><pre>Downloadprogress.completedunitcount/downloadprogress.totalunitcount)</pre></pre><p><p><span style="color: #993300; font-size: 14px;">and the third parameter callback is to have the return value, the return value is the final storage location of the file, as mentioned above, the cutting process is implemented internally, you only need to return an address, the general practice, in the parameter response has a suggested file name attribute ( suggestedfilename), we will splice it into the sandbox path to get a storage file address, the specific code is as follows</span></p></p><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;"> Sandbox path</span> NSString *path =<span style="color: #000000;"> [nssearchpathfordirectoriesindomains (nscachesdirectory, nsuserdomainmask, YES) lastObject];</span> <span style="color: #008000;">//</span> <span style="color: #008000;"> Stitching full path</span> NSString *fullpath =<span style="color: #000000;"> [path stringByAppendingPathComponent:response.suggestedFilename]; </span> <span style="color: #0000ff;">return</span> <span style="color: #000000;">fullPath; </span></pre></pre>Three. Uploading Files<p><p>the <span style="font-size: 14px;">basic usage is similar to download, the response parameters are the same, only the third parameter needs our attention, the following code is marked red</span></p></p><pre> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">1. Create a Session Manager</span></span>Afhttpsessionmanager *manager =<span style="color: #000000;"><span style="color: #000000;">[afhttpsessionmanager manager]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">2. Send a POST request to upload a file</span></span> <span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">first Parameter: request path NSString second Parameter: non-file parameter the third parameter: Constructingbodywithblock used to splice the file data to be uploaded the fourth parameter: progress progress callback the first Five parameters: Success upload successful callback sixth Parameter: failure failed callback</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #000000;"><span style="color: #000000;">[manager POST:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Http://120.25.226.232:32433/upload</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>Parameters:nil<br>Constructingbodywithblock:<strong><strong><span style="color: #ff0000;">^ (id<afmultipartformdata></span></strong></strong><span style="color: #000000;"><span style="color: #000000;"><strong> <span style="color: #ff0000;">_nonnull FormData)</span> {</strong></span></span><span style="color: #000000;"><span style="color: #000000;"><span style="color: #000000;"> <span style="color: #008000;">//<span style="color: #008000;">FileURL: URL Path of the file to be uploaded</span> </span> </span> </span></span><span style="color: #000000;"><span style="color: #000000;">Nsurl</span></span>*fileurl = [nsurl Fileurlwithpath:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">/users/desktop/1233.png</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">];</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">The method is a simple version, the method will automatically get the name of the file and the type of the file (the method inside name for the server to store the file names)</span></span>[formData Appendpartwithfileurl:fileurl name:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">file</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">error:nil]; } progress:</span></span><span style="color: #ff9900;"><span style="color: #ff9900;">^ (nsprogress *</span></span><span style="color: #000000;"><span style="color: #000000;"> <span style="color: #ff9900;">_nonnull Uploadprogress)</span> {upload Progress</span></span><span style="color: #000000;"><span style="color: #000000;">} success:</span></span><span style="color: #ff9900;"><span style="color: #ff9900;">^ (nsurlsessiondatatask * _nonnull task, ID</span></span><span style="color: #000000;"><span style="color: #000000;"> <span style="color: #ff9900;">_nullable Responseobject)</span> {NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@ "success---</span></span><span style="color: #800000;"><span style="color: #800000;">%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, responseobject); } failure:</span></span><span style="color: #ff9900;"><span style="color: #ff9900;">^ (nsurlsessiondatatask * _nullable task, nserror *</span></span><span style="color: #000000;"><span style="color: #000000;"> <span style="color: #ff9900;">_nonnull Error)</span> {NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">failure---%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, error); }];}<br><span style="font-size: 15px;">First introduce these, AFN there are many practical methods, download pictures, analytical data, etc., very convenient and practical time to write some more.</span><br></span></span></pre><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Ios-three-party Framework afnetworking Basic use</p></p></span>

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.