Cocoaheads Berlin
March 12th 2009
About implementing an API wrapper for a OAuth protected Web API in Objective-C for Mac and iPhone.
http://github.com/soundcloud/cocoa-api-wrapper
Presentation on how to chat with PDF using ChatGPT code interpreter
SoundCloud Cocoa API Wrapper
1. The SoundCloud API
Cocoa Wrapper
How to build a connection framework for an
OAuth protected RESTful Web-API
Wednesday, March 18, 2009
2. Agenda
1.Introduction
The SoundCloud API
2.Challenges
Problems we solved
3.Try it!
Integrating SoundCloud sharing to your app
Wednesday, March 18, 2009
4. The SoundCloud API
• RESTful
Resource based
•
User, Track, Comment, ...
•
HTTP based
•
GET, PUT, POST, DELETE
•
Data representation in JSON, XML
•
Wednesday, March 18, 2009
5. The SoundCloud API
• RESTful
Resource based
•
User, Track, Comment, ...
•
HTTP based
•
GET, PUT, POST, DELETE
•
Data representation in JSON, XML
•
• OAuth protected
Wednesday, March 18, 2009
6. REST Example
• Resource: Tracks
Resource Method
/tracks GET, POST
/tracks/{track_id} GET, PUT, DELETE
• Creating a new Track
POST /tracks
• curl -u joey:hairspray
'http://api.sandbox-soundcloud.com/tracks'
-F track[asset_data]=@step_by_step.aif
-F track[title]=Superstitious
Response
• < HTTP/1.1 201 Created
< Location: http://api.sandbox-soundcloud.com/tracks/13
Wednesday, March 18, 2009
8. OAuth
• Token based API authentication + authorization
Exchange tokens rather than login & password
•
Authorization on server side
•
Wednesday, March 18, 2009
9. OAuth
• Token based API authentication + authorization
Exchange tokens rather than login & password
•
Authorization on server side
•
• Client applications registered on server
Consumer key + secret
•
Wednesday, March 18, 2009
10. OAuth
• Token based API authentication + authorization
Exchange tokens rather than login & password
•
Authorization on server side
•
• Client applications registered on server
Consumer key + secret
•
• HTTP requests signed by authorized tokens
Wednesday, March 18, 2009
11. OAuth
• Token based API authentication + authorization
Exchange tokens rather than login & password
•
Authorization on server side
•
• Client applications registered on server
Consumer key + secret
•
• HTTP requests signed by authorized tokens
• Tokens can be revoked
Wednesday, March 18, 2009
13. Challenges
1.Nice code
Wednesday, March 18, 2009
14. Challenges
1.Nice code
2.Integrating OAuth
Signing API requests
•
Implementing the token authorization
•
Wednesday, March 18, 2009
15. Challenges
1.Nice code
2.Integrating OAuth
Signing API requests
•
Implementing the token authorization
•
3.Posting large (80+Mb) data to the server
Memory issues & upload progress
•
Wednesday, March 18, 2009
16. Challenges
1.Nice code
2.Integrating OAuth
Signing API requests
•
Implementing the token authorization
•
3.Posting large (80+Mb) data to the server
Memory issues & upload progress
•
4.Sending requests asynchronously
Wednesday, March 18, 2009
17. Challenges
1.Nice code
2.Integrating OAuth
Signing API requests
•
Implementing the token authorization
•
3.Posting large (80+Mb) data to the server
Memory issues & upload progress
•
4.Sending requests asynchronously
5.Cross platform Xcode project (3 platforms)
Wednesday, March 18, 2009
19. 1. Nice code
• Meant for 3rd party developers
Few classes that are easy to understand
•
Stable interface
•
Wednesday, March 18, 2009
20. 1. Nice code
• Meant for 3rd party developers
Few classes that are easy to understand
•
Stable interface
•
• API might change
Not implementing model or controller logic
•
Just implementing communication layer
•
JSON / XML parsing is up to the developer
•
Wednesday, March 18, 2009
21. Architecture
Client AppDelegate Some Client Controller
Authentication
API Delegate
Delegate
Wednesday, March 18, 2009
22. Architecture
Client AppDelegate Some Client Controller
Authentication
API Delegate
Delegate
SoundCloud API
Wednesday, March 18, 2009
23. Architecture
Client AppDelegate Some Client Controller
Authentication
API Delegate
Delegate
SoundCloud API
SoundCloud API
Configuration
Wednesday, March 18, 2009
24. Architecture
Client AppDelegate Some Client Controller
Authentication
API Delegate
Delegate
SoundCloud API
SoundCloud API
Configuration
Wednesday, March 18, 2009
31. OAuth token flow
Client Application SoundCloud
Step 1 Asking for request
Generating
Getting unauthorized token
request token request token
Step 2 Sent user to browser
Obtaining user with token in URL
authorization via browser
Wednesday, March 18, 2009
32. OAuth token flow
Client Application SoundCloud
Step 1 Asking for request
Generating
Getting unauthorized token
request token request token
Step 2 Sent user to browser
Obtaining user with token in URL User logs in
authorization via browser
+ authorizes token
Wednesday, March 18, 2009
33. OAuth token flow
Client Application SoundCloud
Step 1 Asking for request
Generating
Getting unauthorized token
request token request token
Step 2 Sent user to browser
Obtaining user with token in URL User logs in
authorization via browser
+ authorizes token
Redirecting to
callback URL with
custom URL scheme
Wednesday, March 18, 2009
34. OAuth token flow
Client Application SoundCloud
Step 1 Asking for request
Generating
Getting unauthorized token
request token request token
Step 2 Sent user to browser
Obtaining user with token in URL User logs in
authorization via browser
+ authorizes token
Redirecting to
callback URL with
Application is triggered
custom URL scheme
by URL scheme
Wednesday, March 18, 2009
35. OAuth token flow
Client Application SoundCloud
Step 1 Asking for request
Generating
Getting unauthorized token
request token request token
Step 2 Sent user to browser
Obtaining user with token in URL User logs in
authorization via browser
+ authorizes token
Redirecting to
callback URL with
Application is triggered
custom URL scheme
by URL scheme
App initiates exchange
Step 3
of request token
Exchanging tokens
Wednesday, March 18, 2009
36. OAuth token flow
Client Application SoundCloud
Step 1 Asking for request
Generating
Getting unauthorized token
request token request token
Step 2 Sent user to browser
Obtaining user with token in URL User logs in
authorization via browser
+ authorizes token
Redirecting to
callback URL with
Application is triggered
custom URL scheme
by URL scheme
App initiates exchange
Step 3
of request token
Exchanging tokens
Generating
access token
Wednesday, March 18, 2009
37. OAuth token flow
Client Application SoundCloud
Step 1 Asking for request
Generating
Getting unauthorized token
request token request token
Step 2 Sent user to browser
Obtaining user with token in URL User logs in
authorization via browser
+ authorizes token
Redirecting to
callback URL with
Application is triggered
custom URL scheme
by URL scheme
App initiates exchange
Step 3
of request token
Exchanging tokens
Generating
access token
Step 4 Stores access token
Storing access token
for further usage
Wednesday, March 18, 2009
38. 3.Large files
• Audio files are large!
60 min recording is very large, even compressed
•
• The problem with lage files
iPhone memory limits
•
Upload progress not supported by NSURLConnection
•
• The solution: NSInputStream
Be aware!! Not easy to subclass
•
Wednesday, March 18, 2009
39. Post Body Stream
• Data transfered as multipart
• SCPostBodyStream as wrapper around array of stream parts
• Stream parts
NSString, NSURL (file URL), NSData
•
• Upload progress by counting bytes in read method
Wednesday, March 18, 2009
41. 4.Asynchronous Communication
Connection
Your Class
Connection
Connection Management
Data fetching
Request generation
Connection
Wednesday, March 18, 2009
42. 4.Asynchronous Communication
Connection
SoundCloud API
Connection
Connection Management
Data fetching
Request generation
Connection
Wednesday, March 18, 2009
43. 4.Asynchronous Communication
Connection
SoundCloud API
Your Class
Connection
Lean & Mean Connection Management
Data fetching
Request generation
Connection
Wednesday, March 18, 2009
44. 4.Asynchronous Communication
OAuth
Connection
SoundCloud API
Your Class
Connection
Lean & Mean Connection Management
Data fetching
Request generation
Connection
Uploads
Wednesday, March 18, 2009
46. 5. Cross platform Xcode project
• Targets
Framework for Mac
•
Fat static library for iPhone Device & Simulator
•
Wednesday, March 18, 2009
47. 5. Cross platform Xcode project
• Targets
Framework for Mac
•
Fat static library for iPhone Device & Simulator
•
• Problems
Only one Base SDK per project
•
Base SDK defines which frameworks are allowed
•
Wednesday, March 18, 2009
48. 5. Cross platform Xcode project
• Targets
Framework for Mac
•
Fat static library for iPhone Device & Simulator
•
• Problems
Only one Base SDK per project
•
Base SDK defines which frameworks are allowed
•
• Solution
Manually linking frameworks
•
Wednesday, March 18, 2009
49. 5. Cross platform Xcode project
• Targets
Framework for Mac
•
Fat static library for iPhone Device & Simulator
•
• Problems
Only one Base SDK per project
•
Base SDK defines which frameworks are allowed
•
• Solution
Manually linking frameworks
•
Wednesday, March 18, 2009
51. Try it !
• Licensed under Apache License
You‘re free to use it
•
Can be integrated into closed projects
•
Just mention us somewhere
•
Wednesday, March 18, 2009
52. Try it !
• Licensed under Apache License
You‘re free to use it
•
Can be integrated into closed projects
•
Just mention us somewhere
•
• Wrapper comes with iPhone and Mac demo projects
Wednesday, March 18, 2009
53. Try it !
• Licensed under Apache License
You‘re free to use it
•
Can be integrated into closed projects
•
Just mention us somewhere
•
• Wrapper comes with iPhone and Mac demo projects
Lets see some code!
Wednesday, March 18, 2009