Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use AFNetworking to make a server call in iOS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 986
    Comment on it


    Hi Reader’s,


    This blog is to use  AFNetworking to make a server call with the help of API. Here we are using an API which includes GIF images and other related data. So below is the code used to get response from server. Before proceeding towards the code we need to install pods and in podfile we need to include given below lines:-


     

    platform :ios, '9.0'
    pod 'AFNetworking', '~> 3.0'
    pod 'MBProgressHUD', '~> 0.9.2'

     


    After writing these lines in pod file install the pod file with the help of terminal. Now given below is the code to hit at server:-


     

    ViewController.h
    
    #import <UIKit/UIKit.h>
    #import <AFNetworking/AFNetworking.h>
    #import <MBProgressHUD/MBProgressHUD.h>
    @interface ViewController : UIViewController
    
    
    @end
    
    
    ViewController.m
    
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self hitAtServerMethod];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    -(void)hitAtServerMethod
    {
        NSString *string = [NSString stringWithFormat:@"http://api.giphy.com/v1/gifs/trending?api_key=dc6zaTOxFJmzC"];
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];//for server hit
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", nil];
        [manager GET:string parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
            
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            NSLog(@"response %@",responseObject);
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        }
         
             failure:^(NSURLSessionDataTask  *_Nullable task, NSError  *_Nonnull error) {
                 [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                 NSLog(@"request failed %@",error);
             }];
        
    }
    @end


        


    After writing the above mentioned code and execute the application some error we will get instead of response and that error will be in given format:-

     

     

     

     

     


    To remove this error we need to include given below lines in info.plist file and than again execute the application we will get the output.

     

     

    <key>NSAppTransportSecurity</key>
        <dict>
            <key>NSAllowsArbitraryLoads</key>
            <true/>
        </dict>

     


    Output:-

     

    Response we will print in NSLog that’s why we will get the result in given below way:-

     

     

     

     

    You can also download the sample project from the link given below:-

 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: