How to post data using AFNetwork in iphone?
- First insert the below Classes in your project.
-Reachability.h and .m
-AFHTTPClient.h and .m
-AFHTTPRequestOperation.h and .m
-AFImageRequestOperation.h and .m
-AFJSONRequestOperation.h and .m
-AFJSONUtilities.h and .m
-AFNetworkActivityIndicatorManager.h and .m
-AFNetworking.h
-AFPropertyListRequestOperation.h and .m
-AFURLConnectionOperation.h and .m
-AFXMLRequestOperation.h and .m
-UIImageView+AFNetworking.h and .m
- Now,import these below classes in your viewcontroller.m file.
-#import “AFHTTPClient.h”
-#import “AFHTTPRequestOperation.h”
-#import “AFJSONRequestOperation.h”
-#import “AFNetworking.h”
- Now, code like below for posting any data.
NSString *strURL = @”anylink
AFHTTPClient *httpClient = [[AFHTTPClient alloc] init];
[httpClient defaultValueForHeader:@”Accept”];
////////////Set object with key for post it on server////////////
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setObject:@”getFavoriteBlogs” forKey:@”command”];
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@”POST” path:strURL parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
}];
//Add your request object to an AFHTTPRequestOperation
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *response = [operation responseString];
NSLog(@”response:%@”,response);
/*
…
…code for response
…
*/
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@”App title” message:@”No Internet Connection Available.” delegate:self cancelButtonTitle:@”Ok” otherButtonTitles:nil, nil];
[alert show];
NSLog(@”error: %@”, [operation error]);
}];
//call start on your request operation
[operation start];