Use the Dropbox API to add cloud based import and export to your iPhone and iPad apps

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(@&quot;DBSession is linked&quot;);
} <B>else</B> {
    NSLog(@&quot;DBSession is <B>not</B> linked&quot;);
    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:@&quot;/&quot; withHash:photosHash<B>]</B>;

<B>[</B><B>[</B>DBSession sharedSession<B>]</B> unlink<B>]</B>;
NSLog(@&quot;Unlinking  session&quot;);

}

  • (void)restClient:(DBRestClient)client loadedMetadata:(DBMetadata)metadata; { NSLog(@"restClient:loadedMetadata %@",metadata); NSLog(@"%@ is a %@ at %@",[metadata path],[metadata isDirectory]?@"directory":@"file",[metadata root]); for (DBMetadata *child in metadata.contents){ NSLog(@"with child %@ which is a %@ at %@",[child path],[child isDirectory]?@"directory":@"file",[metadata root]); } }

Attachment: 

Comments

This is great! Thanks for taking the time to share this!

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

Nice Article, really helpful.

Very nice article. Straight to the point. Thanks.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.