iOS Network Connection with AFNetworking – Objective-C


iOS Network Connection with AFNetworking – iOS(Objective-C)

There are many libraries for iOS to connect network and get/post data. This tutorial will show one of the most popular library: AFNetworking. It is easy to implement and gather data with this library in Objective-C.

Assume that you have a data as below and you want to GET it.

{
	"succes": "YES",
	"message": "Connected",
	"returnData": [{
			"field1": "111",
			"filed2": "222",
			"field3": "333"
		},
		{
			"field1": "111",
			"filed2": "222",
			"field3": "333"
		}
	]
}

And this data is belong to “your.domain.com/your/path”. It is optional to divide this path as baseUrl and path like “your.domain.com” and “/your/path” for using baseUrl later. Anyway, we will get whole data and split it as success, message and returnData.

NSURL *baseURL = [NSURL URLWithString:@"http://your.domain.com"];
    NSString *path = @"/your/path";
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
    
    [manager GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        //Network Success
         id JSON = responseObject;
         BOOL success = [[JSON objectForKey:@"success"] boolValue];
         if (success) {
             NSArray * returnData = [JSON objectForKey:@"returnData"];
         }
        
        NSString *message = [[JSON objectForKey:@"message"] stringValue];
        NSLog(@"%@", message);
        
     } failure:^(NSURLSessionDataTask *task, NSError *error) {
         //Network Failure
     }];

returnData array contains all values of field1, field2, field3.

 

 

©Coffee Break Codes – Simple Network Connection with AFNetworking – iOS(Objective-C)


Leave a comment

Your email address will not be published. Required fields are marked *

4 thoughts on “iOS Network Connection with AFNetworking – Objective-C

  • clubfoto

    Привет меня зовут Jaimie Wylly тороплюсь
    поделится счастливым событием я прошел регистрацию 1.
    Вам тоже рекомендую, до встречи.
    Жду всех посетителей блога iOS Network Connection with AFNetworking – Objective-C | Coffee Break Codes .

    Сайт 1

  • situs bola

    I’m really enjoying the theme/design of your web site. Do you ever run into any internet browser compatibility problems?
    A handful of my blog readers have complained about my blog not working correctly in Explorer but looks great in Chrome.
    Do you have any advice to help fix this issue?