Thursday, April 7, 2011

Dropbox Integration in Iphone


Requirements:


1. You need the 4.0 version of the iPhone SDK. The version of your XCode should
be at least 3.2.3.
2. You need to have registered as a Dropbox application with mobile access at
http://dropbox.com/developers. You should have a consumer key and secret.
3. You need to download the dropbox sdk from https://www.dropbox.com/developers/releases

A. Adding DropboxSDK to your project


1. Open your project in XCode
2. Right-click on your project in the group tree in the left pane
3. Select Add -> Existing Files...
4. Navigate to where you uncompressed the Dropbox SDK and select the DropboxSDK
subfolder
5. Select "Copy items into destination group's folder"
6. Make sure "Recursively create groups for any added folders" is selected
7. Press Add button
8. Find the Frameworks folder in your app's group tree in the left pane
9. Make sure the framework Security.framework is added to your project
10. If not, right-click on Frameworks and select Add -> Existing Frameworks...
11. Select Security.framework from the list and select Add
12. Build your application. At this point you should have no build failures or
warning

B. Login successfully in your app


1. In your application delegate's application:didFinishLaunchingWithOptions:
method, add the following code:

DBSession* dbSession = [[[DBSession alloc] initWithConsumerKey:@"<YOUR CONSUMER KEY>" consumerSecret:@"<YOUR CONSUMER SECRET>"] autorelease];
[DBSession setSharedSession:dbSession];

Note: you will need to #import "DropboxSDK.h" at the top of this file

2. Somewhere in your app, add an event to launch the login controller, which
should look something like this:

- (void)didPressLink {
DBLoginController* controller = [[DBLoginController new] autorelease];
[controller presentFromController:self];
}

Note: you will need to #import "DropboxSDK.h" at the top of this file


C. Creating folder in your dropbox using your App


1. In your .m file add the below code,

@interface DropBoxViewController () < DBLoginControllerDelegate, DBRestClientDelegate>

@property (nonatomic, readonly) DBRestClient* restClient;
@end
#pragma mark -
#pragma mark DBLoginControllerDelegate methods
- (void)loginControllerDidLogin:(DBLoginController*)controller
{
restClient = [self restClient];
[restClient setDelegate:self];
[def setBool:YES forKey:@"userLoggedToDropboxAccnt"];
[NSUserDefaults resetStandardUserDefaults];
[restClient loadMetadata:@"" withHash:photosHash];
}
- (void)loginControllerDidCancel:(DBLoginController*)controller {
}
- (DBRestClient*)restClient {
if (restClient == nil) {
restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
restClient.delegate = self;
}
return restClient;
}
#pragma mark -
#pragma mark DBRestClientDelegate methods
- (void)restClient:(DBRestClient*)client loadedMetadata:(DBMetadata*)metadata {
[photosHash release];
photosHash = [metadata.hash retain];
NSMutableArray* newPhotoPaths = [NSMutableArray new];
for (DBMetadata* child in metadata.contents) {
[newPhotoPaths addObject:child.path];
}
[photoPaths release];
photoPaths = newPhotoPaths;
self.contentArray = photoPaths;
if([photoPaths containsObject:folderNmTxtField.text]){
}
else{
[restClient createFolder:folderNmTxtField.text];
}
}

photosHash is of type NSString defined in .h file
photoPaths is an NSArray defined in .h file

D.Uploading file in yur dropbox using your app


if restClient not initialized earlier, add the below code

restClient=[self restClient];
[restClient setDelegate:self];
[restClient loadMetadata:@"" withHash:photosHash];

for uploading,

[restClient uploadFile: filename toPath: (folder in which file is to be uploaded) fromPath: (path of the file to be uploaded);


11 comments:

  1. Hi,
    I have been trying to loadMetadata of a particular folder and so I learned that I have to use loadMetadata:withHash.
    I tried the above approach but it doesn't work.
    my photosHash contains the path of my folder i.e. "/myfolder" but it does not work. Pleae advise.

    ReplyDelete
  2. Does the creation of the DBSession have to be in the appDelegate? Can't I create it elsewhere?

    ReplyDelete
  3. @Namratha you can create in any other class also

    ReplyDelete
  4. Morning Swati, I am new to IOS development. What I try, is to connect to Dropbox to upload and download files. Your example above is fine. My question is how do I keep my login once I have logged on once. I do not want the user to login every time he uploads or downloads a file. There are apps that operate with reading your keychain either or your Mac or iPad. Please have you seen this before. Your comment would be appreciated.
    Pierre (pierresinne@gmail.com)
    thanks

    ReplyDelete
  5. @Pierre you can see in the code I had posted

    - (void)loginControllerDidLogin:(DBLoginController*)controller
    {

    [def setBool:YES forKey:@"userLoggedToDropboxAccnt"];

    }

    Here I am saving to userdefaults that user is logged.You can also verify using this defaults key & upload/ download files without asking user to login again & again

    ReplyDelete
  6. Is it possible to do so with a windows based app in a single form as opposed to a view based app without controllers?

    ReplyDelete
  7. I am not a code savvy but ya sure think that it will work properly. My developer will do that for me :). Thanks swati once again for a great stuff & keep sharing more...
    Shame Alarm iPhone App

    ReplyDelete
  8. @ SWATHI:

    I followed and create appKey and follow steps.
    but i get error session failed with set as root folder or root to drop box and also getting error loadedmetadata

    can u please send me any sample working code along this mail id ran168600@gmail.com

    ReplyDelete
  9. I integrated dropbox. Login is done properly also I'm able to create folder in dropBox. But when I try to load file I'm getting error

    Following error I'm getting

    error making request to /1/files_put/dropbox/Info.plist - Error
    Domain=NSURLErrorDomain Code=-1021 "The operation couldn’t be
    completed. (NSURLErrorDomain error -1021.)" UserInfo=0x6859bc0
    {destinationPath=/Info.plist,
    sourcePath=/Users/bcod/Library/Application Support/iPhone
    Simulator/5.0/Applications/0E1EE43C-8F6B-40FA-8696-D3992DA2DCE5/DBRoulette.app/Inf

    ReplyDelete
  10. hiii can anyone send sample code of this

    thanx.

    ReplyDelete
  11. After importing "Dropboxsdk.h" i am not getting DBLoginControllerDelegate
    what to do nw using dropbox-ios-sdk-1.3.13...

    ReplyDelete