Sunday, October 21, 2012

Dropbox Integration in IOS


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. You should have a consumer key and secret.
  3. You need to download the dropbox sdk

A. Adding DropboxSDK to your project

  1. Open your project in Xcode
  2. Navigate to where you uncompressed the SDK and drag the DropboxSDK.framework folder into your project in Xcode
  3. Make sure Copy items into destination group's folder is selected
  4. Press Add button
  5. Ensure that you have Security.framework and QuartzCore.framework added to your project. To do this in Xcode 4, select your project file in the file explorer, select your target, and select the Build Phases sub-tab. Under Link Binary with Libraries, press the + button, select Security.framework, and press Add. Repeat forQuartzCore.framework.

Your app key is also needed in YourProject-Info.plist file so the app can register for the correct url scheme. To do this, find the file under the Resources group in the left pane, right-click it and select Open As → Source Code. Replace the textAPP_KEY with your app's key. (e.g-db-xs9loltwb0mlu4p)..

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    
    DBSession * dbSession = [[[DBSession alloc]initWithAppKey:@"YOUR-App-Key" appSecret:@"Your-secret-Key
" root:kDBRootDropbox] autorelease];
       [DBSession setSharedSession:dbSession];
    }
Somewhere in your app, add an event to launch the Dropbox authentication process:-
- (void)didPressLink {
    if (![[DBSession sharedSession] isLinked]) {
        [[DBSession sharedSession] linkFromController:yourRootController];
    }
}
The DBRestClient class is the way to access Dropbox from your app once the user has linked his account.o add an instance variable in the .h file 
DBRestClient *restClient;
#import <DropboxSDK/DropboxSDK.h>

...

- (DBRestClient *)restClient {
   if (!restClient) {
      restClient =
         [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
      restClient.delegate = self;
   }
   return restClient;
}
You can list the files in folder you just uploaded 
[[self restClient] loadMetadata:@"/"];
if (metadata.isDirectory) {
- (void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata {
      dropboxPath= metadata.path;
      for (DBMetadata *file in metadata.contents) {
          if (file.isDirectory) {            
  NSLog(@"folder name ...%@", file.filename);
            }
            else {
                NSLog(@"image...%@", file.filename);
             
            }
        } 
}
   
- (void)restClient:(DBRestClient *)client
    loadMetadataFailedWithError:(NSError *)error {

    NSLog(@"Error loading metadata: %@", error);
}

Download a file

[[self restClient] loadFile:dropboxPath intoPath:localPath]
To find out when the file download either succeeds or fails implement the following DBRestClientDelegate methods:
- (void)restClient:(DBRestClient*)client loadedFile:(NSString*)localPath {
    NSLog(@"File loaded into path: %@", localPath);
}

- (void)restClient:(DBRestClient*)client loadFileFailedWithError:(NSError*)error {
    NSLog(@"There was an error loading the file - %@", error);

No comments:

Post a Comment