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 classHRHttpBase
Two 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 useCocoaPods
Third-party library management. If the project is released, the colleague updatesCocoaPods
The 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 requestsheader
Anduser-agent
All 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.html
FormatString
, To interceptSting
Orkey
. For formats that cannot be processed,afn
Possibleerror
Callback.
With AFHTTPSessionManager, I did not find any unparsed characters (you can tell me if you know it, thank you ). Finally, AFHTTPRequestOperationManager is used.
operation.responseString
It is an unformatted character.
Paste_Image.png