Question

Where can I find a good detailed tutorial using sskeychain to store and retrieve usernames and passwords and to do basic authentication in a UIWebView? Secondarily, am I on the right track as far as the methods needed to store and use authentication for a web based application? (See explanation below.)

I found a couple tutorials using different methods:

According to recommendations from other SO questions below, sskeychain is recommended for an easier use of the keychain to store authentication parameters.

My plan is to store a username and password locally on the device in the keychain as recommended and connect over a UIWebView using basic auth to my PHP code. Is there a good step by step tutorial for xcode/Obj-C newbies on the topic of user authentication that would be recommended by experienced iOS developers?

The Apple documentation seems less than helpful. It's either pages with simple sales jargon or just head imploding descriptions of methods and parameters without many helpful examples.

Update:

I ended up just using NSUserDefaults to store the username and password locally and the AFNetworking library to do the authentication. If these are unwise I'd welcome an answer that supplies guidance on a better method.

Was it helpful?

Solution

If you want to do basic auth, you might be better off using a networking service like AFNetworking. They all you to create a webclient, pass in a username/password combination and they take care of the rest.

This link show you the exact method call you need to invoke: http://engineering.gowalla.com/AFNetworking/Classes/AFHTTPClient.html#//api/name/setAuthorizationHeaderWithUsername:password:

There is also a large community and a bunch of examples of developers using AFNetworking in all types of iOS project.

OTHER TIPS

Using AFNetworking to do the authentication and calls to the server is great! But I would highly discourage you from storing credentials (username and password) in NSUserDefaults, since the contents are stored in a plist as plain text and can be read just by plugging your device to a mac. I recommend you to check these other questions and great post of a well-known case for further details on the topic.

You were right going for Keychain and using SSKeychain is easy and fast. You can find this good example on how to use SSKeychain to locally store the credentials.

// Store credentials in Keychain
[SSKeychain setPassword:@"thePassword"
             forService:@"com.yourCompany.yourApp"
                account:@"theUserName"];

// Retrieve credentials from Keychain
NSString *password = [SSKeychain passwordForService:@"com.yourCompany.yourApp"
                                            account:@"theUserName"];

I would recommend to you to use

Lockbox

It's lightweight, works with ARC and easy to use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top