Created
January 20, 2011 23:21
-
-
Save wjlafrance/788919 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Create an account on a Flak server | |
Uses ivar's for server, serverKey, firstName, lastName, email and password | |
*/ | |
- (BOOL) createAccount | |
{ | |
NSLog(@"Trying to create account.."); | |
// Server Key, First Name, Last Name, Email, Password, Password | |
static NSString *createUserFormat = @"{ \"key\": \"%@\", \"user\": { \"first_name\": \"%@\", \"last_name\": \"%@\", " | |
"\"email\": \"%@\", \"password\": \"%@\", \"password_confirmation\": \"%@\", \"time_zone\": \"Central Time (US & Canada)\" } }"; | |
NSString *requestString = [NSString stringWithFormat:createUserFormat, serverKey, firstName, lastName, email, password, password]; | |
NSLog(@"User request: %@", requestString); | |
NSURL *url = [NSURL URLWithString:[[self server] stringByAppendingFormat:@"/users"]]; | |
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; | |
[request setHTTPMethod:@"POST"]; | |
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; | |
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; | |
[request setHTTPBody:[requestString dataUsingEncoding:[NSString defaultCStringEncoding]]]; | |
NSHTTPURLResponse *response = nil; | |
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; | |
BOOL success = NO; // pessimism, captain! | |
if ((response != nil) && ([response statusCode] == 200)) { | |
// For some reason, there's no +(NSString*)stringWithBytes | |
NSString *responseString = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding]; | |
NSLog(@"Creating user account: %@", responseString); | |
if ([responseString characterAtIndex:0] == '{') { | |
// Example: { "user": { "id": 1, "email": "[email protected]", "first_name": "Name", | |
// "last_name": "Name", "time_zone": "Central Time (US & Canada)" } } | |
success = YES; | |
} else if ([responseString characterAtIndex:0] == '[') { | |
NSLog(@"Error?: %@", responseString); | |
} /*else { | |
NSLog(@"I don't know what that is!"); | |
}*/ | |
[responseString release]; | |
} | |
return success; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment