Blog

OAuth support in iOS

One of the hardest things to find in iOS is support for authentication methods like OAuth. While there are many solutions one of the best in my opinion is the RestKit framework. Why? For three reasons:
  1. The ability to call restful webservices using a client which parsers JSON and XML answers directly, through SSL or HTTP Auth.
  2. Core Data support, which means you can locally persist the data received.
  3. Domain Data Object support is another killer feature. In other words, you can map JSON or XML answers to domain objects.
While this sounds like the perfect solution, is more often see API using OAuth as an authentication method and this framework unfortunately did not have support.
… until now!
The solution? I’m not about to complain about the lack fo support so worked on a fix which then I was able to contribute the code back to the project. I forked their repository and added the support. While I am waiting the patch’s acceptance, you can use the code available in my personal repository.
  1. Grab the framework from my fork, and configure everything according the installation instructions.
  2. Use it in your controllers or delegates, next there is an example.
    1. OAuth 1 support:[c language=“++”]
      RKObjectManager* objectManager = [RKObjectManager sharedManager];
      objectManager.client.baseURL = @“https://api.twitter.com”;
      objectManager.client.consumerKey = @“YOUR CONSUMER KEY HERE”;
      objectManager.client.consumerSecret = @“YOUR CONSUMER SECRET HERE”;
      objectManager.client.accessToken = @“YOUR ACCESS TOKEN HERE”;
      objectManager.client.accessTokenSecret = @“YOUR ACCESS TOKEN SECRET HERE”;
      objectManager.client.forceOAuthUse = YES;
      [/c]
    2. OAuth 2 support:
      [c language=“++”]
      RKObjectManager* objectManager = [RKObjectManager sharedManager];
      objectManager.client.baseURL = @“YOUR API URL”;
      objectManager.client.oAuth2AccessToken = @“YOUR ACCESS TOKEN”;
      objectManager.client.forceOAuth2Use = YES;
      [/c]

As you see, I still need to develop the code for the handshaking process, but in the meantime you can start to consume webservices and enjoy the domain data support of this framework. Comments and improvements to the code are always welcome!