Nimblechapps Blog

This is the section where we get a chance to rant about latest happenings in the tech world. Keen to know our thoughts? Refer our blogs

How to Integrate Dropbox with your iOS APP and Perform CRUD Action

Integrate Dropbox with your iOS APP and Perform CRUD Action

The Dropbox framework is the third party that is available for file sharing with friends from your phone. Using Dropbox can upload, download and share files using the iOS device.Before you start, you will need a Dropbox account. Create one for free right here with 2GB of storage, if you don’t have already.

In this article, we are going to cover in detail on how to integrate Dropbox with your iOS application and perform Add and Remove files via coding on certain user action. Following are the steps.

#1

Register the application

1

Go to https://www.dropbox.com/developers and start registering your app.

2

The first choice to make is between “Drop-ins app” and “Dropbox API app”. Make your proper choice and start with the detailing. To know the difference between “Drop-ins app” and “Dropbox API app”, you can refer to this link

3

Then depending on your app need to select the specific option from the type of access you need.

4

After completing these two steps, you would receive an App key and an App secret key. These are important! Dropbox no longer allows direct login via username/password combination but uses OAuth to authenticate users. You will need the keys for authentication purpose.

DropBoxApi

#2

Configure your Xcode project

Now open a new Project or an existing one where you would integrate Dropbox, Download the Dropbox SDK for iOS from here.
Follow these steps:

1

Open your project.

2

Navigate to the SDK and drag the DropboxSDK.framework folder into your project in Xcode. Make sure “Copy items into destination group’s folder” is selected.

3

Ensure that you have the Security. framework added to your project. Click here to see how to add framework to a project

#3

Authentication

  • Add the following header to your AppDelegate File.

[pastacode lang=”c” manual=”%23import%20%3CDropboxSDK%2FDropboxSDK.h%3E” message=”” highlight=”” provider=”manual”/]

Now set DBSessionDelegate and DBNetworkRequestDelegate in your AppDelegate file, and for DropBox, session setting add below code in your

[pastacode lang=”c” manual=”-%20(BOOL)application%3A(UIApplication%20*)application%20didFinishLaunchingWithOptions%3A(NSDictionary%20*)launchOptions%20%7B%0A%09%09%09NSString%20*dropBoxAppKey%20%3D%20%40%22YOUR_APP_KEY%22%3B%0A%09%09%09NSString%20*dropBoxAppSecret%20%3D%20%40%22%20YOUR_APP_SECRET%22%3B%0A%09%09%09NSString%20*root%20%3D%20kDBRootDropbox%3B%0A%09%09%09DBSession*%20session%20%3D%0A%09%09%09%5B%5BDBSession%20alloc%5D%20initWithAppKey%3AdropBoxAppKey%20appSecret%3AdropBoxAppSecret%20root%3Aroot%5D%3B%0A%09%09%09session.delegate%20%3D%20self%3B%0A%09%09%09%5BDBSession%20setSharedSession%3Asession%5D%3B%0A%09%09%09%5BDBRequest%20setNetworkRequestDelegate%3Aself%5D%3B%0A%09%09%7D” message=”” highlight=”” provider=”manual”/]

• In your AppDelegate file add below code to check if session is linked or not

[pastacode lang=”c” manual=”-%20(BOOL)application%3A(UIApplication%20*)application%20handleOpenURL%3A(NSURL%20*)url%0A%09%09%09%7B%0A%20%20%20%09%09%09%20if%20(%5B%5BDBSession%20sharedSession%5D%20handleOpenURL%3Aurl%5D)%20%7B%0A%20%20%20%20%20%09%09%09%20%20%20if%20(%5B%5BDBSession%20sharedSession%5D%20isLinked%5D)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%09%09%20%20%5B%5BNSNotificationCenter%20defaultCenter%5D%20postNotification%3A%5BNSNotification%20notificationWithName%3A%40%22OPEN_DROPBOX_VIEW%22%20object%3Anil%5D%5D%3B%0A%20%20%20%20%20%20%20%09%09%09%20%7D%0A%20%20%20%20%20%20%20%09%09%20return%20YES%3B%0A%20%20%20%20%09%09%09%7D%0A%20%20%20%20%09%09return%20NO%3B%0A%09%09%7D” message=”” highlight=”” provider=”manual”/]

Add API_KEY in Info.plist

Now you need to add you dropboxAppKey (db- YOUR_APP_KEY) to your info.plist file as shown below, to register for correct URL scheme.

infoPlist

#4

Login

Now for Login to Dropbox add method. It first checks that user is already logged in or not. And if user is not logged in than it will open Login view.

[pastacode lang=”c” manual=”%09%09-%20(IBAction)clk_login%3A(id)sender%20%7B%0A%20%20%20%20%09%09%09if%20(!%5B%5BDBSession%20sharedSession%5D%20isLinked%5D)%0A%20%20%20%20%09%09%09%09%7B%0A%09%09%09%09%2F%2FLogin%0A%20%20%20%20%20%20%09%09%09%09%20%20%5B%5BDBSession%20sharedSession%5D%20linkFromController%3Aself%5D%3B%0A%20%20%09%09%09%09%20%20%7D%0A%20%20%20%09%09%09%20else%0A%20%20%20%20%09%09%09%7B%0A%20%20%20%20%20%20%09%09%09%20%20%2F%2FAlready%20logged%0A%09%09%09%09%5Bself%20fetchAllData%5D%3B%0A%20%20%09%09%09%20%20%7D%0A%09%09%7D” message=”” highlight=”” provider=”manual”/]

#5

DBRestClientDelegate methods

Now to fetch data from Dropbox use DBRestClient object and add below function in your class to initialize the DBRestClient object. Set DBRestClientDelegate in your class.

[pastacode lang=”c” manual=”%09%09%40property%20(nonatomic%2C%20strong)%20DBRestClient%20*restClient%3B%0A%09%09%0A%09%09-%20(DBRestClient%20*)restClient%0A%09%09%7B%0A%20%09%09%20%20%20if%20(self.restClient%20%3D%3D%20nil)%20%7B%0A%20%20%20%20%20%09%09%20%20%20self.restClient%20%3D%20%5B%5BDBRestClient%20alloc%5D%20initWithSession%3A%5BDBSession%20sharedSession%5D%5D%3B%0A%20%20%20%20%09%09%20%20%20%20self.restClient.delegate%20%3D%20self%3B%0A%20%09%09%20%20%20%7D%0A%20%20%09%09%20%20return%20self.restClient%3B%0A%09%09%7D” message=”” highlight=”” provider=”manual”/]

After that call below method to fetch data from Dropbox. While calling this method first time make sure to set loadData = @””;

[pastacode lang=”c” manual=”%40property%20(nonatomic%2C%20strong)%20NSString%20*loadData%3B%0A%09%09%0A%09%09-(void)fetchAllData%0A%09%09%7B%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%09%20%20%20%20loadData%20%3D%20%40%22%22%3B%0A%20%20%20%20%09%09%20%20%20%20%5Bself.restClient%20loadMetadata%3AloadData%5D%3B%0A%09%09%7D” message=”” highlight=”” provider=”manual”/]

#6

DBRestClientDelegate handle response

Delegate methods of DBRestClientDelegate’s to handle response of loadMetadata:

[pastacode lang=”c” manual=”-%20(void)restClient%3A(DBRestClient*)client%20loadedMetadata%3A(DBMetadata%20*)metadata%0A%09%09%7B%0A%09%09%09for%20(int%20i%20%3D%200%3B%20i%20%3C%20%5Bmetadata.contents%20count%5D%3B%20i%2B%2B)%20%7B%0A%09%09%09DBMetadata%20*data%20%3D%20%5Bmetadata.contents%20objectAtIndex%3Ai%5D%3B%0A%09%09%09if%20(data.isDirectory)%20%7B%0A%09%09%09%2F%2FAdd%20folder%20object%20in%20your%20array%0A%09%09%09%7Delse%20%7B%0A%09%09%09%2F%2FAdd%20file%20object%20in%20your%20array%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09%09-%20(void)restClient%3A(DBRestClient%20*)client%20loadMetadataFailedWithError%3A(NSError%20*)error%7B%20%0A%09%09%7D” message=”” highlight=”” provider=”manual”/]

#7

DBRestClientDelegate Methods for Add Data

To add files to Dropbox, this just needs to call the below method.

[pastacode lang=”c” manual=”-(void)AddFileToDropBox%3A(NSString%20*)filePath%0A%09%09%7B%0A%20%09%09%20%20%20%5Bself.restClient%20uploadFile%3A%40%E2%80%9CFilename%E2%80%9D%20toPath%3AfilePath%20withParentRev%3A%40%22%22%20fromPath%3A%5B%5BNSBundle%20mainBundle%5D%20pathForResource%3A%40%E2%80%9Cfile%E2%80%9D%20ofType%3A%40%22png%22%5D%5D%3B%0A%09%09%7D” message=”” highlight=”” provider=”manual”/]

To add files to Dropbox at ROOT path

[pastacode lang=”c” manual=”%5Bself%20AddFileToDropBox%3A%40%22%2F%22%5D%3B” message=”” highlight=”” provider=”manual”/]

Delegate Methods to add files

[pastacode lang=”c” manual=”-(void)restClient%3A(DBRestClient%20*)client%20uploadedFile%3A(NSString%20*)destPath%20from%3A(NSString%20*)srcPath%0A%09%09%7B%0A%20%20%09%09%09%20%20UIAlertController%20*alertC%20%3D%20%5BUIAlertController%20alertControllerWithTitle%3A%40%22%22%20message%3A%40%22File%20uploaded%20successfully.%22%20preferredStyle%3AUIAlertControllerStyleAlert%5D%3B%20%20%20%20%0A%20%20%20%09%09%09%20UIAlertAction%20*actionOK%20%3D%20%5BUIAlertAction%20actionWithTitle%3A%40%22OK%22%20style%3AUIAlertActionStyleCancel%20handler%3A%5E(UIAlertAction%20*%20_Nonnull%20action)%20%7B%0A%09%09%09%20%20%20%20%7D%5D%3B%0A%20%09%09%09%20%20%20%5BalertC%20addAction%3AactionOK%5D%3B%0A%20%20%20%09%09%09%20%5Bself%20presentViewController%3AalertC%20animated%3AYES%20completion%3A%5E%7B%0A%20%20%20%09%09%09%20%7D%5D%3B%0A%09%09%7D%0A%0A%09%09-(void)restClient%3A(DBRestClient%20*)client%20uploadFileFailedWithError%3A(NSError%20*)error%0A%09%09%7B%0A%20%20%20%09%09%09%20UIAlertController%20*alertC%20%3D%20%5BUIAlertController%20alertControllerWithTitle%3A%40%22%22%20message%3A%5Berror%20localizedDescription%5D%20preferredStyle%3AUIAlertControllerStyleAlert%5D%3B%0A%20%20%20%09%09%09%20UIAlertAction%20*actionOK%20%3D%20%5BUIAlertAction%20actionWithTitle%3A%40%22OK%22%20style%3AUIAlertActionStyleCancel%20handler%3A%5E(UIAlertAction%20*%20_Nonnull%20action)%20%7B%0A%20%20%20%09%09%09%20%7D%5D%3B%0A%20%20%20%09%09%09%20%5BalertC%20addAction%3AactionOK%5D%3B%0A%20%20%20%20%09%09%09%5Bself%20presentViewController%3AalertC%20animated%3AYES%20completion%3A%5E%7B%0A%20%20%20%09%09%09%20%7D%5D%3B%0A%09%09%7D” message=”” highlight=”” provider=”manual”/]

#8

DBRestClientDelegate Methods for Delete Data

To Delete files to Dropbox, this just needs to call the below method.

[pastacode lang=”c” manual=”DBMetadata%20*metadata%20%3D%20%5Barray%20objectAtIndex%3AindexPath.row%5D%3B%0A%20%20%20%20%09%09%5Bself%20deleteFile%3Ametadata.path%5D%3B” message=”” highlight=”” provider=”manual”/]

Delegate Methods to Delete files

[pastacode lang=”c” manual=”-%20(void)restClient%3A(DBRestClient*)client%20deletedPath%3A(NSString%20*)path%0A%09%09%7B%0A%20%09%09%20%20%20UIAlertController%20*alertC%20%3D%20%5BUIAlertController%20alertControllerWithTitle%3A%40%22%22%20message%3A%40%22File%20deleted%20successfully.%22%20preferredStyle%3AUIAlertControllerStyleAlert%5D%3B%0A%20%20%09%09%20%20UIAlertAction%20*actionOK%20%3D%20%5BUIAlertAction%20actionWithTitle%3A%40%22OK%22%20style%3AUIAlertActionStyleDefault%20handler%3A%5E(UIAlertAction%20*%20_Nonnull%20action)%20%7B%0A%20%20%20%20%09%09%09%7D%5D%3B%0A%20%20%09%09%20%20%5BalertC%20addAction%3AactionOK%5D%3B%0A%20%20%20%09%09%20%5Bself%20presentViewController%3AalertC%20animated%3AYES%20completion%3A%5E%7B%0A%20%20%20%20%09%09%09%7D%5D%3B%0A%09%09%7D%0A%0A%09%09-%20(void)restClient%3A(DBRestClient*)client%20deletePathFailedWithError%3A(NSError*)error%0A%09%09%7B%0A%20%09%09%20%20%20UIAlertController%20*alertC%20%3D%20%5BUIAlertController%20alertControllerWithTitle%3A%40%22%22%20message%3A%5Berror%20localizedDescription%5D%20preferredStyle%3AUIAlertControllerStyleAlert%5D%3B%0A%20%20%20%09%09%09%20UIAlertAction%20*actionOK%20%3D%20%5BUIAlertAction%20actionWithTitle%3A%40%22OK%22%20style%3AUIAlertActionStyleCancel%20handler%3A%5E(UIAlertAction%20*%20_Nonnull%20action)%20%7B%0A%20%20%20%09%09%20%09%7D%5D%3B%0A%20%20%20%20%09%09%09%5BalertC%20addAction%3AactionOK%5D%3B%0A%20%20%20%20%09%09%20%09%20%20%5Bself%20presentViewController%3AalertC%20animated%3AYES%20completion%3A%5E%7B%0A%20%20%09%09%09%20%20%7D%5D%3B%0A%09%09%7D” message=”” highlight=”” provider=”manual”/]

Dropbox integration-iPhone