Monday, July 14, 2014

AFNetwork Tutorial Part 1

AFNetwork Tutorial Part 1

01. Login 



- (void)callSigninWebService:(id)sender {

        // Initialize JSON POST
        NSURL *wsUrl = [NSURL URLWithString:@"put ur URL "];
        AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:wsUrl];
       
        // Show in the status bar that network activity is starting
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
       
        // User Signup parameter definitions
        NSDictionary *dcParams = [NSDictionary dictionaryWithObjectsAndKeys:
                                  @"adrian@gmail.com", @"Email",
                                  @"123456", @"Password",
                                 
                                  nil];
       
        NSURLRequest *afRequest = [httpClient requestWithMethod:@"POST" path:@"put ur URL " parameters:dcParams];
        
       
       
        AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:afRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
                                             {
                                       
                                              NSLog(@"Successful");
                   
                                            
                                             }
                        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
                                             {
                                              
                                                 NSLog(@"Unsuccessful");
                                              }];
                                                
    [operation start];
   
}

02. Upload image with token

-       -(void)uploadImageWebService:(id)sender {

    UIImage *rainyImage =[UIImage imageNamed:@"Vburst-Shirt-1.jpg"];
    NSData *imageData = UIImageJPEGRepresentation(rainyImage, 0.2);
   
                
          NSURL *wsUrl = [NSURL URLWithString: :@"put ur URL "];

        AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:wsUrl];
        [httpClient setDefaultHeader:@"authenticationToken" value:self.userTocken];
       
           [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
       
        NSDictionary *objTrip = [NSDictionary dictionaryWithObjectsAndKeys:
                                
                                                @"Adrian", @"NAME",
                                                @"Album of Trip", @"DETAILS",

                                                nil];
       
       

       
      
        
        NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path :@"put ur URL " parameters:objTrip constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
            [formData appendPartWithFileData:imageData name:@"imgRecipt" fileName:@"anyName.png" mimeType:@"image/jpeg"];
        }];
       
        NSURLRequest *afRequest = [httpClient requestWithMethod:@"POST" path:@"put ur URL " parameters:objTrip];
       
        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
       
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
           
              NSLog(@"Successful");

           
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
           
               NSLog(@"UnSuccessful");
        }];

        [operation start];
       
            
    }
   
}





No comments:

Post a Comment