Created
April 7, 2017 06:54
-
-
Save randhirraj3130/93d40e99f4743ac2e914600401709ad8 to your computer and use it in GitHub Desktop.
how to send data from server in objective c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void)sendData:(NSString*)email : (NSString*)password :(NSString*)user_id : (NSString*)urlString : (NSString*)name { | |
NSLog(@"sendData"); | |
NSURL *url = [NSURL URLWithString:urlString]; | |
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url]; | |
NSString *dataString = [NSString stringWithFormat:@"email=%@&password=%@&id=%@",email,password,user_id]; | |
NSData *requestData = [dataString dataUsingEncoding:NSUTF8StringEncoding]; | |
NSLog(@"requestData%@",requestData); | |
[urlRequest setHTTPMethod:@"POST"]; | |
[urlRequest setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" forHTTPHeaderField:@"Accept"]; | |
[urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; | |
[urlRequest setHTTPBody: requestData]; | |
NSURLSessionDataTask * dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:urlRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { | |
NSLog(@"data=%@",data); | |
if (data.length>0 && error==nil) { | |
@try { | |
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL]; | |
NSLog(@"Dict=%@",dict); | |
NSString *ss = [NSString stringWithFormat:@"%@",[dict valueForKey:@"result"]]; | |
NSLog(@"ss %@",ss); | |
if([ss isEqualToString:@"success"]){ | |
NSLog(@"success"); | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
[[NSUserDefaults standardUserDefaults]setValue:@"name" forKey:@"name"]; | |
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; | |
UITabBarController*vc = [storyboard instantiateViewControllerWithIdentifier: @"MyTabBarController"]; | |
self.navigationItem.hidesBackButton = YES; | |
self.navigationItem.backBarButtonItem = nil; | |
[[self navigationController]pushViewController:vc animated:YES]; | |
}); | |
}else{ | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
UIAlertController * alert = [UIAlertController | |
alertControllerWithTitle:@"Invalid" | |
message:@"Invalid email or passwor.try Again!" | |
preferredStyle:UIAlertControllerStyleAlert]; | |
UIAlertAction* fbButton = [UIAlertAction | |
actionWithTitle:@"Retry" | |
style:UIAlertActionStyleDefault | |
handler:^(UIAlertAction * action) | |
{ | |
// Add your code | |
}]; | |
[alert addAction:fbButton]; | |
[self presentViewController:alert animated:YES completion:nil]; | |
}); | |
} | |
} @catch (NSException *exception) { | |
NSLog(@"Exception %@",exception.description); | |
} @finally { | |
} | |
} | |
}]; | |
[dataTask resume]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment