Skip to content

Instantly share code, notes, and snippets.

@jameswomack
Created August 15, 2011 23:25
Show Gist options
  • Save jameswomack/1148138 to your computer and use it in GitHub Desktop.
Save jameswomack/1148138 to your computer and use it in GitHub Desktop.
Example Login Method
/*
This is an example of sending a POST request to a server that returns a JSON object containing a user ID, API key, etc. isSnagazineReachable is a method for checking if the server is available first. The main work is done by creating an NSDictionary with the data you want to post, passing that to the CCWebUtils class method postToUrl:params: which returns an NSString of the server response.
*/
- (BOOL)authWithEmail:(NSString *)theEmail andPassword:(NSString *)thePassword; {
if (![[CCSnagazineDataSource sharedData] isSnagazineReachable]) {
Alert(0,nil,kNetworkDown,@"Ok",nil);
return FALSE;
}
if (![self userId] || ![self apiKey]) {
NSString *urlString = @"http://snagazine.com/api/login/";
NSDictionary *theData = [NSDictionary dictionaryWithObjectsAndKeys:
theEmail,@"email",
thePassword,@"password",
nil];
NSString *s = [CCWebUtils postToUrl:urlString params:theData];
if ([s isEqualToString:@"false"]) {
return FALSE;
}else {
[[NSUserDefaults standardUserDefaults] setObject:[[self jsonObjectWithJSONString:s] objectForKey:@"id"] forKey:@"userId" ];
[[NSUserDefaults standardUserDefaults] setObject:[[self jsonObjectWithJSONString:s] objectForKey:@"key"] forKey:@"apiKey" ];
[[NSUserDefaults standardUserDefaults] setObject:[theData objectForKey:@"email"] forKey:@"email" ];
[[NSUserDefaults standardUserDefaults] setObject:[theData objectForKey:@"password"] forKey:@"password" ];
}
[[NSUserDefaults standardUserDefaults] synchronize];
}
return TRUE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment