Network Programming for iOS development-1. Basic Introduction to all development processes of AFNetwork 3.x, iosafnetwork

Source: Internet
Author: User

Network Programming for iOS development-1. Basic Introduction to all development processes of AFNetwork 3.x, iosafnetwork

AFNetwork 3.x in the third-party network request framework has a high reception level:

A significant change in iOS 7 and Mac OS X 10.9 Mavericks is the complete reconstruction of the Foundation URL loading system. Now, the AFN 3.x version completely abandons NSURLConnection and uses NSURLSession.

For the usage of NSURLConnection and NSURLSession, you can read many of my previous blog posts.

Download Method: 1. Search for "AFN" directly on the github official website. Naturally, the popularity is high. 2. Integrate cocoapods into your project. For cocoapods installation and usage, read the following two blog posts: "cococoapods installation (illustrated) OS x 10.11 system and CocoaPods use (illustrated) OS X 10.11 System

Content outline:

  • 1. Basic use of AFN 3.x
  • 2. Download the AFN 3. X file
  • 3. AFN 3. X File Upload
  • 4. AFN 3.x usage tips
  • 5. serialization related to AFN 3.X
  • 6. AFN 3.X network listening (not difficult)

Body:

1. Basic use of AFN 3.x

1-1. steps:

1-1-1. Create a Session Manager
AFHTTPSessionManager is used as the session manager to create
Not AFURLSessionManager, and AFURLSessionManager has no manager Method
1-1-2. You do not need to create the request URL.
First parameter: Request Path (NSString
Previous: http: // 120.25.226.186: 32812/video? Type = JSON
Http: // 120.25.226.186: 32812/login? Username = 520it & pwd = 520it & type = JSON
Protocol header + host address + Interface Name +? + Parameters & Parameters
Currently: http: // 120.25.226.186: 32812/video
Http://120.25.226.186: 32812/login
Protocol header + host address + Interface Name
Second parameter: parameter to be passed (dictionary)
Third parameter: progress nil
Fourth parameter: success: callback for successful and successful requests
Task: request task
ResponseObject: response body information, which has been serialized internally
Fifth parameter: callback for failed failure requests
Task: request task
Error: if it fails, the error value is

1-2. source code Display

    

2. Download the AFN 3. X file

2-1. steps:

First parameter: request object
Second parameter: callback of progress information of progress
DownloadProgress
CompletedUnitCount: size of the downloaded data
TotalUnitCount ": total data size
The third parameter: destination has a returned value.
When a target path (NSURL) is passed, AFN automatically performs the cutting operation.
TargetPath: Temporary path
Response: response Header Information
The fourth parameter is called after the completionHandler download is complete.

2-2. source code Display

      

3. AFN 3. X File Upload

  

  

4. AFN 3.x usage tips

In actual development, the network request of the framework is not directly called in the Controller. For example, if the AFN framework is directly called in a controller of the network request, it may be difficult to maintain in the future. For example, an app project developed by an enterprise may have over a dozen viewcontrollers, and most controllers will need active data, that is to say, if you need to obtain data through the request network, the AFN framework is imported and used in multiple controllers. However, the actual development requirements may allow you to replace the network request framework, I want to replace this AFN framework with the ASI framework. As a result, I can imagine that I am crying as a programmer.


Then, feasible solutions are also commonly used in enterprise development:
Create a tool class between the Controller that requires a network request and the network request framework. The tool class can be a category or a common class, and then encapsulate the network request methods easily, it is best to respond to different situations and different data requests and implement them. When I replace the AFN framework with the ASI framework, the methods of this tool class called in the Controller do not need to be changed at all. All you need to do is to replace AFN with the ASI framework in the tool class. In short, creating a network request tool class allows all controllers to depend only on the underlying frame.

  

5. serialization related to AFN 3.X

5-1. JSON parsing: JSON data is automatically deserialized and returned to the dictionary object through the Response Header (you can print out its type and view the dictionary element value)

You do not need to manually set the resolution method.

  

5-2. XML parsing requires the <NSXMLParserDelegate> Protocol to assist in parsing XML. Of course, you can also use the encapsulated tool class "HYXMLParser" to directly convert XML to model dictionary ".

AFN needs to be told to parse data in XML format.

The NSXMLParser parameter must be used in the GET method.

  

5-3. directly return the server's HTML data format. Remember to set the returned data to Encoding to NSUTF8StringEncoding and then directly output the data.

You need to manually set the AFN resolution mode to not parse, that is, AFHTTPResponseSerializer serializer.

  

5-4. For data in NSData format, such as binary data of images and videos returned on the network.

You need to manually set the AFN resolution mode to not parse, that is, AFHTTPResponseSerializer serializer.

  

6. AFN 3.X network listening (not difficult)

  

 

 

Reprinted with the source: http://www.cnblogs.com/goodboy-heyang/p/5215991.html, which is a result of high respect for labor.

 

Related Article

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.