Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone DropBox API: How to load a file?

A very basic question concerning dropBox integration into an iPhone app.

I followed the setup of the DropBoxSDK and everything works fine. I can log on to my account and get it linked. So I set up everything correctly.

Now I would like to use it to simply load a file from the dropBox and save it again. Consider that you only want to sync ONE FILE (for the sake of simplicity), called 'example.txt' which is located in the 'Example' folder in my DropBox. The same 'example.txt' is saved locally on the iPhone in the Documents directory of my app.

The dropBox readme file suggest vaguely the following code which I find highly cryptic and can't really see how to load or save a file:

2. Make an request on the rest client:

   [[self restClient] loadMetadata:@"/"];

3. Implement the DBRestClientDelegate methods needed to get the results of the
   particular call you made:

   - (void)restClient:(DBRestClient*)client 
       loadedMetadata:(DBMetadata*)metadata {

       NSLog(@"Loaded metadata!");
   }

   - (void)restClient:(DBRestClient*)client 
       metadataUnchangedAtPath:(NSString*)path {

       NSLog(@"Metadata unchanged!");
   }

   - (void)restClient:(DBRestClient*)client 
       loadMetadataFailedWithError:(NSError*)error {

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

So my (hopefully) simple question is how I can:

  • check if there is an example folder in my dropbox
  • if not, create one and save the example.txt from app documents into this example folder
  • load example.txt
  • once programme quits: save example.txt to DropBox

I can't really find an answer to these quite basic steps in the DropBox docs on the website. The example they've provided I find too confusing... especially as it is only about loading files and not saving them as far as I can see.

I'd be grateful for any help or suggestions of how to go about this.

like image 334
n.evermind Avatar asked Apr 16 '11 08:04

n.evermind


People also ask

How do I upload to Dropbox from iPhone?

Open the Dropbox folder where you'd like to store your files. Tap the "+" (plus sign) at the bottom of your screen. Tap Upload files (Android) or Create or Upload File (iPhone/iPad). Tap Upload (Android) or Upload File (iPhone/iPad).

How do I use Dropbox API?

Register a Dropbox API app To use the Dropbox API, you'll need to register a new app in the App Console. Select Dropbox API app and choose your app's permission. You'll need to use the app key created with this app to access API v2.

Does Dropbox have an open API?

Dropbox supports OAuth 2.0 for authorizing API requests. Find out more in our OAuth guide. Authorized requests to the API should use an Authorization header with the value Bearer <TOKEN> , where <TOKEN> is an access token obtained through the OAuth flow.


2 Answers

Ok, I found this method to save my example.txt file:

-(void) DBupload:(id)sender
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Example.txt"];

    [self.restClient uploadFile:@"NoteBook.txt" toPath:@"/example" fromPath:filePath];
}

Turns out, no need to create a folder, dropbox will do this automatically for you if it doesn't exist.

This is for downloading the same file form dropbox:

-(void) DBdownload:(id)sender
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Example.txt"];
    NSError *error;

    [self.restClient loadFile:@"/example/Example.txt" intoPath:filePath];

    if (filePath) { // check if file exists - if so load it:
        NSString *tempTextOut = [NSString stringWithContentsOfFile:filePath
                                                          encoding:NSUTF8StringEncoding
                                                             error:&error];
    }
}

Hope this helps if you were struggling with a similar question.

like image 72
n.evermind Avatar answered Nov 04 '22 16:11

n.evermind


Into the DBdownload function you can skip the check by implementing the DBRestClientDelegate method loadedFile and loadFileFailedWithError

like image 28
oiledCode Avatar answered Nov 04 '22 15:11

oiledCode