Preview of dropbox file in iOS - 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.

Related

Dropbox SDK Chooser download file

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];

Back Up & Receive Core data SQlite3 database

I want to back up and retrieve sqlite3 database in core data. This is to provide a quick save and restore of users data saved in an app. The plan is to email the database, then open it on the receiving device and all the previously data will magically appear and all is good.
I have read many posts and documentation on the subject but putting it all together is where I could do with some advice.
What I have done so far:
I have managed to email the sqlite3database using the following
NSString *filePath = [[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"] stringByAppendingPathComponent:#"db.sqlite"];
NSError *error = nil;
NSData *fileData = [[NSData alloc] initWithContentsOfFile:filePath options:0UL error:&error];
if (error != nil) {
DebugLog(#"Failed to read the file: %#", [error localizedDescription]);
} else {
if (fileData == nil) {
DebugLog(#"File data is nil");
}
}
MFMailComposeViewController *mailView = [[MFMailComposeViewController alloc] init];
if (mailView != nil) {
mailView.mailComposeDelegate = self;
[mailView setSubject:#"back up database"];
[mailView addAttachmentData:fileData
mimeType:#"application/octet-stream"
fileName:#"db.sqlite3"];
[mailView setMessageBody:#"Database attached"
isHTML:NO];
[self presentViewController:mailView animated:YES completion: NULL];
}
Further research leads me to believe I have to attach other journal files to this to this? iOS: How can I create a backup copy of my core data base? And how to export/import that copy?
Ive logged these documents as db.sqlite db.sqlite-shm db.sqlite-wal
My question is
1. Do I send all these db.sqlite db.sqlite-shm db.sqlite-wal as attachments in the email?
2. What is the procedure to receive these files and copy them into the app.
Simply opening the attachment does not work as it can only be read by certain apps. I then made a Document type in my info.plist and my app appears in the action sheet as an app to receive the document but crashes the app with error incomprehensible archive
So in summary, how can I save and restore my core data
Archive all 3 sqlite files into one zip file and name it Backup.myappbackup or something similar (it’s good to add date of backup to filename). Than you can register extension to be recognized by your application which will allow Mail.app to recognize this extension and open this backup within your app. Next you will unarchive core data files, replace them at your locations and reconfigure core data stack. You can also allow users to put your backups through iTunes Sharing and restore it from your Documents folder.

Overwriting images in iPhoto app

I have a feature in my iOS application. The user has the option to save photo or video to the iPhoto app. When the user presses the 'Save' button, it saves the photo or video again. Is there a way to overwrite the existing file instead of saving it again.
[self writeVideoAtPathToSavedPhotosAlbum:url completionBlock:^(NSURL* assetURL, NSError* error) {
//error handling
if (error!=nil) {
completionBlock(error);
return;
}
//add the asset to the custom photo album
[self addAssetURL: assetURL
withCompletionBlock:completionBlock];
}];
Any suggestion on this? Using checksum?
Looks like you can't, either you disable your Save button after click once or save to document directory and do checksum algorithm. Please check this answer check image exist

How to implement Open In apps for plain text

I would like to provide the ability for users to tap the Action button and up pops the usual share sheet, which should include other apps to the right of the Messages, Facebook, etc icons - applications that can work with .txt files, or just an NSString.
I am currently displaying a Share sheet via UIActivityViewController, which is working great but it does not include other apps in the list. From reading other SO questions I concluded it's only possible to get those other apps to appear if you use UIDocumentInteractionController instead. I looked into creating a .txt file in a temp directory to share that file (instead of just sharing an NSString), but only Mail (no Copy) shows up when I tap the Share button. [Do note that if I run it on a real device not the simulator more apps other than Mail will appear and AirDrop too.] When I tap Mail, the app crashes: Unable to get data for URL: The operation couldn’t be completed. (Cocoa error 260.) Something is wrong with the way I'm creating/retrieving the .txt file.
My questions are:
Why is my code resulting in a crash when attempting to share the .txt file?
How can I get the Copy option to appear in the same Share sheet as the one that includes other apps?
To summarize: I need a share sheet that includes: Copy, AirDrop, Messages, Mail, Facebook, Twitter, Pages, Dropbox, etc for a simple string of text. Thanks!
The following lines of code lie inside my IBAction share button tap function:
UIActivityViewController approach:
UIActivityViewController *activityView = [[UIActivityViewController alloc] initWithActivityItems:#[self.myUITextField.text] applicationActivities:nil];
[self presentViewController:activityView animated:YES completion:nil];
Result:
UIDocumentInteractionController approach:
NSString *fileName = [NSString stringWithFormat:#"%#mytextfile.txt", NSTemporaryDirectory()];
[self.myUITextField.text writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];
NSURL *textFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:#"mytextfile.txt"]];
UIDocumentInteractionController *documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:textFileURL];
[documentInteractionController presentOptionsMenuFromBarButtonItem:sender animated:YES];
Result (will show more apps and AirDrop if I run on a real device):
Example of what I want to obtain - minus the 3 extra options at the bottom:
If I cannot obtain the above screenshot with a string (instead of a photo) for some reason, I am willing to implement it how Dropbox has done it. They added an Open In button at the bottom that presents a different sheet that only shows additional apps. Note that I would still need a Copy option on the original sheet.
Question 1: Why is my code resulting in a crash
Cocoa error 260 is an NSFileReadNoSuchFileError according to the Foundation Constants Reference document. Looking at your code, the only way how I can see that file creation might fail is if self.myUITextField is nil.
I suggest that you check this first. If the property is not nil, then check whether writeToFile:atomically:encoding:error: returns an error.
Question 2: How can I get the Copy option to appear
First, assign a delegate to the controller:
documentInteractionController.delegate = self;
Then implement the following two delegate methods:
- (BOOL) documentInteractionController:(UIDocumentInteractionController*)controller canPerformAction:(SEL)action
{
if (#selector(copy:) == action)
return YES;
else
return NO;
}
- (BOOL) documentInteractionController:(UIDocumentInteractionController*)controller performAction:(SEL)action
{
if (#selector(copy:) != action)
return NO;
// Perform the copy: action
return YES;
}
Both methods are marked deprecated since iOS 6, but they still seem to work in iOS 7. Unfortunately, I have no idea how to implement the copy: action without those two methods - and neither does Apple, or so it seems to me, since they do not offer a replacement, and the official Document Interaction Programming Topics for iOS document still happily refers to the methods without indication that they are deprecated.
Anyway, here's a simple but complete implementation of the second delegate method:
- (BOOL) documentInteractionController:(UIDocumentInteractionController*)controller performAction:(SEL)action
{
if (#selector(copy:) != action)
return NO;
NSStringEncoding usedEncoding;
NSError* error;
NSString* fileContent = [NSString stringWithContentsOfURL:controller.URL
usedEncoding:&usedEncoding
error:&error];
UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setString:fileContent];
return YES;
}

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