Reputation: 17589
I want to create an iphone app where the back-end will be supported through rails (UI-less/JSON) web service. As with other web sites out there, one of the functionalities that I will have is logging in and logging out. Right now, I am thinking about how I should architect this login functionality on iPhone and what should my rails web service do?
Anyone has any pointers on how to do this? This is what I have thought of so far.
Since my app will be the only one using this web service, I will generate a key that will be passed in for each web service call to make sure that nobody else can use this web service (no other app can use my logging in and logging out functionality).
When a user calls the login ws, I will check to make sure that the user's login creds are valid and when they are, I will pass them a token that they will keep (how should I keep the token? I don't think iPhone has session) and they will have to call another web service passing the token/email/pass to signal the ws that the client has gotten the key.
For each restricted ws call, the user will need to pass the token to call the service.
When the user log out, the token will be invalidated.
Basically I just copied OAuth mechanism or something like that. My biggest question is how can I keep the token around on the client's end (iPhone).
Upvotes: 3
Views: 134
Reputation: 2354
You can use NSUserDefaults to store the token, you set a key and store and retrieve against that key.
When the user logs out you can just clear the NSUserDefault for that key.
-(void)saveToUserDefaults:(NSString*)myString
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) {
[standardUserDefaults setObject:myString forKey:@"LoginAuthToken"];
[standardUserDefaults synchronize];
}
}
-(NSString*)retrieveFromUserDefaults
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *val = nil;
if (standardUserDefaults)
val = [standardUserDefaults objectForKey:@"LoginAuthToken"];
return val;
}
References;
http://www.cocoadev.com/index.pl?NSUserDefaults
If you want to store the username and password for your web service, you should use ios Keychain access to keep it secure.
Reference;
Upvotes: 1