This article might be too old.
There are many applications that can communicate with DropBox. DropBox is the most famous storage service mainly because of how easy people can use it. I’m pretty sure that you’ve heard about it and that most of you are also using it. So, it is natural to think of making applications interact with it. I’d like to introduce how to implement DropBox service in your iOS application.
1. Introduction
Let’s get started. First of all, you should install the SDK from dropbox. ( The latest one is 1.3.5 ) [https://www.dropbox.com/developers/core/sdks/ios] (https://www.dropbox.com/developers/core/sdks/ios) After that, you should register your application at DropBox developer website. [https://www.dropbox.com/developers/apps] (https://www.dropbox.com/developers/apps) In my case, I want to make an app that makes csv files, gets them and display them. So, it goes like this :
You can get “App key” and “App secret”. We’ll use them when we write code.
2. Setting your app
After you get DropBox SDK, you should integrate it into your app. In Xcode, you add “DropboxSDK.framework”. This can be accomplished by control-click on Project Navigator and “add files to blabla”. Also, DropBox uses “Security.framework” and “QuartzCore.framework” so please add the frameworks from project summary screen. After that, you can finally write your code. It’s fun!!
3. Implement DropBox SDK
We add the code to make a session with dropbox:
1 2 3 4 | DBSession* dbSession = [[DBSession alloc] initWithAppKey:@"blabla" //Appkey appSecret:@"blabla" //secretkey root:@"sandbox"]; [DBSession setSharedSession:dbSession]; |
This code is “/* Creating and setting the shared DBSession should be done before any other Dropbox objects are used, perferrably in the UIApplication delegate. */ ”.
Also, you add this code in your view controller.
1 2 3 4 5 6 7 8 | - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if(![[DBSession sharedSession] isLinked]) { [[DBSession sharedSession] linkFromController:self]; } } |
There are many people who forget to edit Info.plist file ( I took some time to figure it out too ). In your Info.plist, please add this url scheme :CFBundleURLTypes CFBundleURLSchemes db-YourAPIKEY
One way of adding this code is to control-click the Info.plist file,“Open as…” and “Source Code”. If you forget this step, this kind of error message will appear:
[ERROR] DropboxSDK: unable to link; app isn’t registered for correct URL scheme
At this time, you can see the login screen to dropbox when you launch your app. If you did it on viewDidLoad function, xcode will warn you about the window hierarchy and you’ll fail to show login screen.
4. How to use DropBox SDK
So far we’ve only implemented DropBox SDK. I’d like to show some basic function to be able to use it. First, let’s add delegate and prepare for DBRestClient class. In your viewcontroller, add something like this :
1 2 3 4 5 6 7 | #import <UIKit/UIKit.h> #import <DropboxSDK/DropboxSDK.h> @interface KKViewController : UIViewController<DBRestClientDelegate> @property (nonatomic, readonly) DBRestClient *restClient; @end |
We added “
1 2 3 4 5 6 7 8 9 10 | // Getter - (DBRestClient *)restClient { if (!_restClient) { restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]]; _restClient.delegate = self; } return restClient; } |
5. Some Features
Functions :
Loads metadata for the object at the given root/path and returns the result to the delegate as a dictionary
1 | - (void)loadMetadata:(NSString*)path; |
Uploads a file that will be named filename to the given path on the server. sourcePath is the full path of the file you want to upload. If you are modifying a file, parentRev represents the rev of the file before you modified it as returned from the server. If you are uploading a new file set parentRev to nil.
1 2 3 4 | - (void)uploadFile:(NSString *)filename toPath:(NSString *)path withParentRev:(NSString *)parentRev fromPath:(NSString *)sourcePath; |
Loads the file contents at the given root/path and stores the result into destinationPath
1 | - (void)loadFile:(NSString *)path intoPath:(NSString *)destinationPath; |
This function is for deleting your file.
1 | - (void)deletePath:(NSString*)path; |
Delegate :
The delegate provides allows the user to get the result of the calls made on the DBRestClient. Right now, the error parameter of failed calls may be nil and [error localizedDescription] does not contain an error message appropriate to show to the user.
1 | - (void)restClient:(DBRestClient*)client loadedMetadata:(DBMetadata*)metadata; |
6. End
If you have any corrections or new ideas related to this post, please feel free to share your thoughts. Also, I made a simple application that can communicate with dropbox and show the csv file in collection view. If you’ll check this out, that would be great. https://github.com/kzykbys/DropBoxAPIwithCollectionView Thank you.