Thursday, December 3, 2015

Introduction AFNetworking Tutorial for JSON

Working with NSURLSession: AFNetworking 2.0

AFNetworking is one of the most widely used open source projects for iOS and OS X development. It powers thousands of popular and critically acclaimed apps, and serves as the foundation for dozens of other great open source libraries and frameworks. With thousands of stars and forks, and hundreds of contributors, the project is also among the most active and influential in the community.

in this tutorial we've taken a close look at theNSURLSession API introduced in iOS 7 and OS X Mavericks. Networking on iOS and OS X has become much simpler and more flexible thanks to the NSURLSessionAPI. Does this mean that you should stop using AFNetworking for your networking needs? And what about AFNetworking 2.0, which was introduced a few months ago? In this final installment, I will tell you about AFNetworking 2.0 and how it compares to the NSURLSession API.


Download AFNetworking Library and try out the included Mac and iPhone example apps


Below display method is used to call post method and getting that response


-(void)postDataOnWebservice
{
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]init];
    
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"application/json"];
    
    NSDictionary *params = @{@"action":@"getDeals"};
    
    NSString *completeURL= [NSString stringWithFormat:@"http://demo.magespider.com/MPSocial/webservice/deal.php"];
    
    [manager POST:completeURL parameters:params success:^(NSURLSessionDataTask *task, id responseObject)
     {
         NSLog(@"Response%@",responseObject);
         
         if ([[NSString stringWithFormat:@"%@",[responseObject valueForKey:@"status"]] isEqualToString:@"1"])
         {
             NSDictionary *data = (NSDictionary *)responseObject;
             NSLog(@"responce%@",data);
         }
         else
         {
             NSLog(@"Not responce");
          }
     }
          failure:^(NSURLSessionDataTask *task, NSError *error)
     {
         NSLog(@"Failure Block");

     }];


}

No comments:

Post a Comment