On iPhone, how do I show a login screen to get username and password before giving access to iPhone app? Also, does the iPhone store a cookie to the secure website like a web browser?
I was thinking of giving users to my website a long API key to store in the settings of their iPhone instead of asking them to login with a username/password (seems to be the Slicehost iPhone app approach.) Which is the best way to get a user to login securely? I have full control over the design of the iPhone app and website so have a lot of flexibility.
I have this need in a couple of apps, and I use the following pattern:
In the controller for the first view that gets presented to the user, I have the following test, where UserPrefsManager is a singleton that knows the user credentials. This call causes a modal view to appear (
FirstTimeWelcomeViewController) which tells the person that they need to register.FirstTimeWelcomeViewController is basically a screen with buttons that greets people and takes them to the various ways to log in:
imagine that these also exist for forgotPassword, and loginACcount… same pattern. This causes the current view to be replaced by a view handling the specific case that they’ve pressed the button for.
taking the ‘loginAccount’ method, you’ve opened the LoginAccountViewController, and it has a method called loginButton, which works something like this…
-(IBAction) loginButton { NSString * u = [self.username text]; NSString * p = [self.password text]; // // app specific logic that tests various inputs and creates a user object. // // goes here... // if([user checkValid]) { UserPrefsManager * prefs = [UserPrefsManager sharedInstance]; [prefs setPassword:p]; [prefs setUsername:u]; [self dismissModalViewControllerAnimated:FALSE]; } // // more app specific stuff // }And that’s pretty much that. You have to use one of the standard ways for putting stuff in the keychain or user defaults to save your information. check that the one you pick lasts between restores if it is something that is annoying for the user to recreate. The first part is the most useful bit, thought the rest might be useful for context.