Requirements:
- You need the 4.0 version of the iPhone SDK. The version of your XCode should be at least 3.2.3.
- You need to have registered as a Dropbox application with mobile access. You should have a consumer key and secret.
- You need to download the dropbox sdk
A. Adding DropboxSDK to your project
- Open your project in Xcode
- Navigate to where you uncompressed the SDK and drag the DropboxSDK.framework folder into your project in Xcode
- Make sure Copy items into destination group's folder is selected
- Press Add button
- 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 text
APP_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;
if (file.isDirectory) {for (DBMetadata *file in metadata.contents) {
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