sandeep tomar sandeep tomar - 5 months ago 11
Objective-C Question

How to add login api using nsjsonserilization.?

I have a api like this http://qa.networc.in:1336/api/users/app/login 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:@"http://qa.networc.in:1336/api/users/app/login"];
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];
NSLog(@"daata");
}

-(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 {


}

Answer

Step-1

add the following details in your .plist

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

step-2

- (IBAction)login:(id)sender    {




NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://qa.networc.in:1336/api/users/app/login"]];

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
                                        completionHandler:
                              ^(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

Comments