abbreviated iOS development (7): network requesting module

Mainstream APP and ultimately interact with the server, network requests are indispensable thing.

Open source web request library, there are many, such as: AFNetworking, YTKNetwork, PPNetworkHelper, ASIHttpRequest, and so on.

Here the record using AFNetworking.

(1) Installation afnetworking

With the use of QMUIKit as a small way to cocoapods way to introduce AFNetworking.

First with the pod command to find the latest version of AFNetworking:

pod search AFNetworking

Find results are as follows:
Find afnetworking pod version

In Podfile added content on the red box, as follows:
Add afnetworking in the podfile

In the directory where podfile, execute:

pod install

After installation, the project can be seen in the target afnetworking figure, such as:
xcode in afnetworking

(2) afnetworking

For afnetworking, refer to the "official" website: https://github.com/AFNetworking/AFNetworking

The Usage section of the page, detailing afnetworking use (including monitor network status, uploading and downloading, data requests, etc.), but also provides a demo code, but note presented here are likely to keep up afnetworking version (afnetworking has been upgraded).

An example of using a small afn process is such that:
Use afn

Above using the GET method manager, this method is an asynchronous request, in fact, afn requests are asynchronous.

In some scenes, or in some code to achieve synchronization request will be more easy to control, in order to use synchronous request, consider this open source project: https://github.com/paulmelnikow/AFNetworking-Synchronous

Pod can also be used to synchronize the introduction of this afn projects (such as for afnetworking3.x version):

pod 'AFNetworking-Synchronous/3.x'

Then press the project presented, the synchronization request:
Synchronous afn

Finally mention, a request for the data returned json, AFN parsing has been done (typically resolved into dictionary). If you want to deal with json data alone, you can use MJExternsion this open source library, it can be installed:

under 'MJExtension' '~> 3.0.15.1'

Use on, please refer https://github.com/CoderMJLee/MJExtension


keep simple

Guess you like

Origin www.cnblogs.com/freeself/p/11136471.html