Wednesday, September 17, 2008

Netflix API - Getting OAuth to work on iPhone - part 2: Adding the OAuth Code

To start with I found some examples by Nick Dalton that helped me build a simple application that included a Web View, a screen that acts like a web browser but with my custom Objective-C code embedded in it. This is important, because the OAuth sign-in process uses a web page, but on the iPhone, if you spawn a copy of Safari to visit a web page, your application quits first.

Next the Source Code Manager in Xcode was configured to load the OAuthconsumer Objective-C framework via subversion. This was easy and obvious, enter the URL http://oauth.googlecode.com/svn/code/obj-c/ and checkout the code.

When trying to import the framework I discovered that Apple does not allow user specified binary frameworks to be added to iPhone applications. To work around this the source code was copied from the Xcode project for the framework, to the Xcode project for my Instant Test application. I renamed the  framework Classes folder as OAuth and copied to my project via drag and drop, choosing to copy the underlying files. The Cocoa Categories, Protocols and Other Sources>Crypto folder were also copied. The Tests folder did not compile for iPhone so don't bother to copy it over.

The standard system Security.Framework doesn't need to be added to the Frameworks folder. I initially thought it did, but its probably only needed for the KeyChain code.

The iPhone doesn't support the KeyChain functionality, so if you try to build for iPhone it will fail. It does however build for the iPhone Simulator, which is confusing. Open up the OAuth source code, and delete the last two files OAToken_KeychainExtensions.h and OAToken_KeychainExtensions.m.

Since the code is no longer a framework, the header file references need to be changed from #include to #include "file" for all the includes in OAuthConsumer.h apart from the first Foundation one.

At this point, before you try and call anything, try a build, it should compile with no errors. If it doesn't, look for missing files.

At this point, you should have all you need to connect to an OAuth service

No comments: