Afnetworking of iOS Web development

Source: Internet
Author: User

Overview

Afnetworking is a very popular lightweight iOS, MAC OS x network communication class library. Based on Nsurlconnection, Nsoperation and its technology, it has a well-designed modular structure and rich API, making it easy to implement many of the network communication functions.

Afnetworking supports HTTP requests and rest-based network services (including GET, POST, put, delete, and so on). Support Arc. Some column unit tests are also included in the Afnetworking project.

Requires iOS 5.0 and later, or Mac OS 10.7 and later.

Source Address: Https://github.com/AFNetworking/AFNetworking

Practical use

In the source code, has been introduced very clearly, below, for everyone to put out part of the commonly used.

Get method Please ask

No parameter mode:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager GET: @"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {      NSLog( @"JSON: %@" , responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) {      NSLog( @"Error: %@" , error); }];

 

There are parametric ways, in fact, the same as no argument:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSMutableDictionary *params = [ NSMutableDictionary dictionary]; params[@ "param1" ] = @ "1" ; params[@ "param2" ] = @ "2" ; [manager GET:@ "http://example.com/resources.json" parameters:params success:^(AFHTTPRequestOperation *operation,  id responseObject) {      NSLog (@ "JSON: %@" , responseObject); } failure:^(AFHTTPRequestOperation *operation,  NSError *error) {      NSLog (@ "Error: %@" , error); }];

Post Request mode:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

NSDictionary *parameters = @{@ "foo" : @ "bar" }; [manager POST:@ "http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation,  id responseObject) {      NSLog (@ "JSON: %@" , responseObject); } failure:^(AFHTTPRequestOperation *operation,  NSError *error) {      NSLog (@ "Error: %@" , error); }];

Also note:

When we request the network, we often see the return data, there is header information, such as:

Content-type:application/json

Afnetworking The default accepted data type is (under Afjsonresponseserializer):

self .acceptableContentTypes = [ NSSet setWithObjects:@ "application/json" , @ "text/json" , @ "text/javascript" nil ];

If the number of classes returned is Text/plain, an error will occur.

You can add this type directly:

self .acceptableContentTypes = [ NSSet setWithObjects:@ "text/plain" , @ "application/json" , @ "text/json" , @ "text/javascript" nil ];

Afnetworking of iOS Web development

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.