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.
#import <DropboxSDK/DropboxSDK.h>

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

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
			NSString *dropBoxAppKey = @"YOUR_APP_KEY";
			NSString *dropBoxAppSecret = @" YOUR_APP_SECRET";
			NSString *root = kDBRootDropbox;
			DBSession* session =
			[[DBSession alloc] initWithAppKey:dropBoxAppKey appSecret:dropBoxAppSecret root:root];
			session.delegate = self;
			[DBSession setSharedSession:session];
			[DBRequest setNetworkRequestDelegate:self];
		}

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

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
			{
   			 if ([[DBSession sharedSession] handleOpenURL:url]) {
     			   if ([[DBSession sharedSession] isLinked]) {
          		  [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"OPEN_DROPBOX_VIEW" object:nil]];
       			 }
       		 return YES;
    			}
    		return NO;
		}

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.

		- (IBAction)clk_login:(id)sender {
    			if (![[DBSession sharedSession] isLinked])
    				{
				//Login
      				  [[DBSession sharedSession] linkFromController:self];
  				  }
   			 else
    			{
      			  //Already logged
				[self fetchAllData];
  			  }
		}

#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.

		@property (nonatomic, strong) DBRestClient *restClient;
		
		- (DBRestClient *)restClient
		{
 		   if (self.restClient == nil) {
     		   self.restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
    		    self.restClient.delegate = self;
 		   }
  		  return self.restClient;
		}

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

@property (nonatomic, strong) NSString *loadData;
		
		-(void)fetchAllData
		{        
       	    loadData = @"";
    		    [self.restClient loadMetadata:loadData];
		}

#6

DBRestClientDelegate handle response

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

- (void)restClient:(DBRestClient*)client loadedMetadata:(DBMetadata *)metadata
		{
			for (int i = 0; i < [metadata.contents count]; i++) {
			DBMetadata *data = [metadata.contents objectAtIndex:i];
			if (data.isDirectory) {
			//Add folder object in your array
			}else {
			//Add file object in your array
				}
			}
		}
		
		- (void)restClient:(DBRestClient *)client loadMetadataFailedWithError:(NSError *)error{ 
		}

#7

DBRestClientDelegate Methods for Add Data

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

-(void)AddFileToDropBox:(NSString *)filePath
		{
 		   [self.restClient uploadFile:@“Filename” toPath:filePath withParentRev:@"" fromPath:[[NSBundle mainBundle] pathForResource:@“file” ofType:@"png"]];
		}

To add files to Dropbox at ROOT path

[self AddFileToDropBox:@"/"];

Delegate Methods to add files

-(void)restClient:(DBRestClient *)client uploadedFile:(NSString *)destPath from:(NSString *)srcPath
		{
  			  UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"" message:@"File uploaded successfully." preferredStyle:UIAlertControllerStyleAlert];    
   			 UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
			    }];
 			   [alertC addAction:actionOK];
   			 [self presentViewController:alertC animated:YES completion:^{
   			 }];
		}

		-(void)restClient:(DBRestClient *)client uploadFileFailedWithError:(NSError *)error
		{
   			 UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"" message:[error localizedDescription] preferredStyle:UIAlertControllerStyleAlert];
   			 UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
   			 }];
   			 [alertC addAction:actionOK];
    			[self presentViewController:alertC animated:YES completion:^{
   			 }];
		}

#8

DBRestClientDelegate Methods for Delete Data

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

DBMetadata *metadata = [array objectAtIndex:indexPath.row];
    		[self deleteFile:metadata.path];

Delegate Methods to Delete files

- (void)restClient:(DBRestClient*)client deletedPath:(NSString *)path
		{
 		   UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"" message:@"File deleted successfully." preferredStyle:UIAlertControllerStyleAlert];
  		  UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    			}];
  		  [alertC addAction:actionOK];
   		 [self presentViewController:alertC animated:YES completion:^{
    			}];
		}

		- (void)restClient:(DBRestClient*)client deletePathFailedWithError:(NSError*)error
		{
 		   UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"" message:[error localizedDescription] preferredStyle:UIAlertControllerStyleAlert];
   			 UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
   		 	}];
    			[alertC addAction:actionOK];
    		 	  [self presentViewController:alertC animated:YES completion:^{
  			  }];
		}
Dropbox integration-iPhone
Darshan Patel
Darshan Patel is a lead Search Engine Analyst at Nimblechapps - Coolest Mobile App Development Company. He believes in using a variety of strategy to create business’s online presence that delight and deliver.