I'm putting a UIDocumentInteractionController into my app, which can download arbitrary file types from a server.
It's there for two purposes:
To allow the user to 'Open In' the file in another app that may be able to view the file.
To preview files that the built-in UIDocumentInteractionController is able to preview.
What I'd like to so is to only attempt to display the preview for file types that the system is able to preview. For example, if it's a zip file, the system can't preview that so I want to go straight to the 'Open In'.
I can't find anywhere a list of the types that are supported for preview.
Here's my code:
self.docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path]];
[self.docController setDelegate:self];
// I'd like to skip this line if the system can't preview the file.
BOOL isOpen = [self.docController presentPreviewAnimated:YES];
if (!isOpen) {
BOOL canOpen = [self.docController presentOpenInMenuFromRect:CGRectMake(300, 300, 100, 100)
inView:self.view
animated:NO];
if (!canOpen) {
// tell user to install an app that's able to open this file.
return;
}
}
The method presentPreviewAnimated: returns a BOOL value (YES if it manages to preview and NO for not managing) so you can:
if (![self.docController presentPreviewAnimated:YES])
{
//present openIn
}
Related
My application required to post an image to Instagram, I used UIDocumentInteractionController to open image file saved in the Documents directory with an extension .igo. Set com.instagram.exclusivegram as the UIDocumentInteractionController's UTI property. That all worked fine, my problem is that when I use
[dic presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
to open the UIDocumentInteractionController, it shows PDF reader, DropBox etc. I have to hide the other options than Instagram or show Instagram only. And also how to identify the Cancel button press in the presented menu.
Why is my UIDocumentInteractionController showing other options?
If you're using some code snippet from the Internet, make sure you implemented the delegate method correctly.
-(UIDocumentInteractionController *)setupControllerWithURL:(NSURL *)fileURL
usingDelegate:(id<UIDocumentInteractionControllerDelegate>) interactionDelegate
{
// if you're creating a new instance here,
// make sure you set the properties correctly
UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
// remember to set it again here
interactionController.UTI = #"com.instagram.exclusivegram";
interactionController.delegate = interactionDelegate;
return interactionController;
}
How can I know if a user cancel the action or proceed to Instagram?
Please refer to the answer of this thread: UIDocumentInteractionController Open Menu Cancelled Callback.
I use this piece of code, but it shows other apps.
NSURL *URL = [[NSBundle mainBundle] URLForResource:#"ABC" withExtension:#"png"];
if (URL)
{
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
[self.documentInteractionController setDelegate:self];
CGRect rect = self.view.frame;
[self.documentInteractionController presentOpenInMenuFromRect:rect inView:self.view animated:YES];
How can I show the WhatsApp icon only?
Here are the WhatsApp developer docs: https://www.whatsapp.com/faq/iphone/23559013
Here is the relevant bit:
Alternatively, if you want to show only WhatsApp in the application
list (instead of WhatsApp plus any other public/*-conforming apps) you
can specify a file of one of aforementioned types saved with the
extension that is exclusive to WhatsApp:
images - «.wai» which is of type net.whatsapp.image
videos - «.wam» which is of type net.whatsapp.movie
audio files - «.waa» which is of type net.whatsapp.audio
So in order to share an image you would set the UTI of your UIDocumentInteractionController to net.whatsapp.image and append the extension .wai to your filepath.
Additionally, this question may have some helpful example code.
Heres the UIDocuemtnInteractionController from my application(does not show mail option)
Here the one that Apples sample project uses
Here are the respective codes
My application
docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
[docInteractionController presentOpenInMenuFromBarButtonItem:(UIBarButtonItem*)sender animated:YES];
Apple Sample Project
NSURL *fileURL;
if (cellIndexPath.section == 0)
{
// for section 0, we preview the docs built into our app
fileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:documents[cellIndexPath.row] ofType:nil]];
}
else
{
// for secton 1, we preview the docs found in the Documents folder
fileURL = [self.documentURLs objectAtIndex:cellIndexPath.row];
}
self.docInteractionController.URL = fileURL;
[self.docInteractionController presentOptionsMenuFromRect:longPressGesture.view.frame
inView:longPressGesture.view
animated:YES];
WHAT SHOULD I DO TO GET THE MAIL OPTION?
To provide the Mail option, -presentOpenInMenuFromBarButtonItem: needs to be -presentOptionsMenuFromRect:
As per the Apple Docs on UIDocumentInteractionController
For -presentOpenInMenuFromBarButtonItem:animated: it says:
This method is similar to the
presentOptionsMenuFromBarButtonItem:animated: method, but presents a
menu restricted to a list of apps capable of opening the current
document. This determination is made based on the document type (as
indicated by the UTI property) and on the document types supported by
the installed apps.
...
If there are no registered apps that support opening the document, the
document interaction controller does not display a menu.
So:
To present options to open the file, use -presentOpenInMenuFromBarButtonItem:
To present all possible options applicable on the file, use -presentOptionsMenuFromBarButtonItem: or the generic -presentOptionsMenuFromRect:
Also... for any file, it would be better to specify the UTI type:
Example:
docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
//[docInteractionController setDelegate:self];
[docInteractionController setUTI:#"public.data"];
[docInteractionController presentOptionsMenuFromBarButtonItem:(UIBarButtonItem*)sender
animated:YES];
//or a generic method
//[docInteractionController presentOptionsMenuFromRect:sender.frame
// animated:YES];
I'm developing a PhoneGap Build application, but needed a way to prompt my user to open local files in Adobe Reader (inAppBrowser will not work for my situation). I found a 3rd party plugin for PhoneGap that does what I need. However it only does it for remote .pdf files. I have figured out how to alter the plugin code to work with local files on the iPad/iPhone but it seems to close my application when the user opens the pdf in Adobe Reader.
I know almost nothing about objective C, Xcode, etc.
Desired functionality is:
User navigates my app searching or browsing for files
Finds desired file
Selects file
User is presented with "open with" options of which one is Adobe reader
User selects Adobe, PDF opens
User returns to my app in location they left it
I have it all working except on step #6 above my app seems to be closed. If I press the home button twice app show up at the bottom of iPad but when selected app starts from closed state showing splash screen. I'd like it to be where the user left it.
I believe the relevant code is
NSURL *fileURL = [NSURL fileURLWithPath:localFile];
UIDocumentInteractionController *controller = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
[controller retain];
controller.delegate = self;
controller.UTI = uti;
CDVViewController* cont = (CDVViewController*)[ super viewController ];
//CGRect rect = CGRectMake(0, 0, cont.view.bounds.size.width, cont.view.bounds.size.height);
CGRect rect = CGRectMake(0, 0, 1024, 768);
[controller presentOpenInMenuFromRect:rect inView:cont.view animated:YES];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: #""];
[self writeJavascript: [pluginResult toSuccessCallbackString:callbackID]];
[callbackID release];
[path release];
[uti release];
Where this line
[controller presentOpenInMenuFromRect:rect inView:cont.view animated:YES];
prompts the user to select Adobe. Is there a parameter I can pass here to not have my application shut down once the user selects to open the pdf in Adobe?
Thanks!
The plugin I have altered in its original state can be found and discussed here:
http://www.tricedesigns.com/2012/08/15/open-with-in-ios-phonegap-apps/
In the app that I am working I have files to be downloaded from a server and store it locally in the app's sandbox, then open it in iBooks using UIDocumentInteractionController.
I have done it all the downloading, saving, and opening it to iBooks. But, there are times that when I download a file then open it to iBooks it is not implementing, iBooks is not opening but the delgates are read from willBeginSendingToApplication: to didEndSendingToApplication: to documentInteractionControllerDidDismissOpenInMenu: (i put logs on it to check). Then if I restart the app it opens, then not open again, it is not consistent. What could be the problem?
Here my code for opening it to iBooks:
NSURL *url = [NSURL fileURLWithPath:filePath1];
DocController = [UIDocumentInteractionController interactionControllerWithURL:url];
DocController.delegate = self;
[DocController presentOpenInMenuFromRect:openIt.frame inView:menu animated:YES];
With UIDocumentInteractionControllerDelegate added in your .h, you can add this function :
- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller
{
return self; //return the controller who open the doc
}