Keith W
Keith W

Reputation: 71

How do I design and implement password authentication for IOS?

I have what seems to be a simple issue but I can't find a solution. I have spent hours trying to find an example that shows how to build a view, allow a user to enter a password, authenticate the password and return the view to a designated view. I have found a lot a great resources on bits and pieces, but they all are somewhat vague on how to authenticate the password and then send the user to a designated view.

This seems to be the best solution to my problem, which is: I have several pdfs that I need to password protect in my iPad app.

Upvotes: 7

Views: 6160

Answers (4)

Jose Cherian
Jose Cherian

Reputation: 7727

Issue1: Prompt for userName and Password

Present a view modally. if username/password combination is correct, dismiss the modal view. alternatively, you can use an alert with textfields.

Issue2: Store username/password in a secure way

Use key chain as suggested in the other answer. USage of key chain is as simple as using NSUserDefaults with Carlbrown's PDKeychainBindingsController. You can find it at the below link

https://github.com/carlbrown/PDKeychainBindingsController

EDITED to add info requested in comment:

Assuming you are using a custom view controller for login prompt, you have to do something like this when you want to prompt for password. it can be in your application didFinishLaunchingWithOptions.

LoginViewController *controller = [LoginViewController alloc];
            [self presentModalViewController:controller animated:YES];
                        [controller release];

Then in your LoginViewController, you have to do something like this.

  PDKeychainBindings *keyChain =[PDKeychainBindings sharedKeychainBindings];
            if ([[keyChain objectForKey:@"User Name"] isEqualToString:userName] && [[keyChain objectForKey:@"Password"] isEqualToString:passWord] ) {
                [self dismissModalViewControllerAnimated:YES];
            }else {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:@"Wrong User Name and/or Password\nEnter Login Information Again"
                                                               delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
                [alert show];
                [alert release];
                  }

Please note that the strings userName and PassWord are captured from your textfields in login view controller.

Upvotes: 9

Nik Burns
Nik Burns

Reputation: 3423

Oliver over at Cocoanetics.com has a very nice implementation of the security login screen apple uses. Look at the PinLockController class in MyAppSales.

https://github.com/Cocoanetics/MyAppSales

For keychain storage I use SFHFKeychainUtils. Its a simple method call to store and retrieve secure passwords. But I think standard NSUserDefaults would be sufficient for your needs.

(handy site http://gorgando.com/blog/tag/sfhfkeychainutils)

Upvotes: 1

However, if you want to sell that app, keep in mind, that apple says

The user can set a four-digit personal identification number (PIN) to prevent unauthorized use of the device, so there is no need for the user to be authenticated and there are no authentication or authorization APIs in iPhone OS.

This can be found in the Security Coding How-To's

Upvotes: 0

Ben Mosher
Ben Mosher

Reputation: 13381

In anticipation of your answer to my comment--

If you want the password to be stored locally, I would store it (hashed or otherwise) in the Keychain, to compare against the user-entered password. More in the Apple docs here.

I'd recommend downloading the Apple sample project for an example of storing/retrieving data from the keychain.

Upvotes: 4

Related Questions