sandeep tomar sandeep tomar - 4 months ago 7x
Objective-C Question

How to add login api using nsjsonserilization.?

I have a api like this and want to fire this api on login button click. I need to match email and password.

- (void)viewDidLoad {
[super viewDidLoad];
name = [NSMutableArray new];

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL *url = [NSURL URLWithString:@""];
NSURLRequest *request= [NSURLRequest requestWithURL:url];
[NSURLConnection connectionWithRequest:request delegate:self];


- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];

-(void)connection:(NSURLConnection *)connection didReceiveResponse: (nonnull NSURLResponse *)response {
data = [[NSMutableData alloc] init];
NSLog(@"Did receive response");

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theta {
[data appendData:thedata];

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
name= [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];


-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
UIAlertView *errorview= [[UIAlertView alloc]initWithTitle:@"Error" message:@"The download could not complete please make sure you're connected to internet" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles: nil];
[errorview show];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

- (IBAction)login:(id)sender {




add the following details in your .plist



- (IBAction)login:(id)sender    {

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@""]];

NSURLSession *session = [NSURLSession sharedSession];
//  email": user_name, "password": password, "deviceToken":'testdata
NSString * params =[NSString stringWithFormat:@"email=%@&password=%@&deviceToken=testdata",yourEmailtextfield.text,yourpasswordtextfield.text];

request.HTTPMethod = @"POST";
request.HTTPBody =[params dataUsingEncoding:NSUTF8StringEncoding];

NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                              ^(NSData *data, NSURLResponse *response, NSError *error) {
                                  // ...
                                   NSLog(@"Response:%@ %@\n", response, error);
                                  if(error == nil)
                                      // use NSJSON Serlizeitaion and serlize your value
                                      NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
                                      NSLog(@"Data = %@",text);

[task resume];


for additional information see this tutorial