FAQs about the use of AFNetworking 3.0 in iOS development

Source: Internet
Author: User

FAQs about the use of AFNetworking 3.0 in iOS development
1. Many beginners directly request requests from afn instances where network requests are used, which makes it difficult to maintain code in the future.

    [[AFHTTPSessionManager manager] POST:nil parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {    } failure:^(NSURLSessionDataTask *task, NSError *error) {    }];

Thinking: If afn is not updated, a major bug adaptation problem will occur in the future, which will cause changes to every place where afn is used. What should I do?
Answer: You can only modify the settings one by one! Some people may say, how can we not update it? Many people are using it for fear. What if other third parties have the same type of problems? So should there be methods or ideas to solve it?

Solution: Write a network base classHRHttpBaseTwo methods are provided: GET and POST. When an external request is sent, the two methods are called directly. In case of a problem with afn, you only need to modify the two methods to reduce the subsequent maintenance cost! This method also applies to other third-party libraries, which is also a development idea.

HRHttpBase.h


HRHttpBase.m


If the business is complex, such as logon, involving third-party logon, and many other network requests, you can select a network request class to process it:

HRhttpLogin.h


HRhttpLogin.m


2. CocoaPods is used, but the afn code is changed.

This is because the text/html parsing format is missing. When requesting the network, you only need to add it. Many people find the setting on the Internet (for example ). But he forgot to useCocoaPodsThird-party library management. If the project is released, the colleague updatesCocoaPodsThe code written earlier is gone!


Solution: Find the AFHTTPResponseSerializer object through the AFHTTPSessionManager instance, and check whether the corresponding method or attribute is provided to set the format.


Similar network requestsheaderAnduser-agentAll should be set in your own code!

3. The returned string is not in json or xml format. How can this problem be parsed?

There is indeed a strange demand in development, and a bunch of results may be returned.htmlFormatString, To interceptStingOrkey. For formats that cannot be processed,afnPossibleerrorCallback.

With AFHTTPSessionManager, I did not find any unparsed characters (you can tell me if you know it, thank you ). Finally, AFHTTPRequestOperationManager is used.

operation.responseStringIt is an unformatted character.


Paste_Image.png

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.