Skip to content

Instantly share code, notes, and snippets.

@mladjan
Created October 25, 2011 22:56
Show Gist options
  • Save mladjan/1314617 to your computer and use it in GitHub Desktop.
Save mladjan/1314617 to your computer and use it in GitHub Desktop.
Basic authentication
// U header fajlu definises delegate:
<RKRequestDelegate>
// u didFinishLaunching inicijalizujes singletone objekat RKClient i setujes domen za service:
RKClient *client = [RKClient clientWithBaseURL:@"http://taksiko.com"];
// u implementaciji ovako posaljes zahtev:
userParams = [NSDictionary dictionaryWithObjectsAndKeys:[defaults objectForKey:@"login"], @"table_number", [defaults objectForKey:@"password"], @"driver_password", nil];
[RKClient sharedClient].username = @"mladjan";
[RKClient sharedClient].password = @"password";
[RKClient sharedClient].forceBasicAuthentication = YES;
[[RKClient sharedClient] post:@"/api/driver_login.json" params:userParams delegate:self];
// A u delegate metodi primas sve response i ispitujes od kog je request-a (ja imam vise requesta u sigleton klasi
#pragma mark - Responses
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
if([request params] == userParams){
if([[response bodyAsString] isEqualToString:@"ERROR"]){
logedIn = FALSE;
[[NSNotificationCenter defaultCenter] postNotificationName:@"presentLoginView" object:nil];
NSLog(@"Login: %@", [response bodyAsString]);
}else{
NSLog(@"Dobar je mali!");
}
} else if([request params] == stateParams){
[[NSNotificationCenter defaultCenter] postNotificationName:@"removeLoading" object:nil];
if([response statusCode] != 200 || [[response bodyAsString] isEqualToString:@"ERROR"]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oups" message:@"Sorry, but I can't change your state. Try again."
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}else if([response statusCode] == 200){
NSDictionary *responseDictionary = [[response bodyAsString] objectFromJSONString];
currentState = (int)[responseDictionary objectForKey:@"state"];
}
} else if([request params] == locationParams){
if([[response bodyAsString] isEqualToString:@"OK"]){
NSLog(@"Location updated");
}else if([[response bodyAsString] isEqualToString:@"ERROR"]){
NSLog(@"Location not updated!!!!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment