mardi 3 mars 2015

How to use AFNetworking frame work with json Login with my own api i have tokens to pass but i dont now how to use those

How do i use AFNetworking Framework to authenticate and login in to my IPhone application i have my own api and have some tokens like this X-User-Email,X-User-Token. i dont know what i have to do with this tokens somke one help me exactly what i have to do


i am using some code searching many methods but am not getting exactly did i have to do anything more


i have given code like this but i dont know where to give tokens



{
if([[userNameTF text] isEqualToString:@""] || [[passWordTF text] isEqualToString:@""] ) {
[self alertStatus:@"Please enter both Username and Password" :@"Login Failed!"];
} else {
NSString *post =[[NSString alloc] initWithFormat:@"user[email]=%@&user[password]=%@",[userNameTF text],[passWordTF text] ];
NSLog(@"PostData: %@",post);

NSURL *url=[NSURL URLWithString:@"http://ift.tt/17NCfTJ"];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSLog(@"Response code: %d", [response statusCode]);
if ([response statusCode] >=200 && [response statusCode] <300)
{
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);

SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
NSLog(@"%@",jsonData);
NSInteger success = [(NSNumber *) [jsonData objectForKey:@"id"] integerValue];
NSLog(@"%d",success);
if(success == 1)
{
NSLog(@"Login SUCCESS");
[self alertStatus:@"Logged in Successfully." :@"Login Success!"];

} else {

NSString *error_msg = (NSString *) [jsonData objectForKey:@"error_message"];
[self alertStatus:error_msg :@"Login Failed!"];
}

} else {
if (error) NSLog(@"Error: %@", error);
[self alertStatus:@"Connection Failed" :@"Login Failed!"];
}
}
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
[self alertStatus:@"Login Failed." :@"Login Failed!"];
}



Aucun commentaire:

Enregistrer un commentaire