Tuesday, December 15, 2015

Webservice Demo Using ASIHTTPRequest Classes in iOS



How to call webService in iOS using ASIHTTPRequest Library


ASIHTTPRequest is third party class. But its needed "libz.dylib" Framework. So First import this framework into your project

---------Webservice Methods for webservice--------------

-(void)postDataOnWebservice{
    
    
    //    NSString *urlString = [NSString stringWithFormat:@"%@%@uemail=%@&upass=%@", MainURL, registerUrl,txtEmailId.text,txtPassword.text];
    
    NSString *urlString = [NSString stringWithFormat:@"%@%@", MainURL, registerApiUrl];
    
    NSLog(@"UrlString:->%@",urlString);
    
    NSURL *url = [NSURL URLWithString:urlString];
    //ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    request.delegate = self;
    [request setRequestMethod:@"POST"];
    [request addRequestHeader:@"Content-Type" value:@"application/json;"];
    //[request addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded;"];
    
    [request setPostValue:txtFirstname.text forKey:@"first_name"];
    [request setPostValue:txtLastname.text forKey:@"last_name"];
    [request setPostValue:txtUsername.text forKey:@"username"];
    [request setPostValue:txtPassword.text forKey:@"password"];
    [request setPostValue:txtEmailId.text forKey:@"email"];
    
    request.tag = registerUrlTag;
    [request setDidFinishSelector:@selector(checkupdateFinished:)];
    [request setDidFailSelector:@selector(checkupdateFailed:)];
    [request startSynchronous];
}



-(void)checkupdateFinished:(ASIFormDataRequest *)request
{
   
     if (request.responseData) {
        
        if (request.tag == registerUrlTag) {
            
            NSError *err;
            NSDictionary *resultDict = [NSJSONSerialization JSONObjectWithData:request.responseData options:kNilOptions error:&err];
            NSLog(@"Result :->%@",resultDict);
        
        }
    }
    [SVProgressHUD dismiss];
}

-(void)checkupdateFailed:(ASIFormDataRequest *)request
{
    [SVProgressHUD dismiss];
    NSString *receivedString = [request responseString];
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Temporary server is down. Please try again." message:receivedString delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alertView show];
}

Calling Webservice in iOS


if (dataManager.isConnectionReachable) {
            [SVProgressHUD showWithStatus:@"Loading.."];
            [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(postDataOnWebserviceForLogin) userInfo:nil repeats:NO];
            // [self postDataOnWebserviceForLogin];
        }else{
            showAlert(@"Check Connection", @"Internet is not available.");
        }

No comments:

Post a Comment