open in ibooks from a url crashes - ios

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://....'.

Related

Automatic open WhatsApp directly using UIDocumentINteractionController without presenting the other apps

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.

UIDocumentInteractionController presentOptionsMenuFromBarButtonItem don't open app

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

UIDocumentInteractionController share via Instagram (and ONLY Instagram)

When I use UIDocumentInteractionController to allow users to share via Instagram, it does work, it brings up the option for "open with" and "Instagram" as one of the options... the problem is that it also displays many other apps like "Facebook" and "Twitter"...
Is there any way I can make it only give the option of opening in the instagram app?
Instagram claims there is a way to do this: http://instagram.com/developer/iphone-hooks/ but they mention this:
"Alternatively, if you want to show only Instagram in the application list (instead of Instagram plus any other public/jpeg-conforming apps) you can specify the extension class igo, which is of type com.instagram.exclusivegram."
but I honestly have no clue what this part means, "extension class igo"
My code:
UIImage *imageToUse = [UIImage imageNamed:#"imageToShare.png"];
NSString *documentDirectory=[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *saveImagePath=[documentDirectory stringByAppendingPathComponent:#"Image.ig"];
NSData *imageData=UIImagePNGRepresentation(imageToUse);
[imageData writeToFile:saveImagePath atomically:YES];
NSURL *imageURL=[NSURL fileURLWithPath:saveImagePath];
docController = [UIDocumentInteractionController interactionControllerWithURL:imageURL];
docController.delegate = self;
docController.annotation = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:#"This is the users caption that will be displayed in Instagram"], #"InstagramCaption", nil];
docController.UTI = #"com.instagram.exclusivegram";
[docController presentOpenInMenuFromRect:CGRectMake(1, 1, 1, 1) inView:self.view animated:YES];
Turns out to get the doc controller to only open in Instagram you just save your png or jpg file in format ".igo" file type extension, (Maybe stands for Instagram-Only?)
Changed the third line of code in my code to read this instead:
NSString *saveImagePath=[documentDirectory stringByAppendingPathComponent:#"Image.igo"];
(With "igo")
and then it worked! :)
Lower version of iOS 13:
NSString *saveImagePath=[documentDirectory stringByAppendingPathComponent:#"Image.igo"];
After iOS 13:
NSString *saveImagePath=[documentDirectory stringByAppendingPathComponent:#"Image.ig"];

IBAction Open Local HTML File

I've got a view based application that one is at last just a single WebView ... with a Tabbar with some Icons.
So i need to add a small icon for go back to the localHTML File home.html.
Maybe could someone give me some informations how to handle it without change the view to a new one?
Here is how my other IBActions looks like
- (IBAction)news_button:(id)sender; {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"http://www.google.com"]];
}
Other option could be open a Website but not in safari ... like it does at the moment.
Thanks for sharing! :)
With the line above you are opening it in Safari right now.
You can get path to the local file if it is in main bundle catalog with
NSString *filePath = [[NSString stringWithFormat:#"%#/%#", [[NSBundle mainBundle] resourcePath], #"home.html"] retain];
So why not use
[yourWebView loadRequest:[NSURLRequest initWithURL:[NSUrl initWithString:filePath]]];

how to find out all the applications that can open the same content type in iPad?

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.

Resources