Dropbox SDK Chooser download file - ios

I would like to download a file from dropbox into my app i already implemented the Dropbox Drop-in API chooser so i get directed to the dropbox app and can choose a file but i don't know how to download the selected file
- (void)didPressChoose{
[[DBChooser defaultChooser] openChooserForLinkType:DBChooserLinkTypePreview
fromViewController:self completion:^(NSArray *results)
{
if ([results count]) {
fileurl = [[results[0] link] absoluteString];
NSLog(#"got results %#", results);
NSLog(#"link 0 = %#", [[results[0] link] absoluteString]);
} else {
// User canceled the action
}
}];
}
I tried this but i only get a link like "dropbox.com/s/2hro2i45h ..." but for this
[self.restClient loadFile:fileurl intoPath:localDir];
I need something like "/test.txt"

First, the documentation for the Dropbox iOS Chooser lists two different link types. If you want to download the file directly, you should use DBChooserLinkTypeDirect instead of DBChooserLinkTypePreview as you have in your code.
Second, once you have the direct link, you can use a normal HTTP request on the link to download the file content, e.g., using NSURLRequest. The loadFile method you have in your code is for the Dropbox iOS Core SDK which you don't need to use if you're just using the Chooser. That method won't work with the link returned by the Chooser anyway. That method is designed to take a relative path in a user's Dropbox account, but the Chooser is a simpler integration that just gives you a link instead.

I'm not sure but I have this before calling loadFile on restClient
NSString* filePath = [path stringByReplacingOccurrencesOfString:#"dropbox://" withString:#""];
[restClient loadFile:filePath intoPath:destinationPath];

Related

Dropbox API v2 for uploading media files in iOS

I am trying to transition from Dropbox API v1 to v2. My objective is to upload video files to Dropbox in the app folder that Dropbox creates for the apps that do not require access to root folder. I checked this tutorial but have the following confusions:
NSData *fileData = [#"file data example" dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
// For overriding on upload
DBFILESWriteMode *mode = [[DBFILESWriteMode alloc] initWithOverwrite];
[[[client.filesRoutes uploadData:#"/test/path/in/Dropbox/account/my_output.txt"
mode:mode
autorename:#(YES)
clientModified:nil
mute:#(NO)
inputData:fileData]
setResponseBlock:^(DBFILESFileMetadata *result, DBFILESUploadError *routeError, DBRequestError *networkError) {
if (result) {
NSLog(#"%#\n", result);
} else {
NSLog(#"%#\n%#\n", routeError, networkError);
}
}] setProgressBlock:^(int64_t bytesUploaded, int64_t totalBytesUploaded, int64_t totalBytesExpectedToUploaded) {
NSLog(#"\n%lld\n%lld\n%lld\n", bytesUploaded, totalBytesUploaded, totalBytesExpectedToUploaded);
}];
What should be "/test/path/in/Dropbox/account/my_output.txt" in my case, as I do not access the root folder?
Whether the same code is supposed to work for binary files such as mp4 files (it uses UTF8 encoding in the sample code when preparing NSData)?
The "/test/path/in/Dropbox/account/my_output.txt" in the sample is just an example. You should supply the path for the desired location of the uploaded file in the Dropbox account. If you're using an app folder app, the root you supply will automatically be translated into the app folder itself. For example, if you have an app folder at "/Apps/MyAppName", and you want to upload a file named "video.mp4" into a folder called "Videos" in your app folder, you should supply a path value of "/Videos/video.mp4". That will automatically become /Apps/MyAppName/Videos/video.mp4 in the account.
The sample makes an NSData by encoding a string, but you can use the same uploadData to upload a file from any NSData.
Basically this path is /test/path/in/Dropbox/account/my_output.txt.
In dropbox account it will create folders like this test>path>in>Dropbox>account-- then your file will be in account folder. You can replace it with
/yourFolderName/Yourfilename.extension

ios share extension - files from dropbox not loading correctly

When I try to load a shared item, the data that comes back is dropbox's login page - as if I weren't authenticated.
Here is the current method I am using to get the file data:
[itemProvider loadItemForTypeIdentifier:docType options:nil completionHandler:^(NSURL *url, NSError *error) {
//my code
}];
doctype is an appropriate kUTType like kUTTypeImage or kUTTypeText, for example. The mimeType that we write the file with is correct to, per other files. It's the actual content loaded from dropbox (just a login page every time).
I have used other variations of the method (UIImage *, and NSData *) but get the same result for dropbox files.
Our shared extension works fine with files that are downloaded in apps like goodreader or Files. The problem arises when I try to share a file from the dropbox app. It gives me a url that I can put into any browser and it will take me to the file, so the url is not the problem.
Has anyone else faced this?
Here is an example link to a document that does this:
https://www.dropbox.com/s/qxkd1957qf7iq9x/04%20-%20Test%20Document.doc?dl=0
Thank you for your help on this Greg. I found that this worked instead by just changing the url and setting dl=1 like so
From:
https://www.dropbox.com/s/qxkd1957qf7iq9x/04%20-%20Test%20Document.doc?dl=0
To https://www.dropbox.com/s/qxkd1957qf7iq9x/04%20-%20Test%20Document.doc?dl=1

Preview of dropbox file in iOS

I am integrating dropbox with my IOS application. I am able to fetch selected file meta data. But couldn't find the way to show preview after selecting the file. Can someone suggest which API is helpful.
Drop box i am using is : https://www.dropbox.com/developers/dropins/chooser/ios
Below piece of code is called when user wants to select file from dropbox :
- (void)didPressChoose
{
[[DBChooser defaultChooser] openChooserForLinkType:DBChooserLinkTypePreview fromViewController:self
completion:^(NSArray *results)
{
if ([results count]) {
_result = results[0];
//After getting the result, i want to preview the file
} else {
_result = nil;
[[[UIAlertView alloc] initWithTitle:#"CANCELLED" message:#"user cancelled!"
delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil]
show];
}
[[self tableView] reloadData];
}];
}
When you ask for DBChooserLinkTypePreview, the DBChooserResult you get back from the Chooser will have an NSURL link like this:
https://www.dropbox.com/s/toyzur6e0m34t7v/dropbox-logos_dropbox-glyph-blue.png
This link type is meant for direct user interaction, so you can send a user there and Dropbox will display the page with a preview of the file if possible.
Alternatively, you may want to use DBChooserLinkTypeDirect which gives you a direct link like this:
https://dl.dropboxusercontent.com/1/view/969vkzdys770277/Testing/Images/dropbox-logos_dropbox-glyph-blue.png
This is a direct (but temporary) link to the file contents. You can download the file contents programmatically (e.g., see How do I download and save a file locally on iOS using objective C? ) and then do whatever you want with it. For example, you may want to display it in an UIImageView if it's an image, etc.
Also, DBChooserResult contains a thumbnails property with links to thumbnails (if the selected file was an image or video) that might be similarly useful.

Using loadDelta feature in Dropbox IOS

I am using dropbox to upload and download files. I want to be able to be notified when a file changes in a certain folder (or if not possible in the entire dropbox account) so that the user is advised. I don't seem to find any way to do it apart from using the delta feature. I found this code:
-(void)restClient:(DBRestClient *)client loadedDeltaEntries:(NSArray *)entries reset:(BOOL)shouldReset cursor:(NSString *)cursor hasMore:(BOOL)hasMore{
for (DBDeltaEntry *file in entries) {
if(!file.metadata.isDirectory){
NSLog(#"File: %# ", file.metadata.filename );
}else {
NSLog(#"Directory: %# ", file.metadata.filename );
}
}
}
....from this stack overflow question: Using Delta in Dropbox API with iOS
But it gives errors. I found out there is a feature like:
[self.restClient loadDelta:NSString];
But I don't seem to find any examples, and please don't give me a link to the dropbox site which has no sample code at all.

How to display Dropbox Folders after logging in

I am in the process of learning the Dropbox SDK. I want to learn how to display the users Dropbox folders as well as give the user the option to upload the file from my app to their Dropbox account.
I have looked through their example app but it is not giving me the info that I need.
Can someone help me with this please?
I think you might find this tutorial useful.
http://www.nanaimostudio.com/blog/2011/1/20/how-to-synchronize-your-app-data-using-dropbox-api.html
You can call upload method on the DBRestClient object.
NSString* path = [self getDocumentPath];//or however you can obtain the path where your file is stored
[restClientObject uploadFile:#"filename" toPath:#"/" fromPath:path];
//you can choose to upload to the dropbox root directory(above) or a folder of your choice eg toPath:#"/myfolder"
Use loadMetaData for displaying the contents of dropbox folder
[restClientObject loadMetadata:#""];
you also have to implement loadedMetadatafunction. Refer the tutorial above.
-(void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata{
NSMutableArray *list=[[NSMutableArray alloc] init];
for (DBMetadata *child in metadata.contents) {
[list addObject:child];
}
self.arrayFoldersList=[[NSMutableArray alloc] initWithArray:list];
[self.tableView reloadData];
}
[self.restClient loadMetadata:#""];
I think this will help you alot.

Resources