Objective C: Popover Dismissed Without Opening the File to Another App - ios

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
}

Related

Is it possible to view a PDF file from an URL link using Quick Look Framework

As the title says, I need to display a PDF file stored in a remote server without downloading it on the device just by using an URL link. Is it possible to do it by using the Quick Look framework?
I am using this code below:
- (void)openDocument {
QLPreviewController *docPreviewController = [[QLPreviewController alloc] init];
[docPreviewController setDataSource:self];
[docPreviewController setDelegate:self];
[docPreviewController setCurrentPreviewItemIndex:sender.tag];
[self.destinationViewController presentViewController:docPreviewController animated:true completion:nil];
}
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
return 1;
}
- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
return [NSURL fileURLWithPath:#"http://www.domain.com/file.pdf"];
}
But I have this problem in the console:
UIDocumentInteractionController: invalid scheme https. Only the file scheme is supported.
No it's not supported by current QLFramework.
All quicklook supported items should conforms to "QLPreviewItem" protocol.
According to QL's document
The methods in the QLPreviewItem protocol are also declared as a category on the NSURL class. As a result, you can use NSURL objects directly as preview items—provided that you want to use the default titles of those items.
QLPreviewItem protocol contains previewItemURL attribute, which is a URL type. But the document tells us directly:
The value of this property must be a file-type URL.
In other words, it doesn't accept other url schema.
I have resolved by using UIDocumentInteractionController
UIDocumentInteractionController *viewer = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path]];
viewer.delegate = self;
[viewer presentPreviewAnimated:YES];

Open Instagram only in UIDocumentInteractionController

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.

Share image through WhatsApp, just show WhatsApp icon

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.

UIDocumentInteractionController Does Not Show Mail Option

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

UIDocumentInteractionController opening pdf in iBooks crashes

I have a test PDF file that I want to open in iBooks through my app. I saved it in my temp directory and I use this code to load it to iBooks:
NSURL *targetURL = [NSURL fileURLWithPath:tempFullPath];
NSLog(#"Path is %#", tempFullPath);
UIDocumentInteractionController *controller = [UIDocumentInteractionController interactionControllerWithURL:targetURL];
controller.delegate = self;
controller.UTI = #"com.adobe.pdf";
[controller presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES];
The menu pops up just fine but when I tap the iBooks button the app crashes and hangs my Xcode.
The path to the file I get in my NSLog output is like this:
Path is /private/var/mobile/Applications/65EC4182-A79B-431C-9E74-BD72D91A31AB/tmp/TestFile.pdf
What am I doing wrong? Thanks in advance!
The use of a UIDocumentInteractionController requires that you keep a reference around until it is complete. This means you should use an instance variable, not a local variable. Implement the proper delegate methods so you can reset the ivar when you are done with the controller.
Enabling zombies will help debug such an issue. Most likely you will see that a message is being sent to a deallocated object (your controller).

Resources