I use UIDocumentInteractionController presentOptionsMenuFromBarButtonItem to open PDF in Adobe Reader app, send it with Mail and print it. Mail and print work fine but I'm unable to open any other app. I tried presentOpenInMenuFromBarButtonItem and UIActivityViewController but neither of them do all I need.
I tried to open Adobe Reader with documentInteractionController: willBeginSendingToApplication: delegate but i'm unable to find how I can pass the pdf to the app. Is it possible?
If not, is there a another way to open PDF in Adobe Reader app, send it with Mail and print it?
Thanks
Normally this is how I do:
NSURL *documentsDirectoryPath = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]];
NSURL *URL =[documentsDirectoryPath URLByAppendingPathComponent:#"your pdf"];
UIButton *button = (UIButton *)nil;
self.documentInteractionController.delegate=self;
if (URL) {
// Initialize Document Interaction Controller
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
// Configure Document Interaction Controller
[self.documentInteractionController setDelegate:self];
//preview
[self.documentInteractionController presentPreviewAnimated:YES];
// Present Open In Menu
[self.documentInteractionController presentOpenInMenuFromRect:[button frame] inView:self.view animated:YES];
}
Related
I'm developping share image to the other apps, including WhatsApp.
I'm using this code and it works.
if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:#"whatsapp://app"]]){
UIImage *image = [self processImage:sender];
NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/whatsAppTmp.wai"];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:savePath atomically:YES];
_documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
_documentInteractionController.delegate = self;
_documentInteractionController.UTI = #"net.whatsapp.image";
[_documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}else {
[self showAlertTitle:#"WhatsApp not installed." message:#"Your device has no WhatsApp installed."];
}
The problem is, when I run this code, documentInteractionController presents the other options apps like this image, so I must pick WhatsApp first before it open WhatsApp application.
Can I select Whatsapp App to share the file without presenting the menu for choosing? In other words, can I avoid presentOpenInMenuFromRect ?
I`m using iOS 9 and this issue happen also with my Instagram post
Yes. you can write this code. After run code do the following.
Step:1 Add LSApplicationQueriesSchemes in Info.plist
Step:2 Add whatsapp in items.
I am trying to open ibooks from webview. When a user taps on a webview, I am showing all the apps they can use to open that certain pdf. But when i click on ibooks, app closes and nothing happens.
-(void) handleTap{
NSURL *pdfURL =[NSURL fileURLWithPath:self.url];// url is like https://www....
UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:pdfURL];
//present a drop down list of the apps that support the file type, click an item in the list will open that app while passing in the file.
[docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
Thank you
NSURL *pdfURL =[NSURL fileURLWithPath:self.url];// !!! dpfURL = nil;
Please check pdfURL, it's value must be nil. Because the argument of method fileURLWithPath: should likes 'file:///...' not likes 'https://....'.
I'm sending an email, with a PDF attachment, while using UIDocumentInteractionController, like this:
I start by showing the PDF file
-(void)showPDFFile
{
NSURL *url = [NSURL fileURLWithPath:_filePath];
if (url) {
_documentInteractionController =
[UIDocumentInteractionController interactionControllerWithURL:url];
[_documentInteractionController setDelegate: self];
[_documentInteractionController presentPreviewAnimated:YES];
}
}
- (UIDocumentInteractionController *)setupControllerWithURL:(NSURL *)fileURL
usingDelegate:(id <UIDocumentInteractionControllerDelegate>)interactionDelegate {
UIDocumentInteractionController *interactionController =
[UIDocumentInteractionController interactionControllerWithURL: fileURL];
[interactionController setDelegate: interactionDelegate];
return interactionController;
}
When the PDF file is shown, the user clicks the "Export" option and the iOS's "Open with" view appears.
Clicking the email now opens a View Controller ready to send an email.
How would I set the To: CC/BCC and Subject fields programatically?
Thank you!
You can assign the mail's subject by using the UIDocumentInteractionController name property:
_documentInteractionController.name = #"My custom mail subject";
Unfortunately this is the only attribute I've figured out that can be configured via UIDocumentInteractionController.
Unfortunately Florians answer didn't work for me. I had to copy the file locally and then set the URL to the local file. Setting the name only changed the title on the preview, not the filename or subject in the email.
i.e.
NSFileManager* fileManager = [NSFileManager defaultManager];
NSError* err = nil;
NSString* newPath = [appDocumentsFolder stringByAppendingPathComponent:name];
if (![fileManager copyItemAtPath:[[NSURL URLWithString:path] path] toPath:newPath error:&err]) {
// handle error
}
NSURL *fileURL = [NSURL fileURLWithPath:newPath];
_controller = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
Is it possible to open a pdf file from an app itself into iBooks.
OR
any 3rd party solutions, that can use any pdf file and create a flip book thing in an app itself.
You can indeed open a PDF file from an app into iBooks (or any other installed app that can read PDF files).
To do this, you can use the UIDocumentInteractionController
NSString *pdfPath = #"path to your pdf file";
NSURL *url = [NSURL fileURLWithPath:pdfPath];
UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:url];
docController.delegate = self;
[docController retain];
// docController is released when dismissed (autorelease in the delegate method)
BOOL isValid = [docController presentOpenInMenuFromRect:readPdfButton.frame inView:self.view animated:YES];
if (!isValid) {
NSString * messageString = [NSString stringWithFormat:#"No PDF reader was found on your device. In order to consult the %#, please download a PDF reader (eg. iBooks).", yourPDFFileTitle];
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"Error" message:messageString delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
...
// UIDocumentInteractionController delegate method
- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller {
[controller autorelease];
}
The UIDocumentInteractionController will open a popover (on iPad) or an action sheet (on iPhone) with a choice of all the PDF capable apps that are installed on the device so the user can choose his favorite one he wants to read the PDF with.
There are also a few decent PDF reader libraries you can use to open the PDF directly in your app, rather than having to switch application, one of which is VFR PDF Reader/Viewer for iOS
how to find out all the applications that can open the same content type in iPad? Is there any API to find out this?
You can use UIDocumentInteractionController as the following:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"pdfName" ofType:#"pdf"]];
_docController = [[UIDocumentInteractionController interactionControllerWithURL:url] retain];
BOOL isValid = [_docController presentOpenInMenuFromRect:CGRectMake(980, 52, 0, 0) inView:self animated:YES];
It will open a menu with all the options to read that file. You can also use its delegate UIDocumentInteractionControllerDelegate to have more control of it.