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:
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.
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).
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.
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.
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.
Into the DBdownload function you can skip the check by implementing the DBRestClientDelegate method loadedFile and loadFileFailedWithError
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With