Menu

Category

Archive

logo


Introction to DropBox SDK

2013-08-12 09:00:00 +0900
  • このエントリーをはてなブックマークに追加

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 After that, you should register your application at DropBox developer website. 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 DBSession* dbSession = [[DBSession alloc] initWithAppKey:@"blabla" //Appkey
2                                                appSecret:@"blabla" //secretkey
3                                                   root:@"sandbox"];
4 [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 - (void)viewDidAppear:(BOOL)animated
2 {
3   [super viewDidAppear:animated];
4   if(![[DBSession sharedSession] isLinked])
5   {
6       [[DBSession sharedSession] linkFromController:self];
7   }
8 }

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 #import <UIKit/UIKit.h>
2 #import <DropboxSDK/DropboxSDK.h>
3 @interface KKViewController : UIViewController<DBRestClientDelegate>
4 
5 @property (nonatomic, readonly) DBRestClient *restClient;
6 
7 @end

We added “” and the property. After that, write getter to the restClient in your viewcontroller implement file like this :

 1 // Getter
 2 - (DBRestClient *)restClient
 3 {
 4  if (!_restClient)
 5  {
 6      restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
 7      _restClient.delegate = self;
 8  }
 9  return restClient;
10 }

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 - (void)uploadFile:(NSString *)filename
2             toPath:(NSString *)path
3    withParentRev:(NSString *)parentRev
4         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.