
Cloud based storage and syncing is fast becoming a critical part of many iPhone and iPad applications. There are many internet/cloud based services such as S3 and one of the easiest and more popular is DropBox https://www.dropbox.com/developers
In late July the Dropbox api became public and since then it has been adopted in many different applications. The API is easy to use, has good documentation and comes with a sample application.
The sample application shows a random photo from your /photos folder. I thought it might be useful to also go through another even simpler example to explore the basics of the process. The first thing you need to do is go to https://www.dropbox.com/developers sign up as a developer and download the iPhone Objective-C SDK.
Then for the purposes of this example, create a simple view based project, with one button hooked up to an action. Drag and drop the DropboxSDK folder into Xcode to add it to your project. Add the security framework to your project so that Dropbox can store the necessary credentials.
The way login works is by linking your application with the users account and getting a token that can be used in subsequent calls. Luckily you don’t need to worry about any of that and you never even see the user information.
The first thing you need to do in code is create a DBSession. Minor point but DB in my mind still means Data Base but here it means Drop Box. If the session is not linked (user logged in) then create and present a DBLoginController.
The drop box calls are asynchronous and there are various delegates for being notified of successful actions and failures. I’ve simplified all that for the purposes of this example but you will want to break things out to their various delegate methods and handle errors gracefully.
We’ll assume the login succeeded and we would like to get some information about the users root / top level folder. To get information on a file or directory we ask the DBRestClient for the metadata passing in the path of the file or directory and the hash if you have it. The hash lets the system know if the file or directory has been changed and can save you from downloading data if it has not.
The meta data returns info such as size, modified, is a directory or not, etc. And if a directory will return metadata for the directories and files it contains. You can then search for the file you are interested in and download the contents to your app. There also calls available for file operations such as copy, move and folder creation.
I hope this simple example gets you started with the Dropbox api and you add some great syncing, import/export features to your apps.
-(IBAction) testDropbox; { NSString* consumerKey = YOUR_KEY_HERE; NSString* consumerSecret = YOUR_SECRET_HERE;
DBSession* session = <B>[</B><B>[</B>DBSession alloc<B>]</B> initWithConsumerKey:consumerKey consumerSecret:consumerSecret<B>]</B>; session.delegate = self; <B>[</B>DBSession setSharedSession:session<B>]</B>; <B>[</B>session release<B>]</B>; <B>if</B> (<B>[</B><B>[</B>DBSession sharedSession<B>]</B> isLinked<B>]</B>){ NSLog(@"DBSession is linked"); } <B>else</B> { NSLog(@"DBSession is <B>not</B> linked"); DBLoginController* controller = <B>[</B><B>[</B>DBLoginController new<B>]</B> autorelease<B>]</B>; controller.delegate = self; <B>[</B>controller presentFromController:self<B>]</B>; } DBRestClient *rc = <B>[</B><B>[</B>DBRestClient alloc<B>]</B> initWithSession:<B>[</B>DBSession sharedSession<B>]</B><B>]</B>; self.restClient = rc; <B>[</B>rc release<B>]</B>; self.restClient.delegate = self; NSString *photosHash = nil; // <B>fill</B> with a previous value <B>if</B> you have one <B>[</B>self.restClient loadMetadata:@"/" withHash:photosHash<B>]</B>; <B>[</B><B>[</B>DBSession sharedSession<B>]</B> unlink<B>]</B>; NSLog(@"Unlinking session");}
Comments
megamancito (not verified)
Sat, 04/02/2011 - 22:29
Permalink
This is great! Thanks for
This is great! Thanks for taking the time to share this!
pierre (not verified)
Sun, 06/05/2011 - 23:00
Permalink
Dropbox integration with iPad
Thanks for the above. I want my app to use keychain for login to Dropbox instead of the user to type in his name and password every time he upload or download a file, HOW? Please show us an example.
thanks
Kamleshwar (not verified)
Wed, 11/30/2011 - 01:42
Permalink
Nice Article
Nice Article, really helpful.
SmithSRaj (not verified)
Fri, 12/02/2011 - 00:29
Permalink
short and precise
Very nice article. Straight to the point. Thanks.
Add new comment