Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Implementing AFNetworking

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.27k
    Comment on it

    AFNetworking is currently the most popular 3rd party networking library for iOS and OSX. The latest version of AFNetworking (2.0) is built on top of NSURLSession which is the new preferred method of networking introduced in iOS 7 (in place of older NSURLConnection API).
    This means AFNetworking library is up-to-date for use with latest iOS. In addition it provides other facilities like serialization, reachablility support, UIKit integration etc.
    To implement AFNetworking follow following steps:

    1. first download the library from [gitbub][1].
    2. Open the zip folder and add AFNetworking and UIKit+AFNetworking folders to your project.
    3. Import "AFNetworking.h" in the prefix.pch file of your project.


    Following code shows how to use it for making a request (with respone in JSON format) :

    -(void)getJSON 
    {
        NSURL *url = [NSURL URLWithString:]; // 1
        NSURLRequest *request = [NSURLRequest requestWithURL:url]; // 2
    
        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request]; // 3 
        operation.responseSerializer = [AFJSONResponseSerializer serializer]; // 4
    
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"JSON Response fetched : %@",responseObject);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error : %@",error);
        }]; // 5
    
        [operation start]; // 6
    }
    
    Here is explanation of above code:
    1&2 : Create an NSURLRequest object.
    3 : Create AFHTTPRequestOperation object (which will be used to make request).
    4 : Set response serializer. This feature of AFNetworking parses the response for us whether it be JSON or plist or XML.
    operation.responseSerializer = [AFPropertyListRequestSerializer serializer]; // for plist
    OR
    operation.responseSerializer = [AFXMLParserResponseSerializer serializer]; // for xml
    NOTE: For xml response we will have to parse xml response ourself using NSXMLParser.
    5 : Set completion block for the operation. The block is where we get the response after the request operation completes.
    6 : Start the request operation.

    We can also use AFHTTPSessionManager instead of AFHTTPRequestOperation in which we can set a base URL and then make several requests to the same endpoint. It can also handle multipart form requests, encode paramters, monitor for changes in connectivity and more. For iOS version 6 or earlier use AFHTTPRequestOperationManager which uses NSURLConnection internally (which is not available in iOS 7) but has functionality similar to AFHTTPSessionManager.
    Following code shows how to use it :

    NSDictionary *parameters;
    // set values in parameters dictionary
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithBaseURL:baseUrl]; // 1
    manager.responseSerializer = [AFJSONResponseSerializer serializer]; // 2
    NSString *serverApi = @"testapi.php"; 
    [manager POST:serverApi parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
                NSLog(@"POST response %@",responseObject);
            } failure:^(NSURLSessionDataTask *task, NSError *error) {
                NSLog(@"error %@",error);
            }]; // 3
    


    1 : Create object of AFHTTPSessionManager with base url.
    2 : Set response serializer of required type.
    3 : Make post request with parameters.

    Here is a nice tutorial by [Raywenderlich][1] that shows with interesting and easy to understand example how to use AFNetworking.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: