Skip to content

Easy authentication for iOS apps

joeldev edited this page Mar 15, 2013 · 5 revisions

So you want to get started with a new App.net project on iOS? Lucky you, iOS developer! ADNKit works hard to make getting past authentication on iOS very very quick. There are two approaches, one for OAuth authentication (which you really should not be using for native apps), and one for username/password authentication.

These features are provided in the form of auth view controllers that you can simply create and present, and they will let you know when the entire authentication process is finished.

NOTE: These controllers are not meant to be polished enough for production. These are specifically designed for getting past auth very quickly at the beginning of your project. Think of them as scaffolding. No need to write any auth code!

[More information on how these controllers work.] (https://github.com/joeldev/ADNKit/wiki/Authentication-basics)

ANKUsernamePasswordAuthViewController

Use this controller when you are using username/password authentication. The controller displays username and password fields, and a log in button. It handles end-to-end authentication. The core method is:

- (id)initWithClient:(ANKClient *)client
            clientID:(NSString *)clientID
 passwordGrantSecret:(NSString *)passwordGrantSecret
          authScopes:(ANKAuthScope)authScopes
          completion:(void (^)(ANKClient *authedClient, NSError *error, ANKUsernamePasswordAuthViewController *controller))completionHandler;

Call this method with your auth details, an ANKClient instance you want to auth, and a completion block for when auth completes (or fails). There is nothing else you have to do to get working auth. Show this controller if you have no persisted access token(s), and don't show it if you do.

ANKOAuthViewController

This controller is a thin wrapper around a UIWebView that handles displaying the web OAuth form.

Using this controller is a bit more complex than the username password controller simply because OAuth is two step instead of a single step, and because of the reliance on a redirect URI you will need to catch that redirect in your app delegate and throw control back to this controller after you have parsed out the code. [More information about this process can be found here.] (https://github.com/joeldev/ADNKit/wiki/Authentication-basics)

First, we need to generate the web auth request using ANKClient:

- (NSURLRequest *)webAuthRequestForClientID:(NSString *)clientID
                                redirectURI:(NSString *)redirectURI
                                 authScopes:(ANKAuthScope)authScopes 
                                      state:(NSString *)state
                          appStoreCompliant:(BOOL)shouldBeAppStoreCompliant;

Next, initialize and show one of these (and wait for your redirect URI to be called and for control to transfer over to app delegate):

- (id)initWithWebAuthRequest:(NSURLRequest *)request 
                      client:(ANKClient *)client
                  completion:(void (^)(ANKClient *authedClient, NSError *error, ANKOAuthViewController *controller))completionHander;

Once you have an access code and are ready for part two of the auth, call this method:

- (void)authenticateWebAuthAccessCode:(NSString *)accessCode
                          forClientID:(NSString *)clientID
                         clientSecret:(NSString *)clientSecret;

Once your completion block is called, you are done (or there was a error with the final step of authentication).