iOS - get document's URL when QLPreviewController is closed - ios

I have this code which previews the PDF file:
QLPreviewController *previewController=[[QLPreviewController alloc]init];
previewController.delegate=self;
previewController.dataSource=self;
[self presentModalViewController:previewController animated:YES];
Now, when I close QLPreviewController or previewController this method is called:
- (void)previewControllerDidDismiss:(QLPreviewController *)controller {
NSLog(#"You closed the document");
}
Now, how I can return the document's or Pdf's URL when I call this method:
- (void)previewControllerDidDismiss:(QLPreviewController *)controller {
//here I need to be able to get the PDF's URL or Data when is closed
}
Can anyone please help?
Many thanks.

Check documentation here. There is another method, that responds to user actions and returns needed URL:
- (BOOL)previewController:(QLPreviewController *)controller
shouldOpenURL:(NSURL *)url
forPreviewItem:(id<QLPreviewItem>)item

Related

Unbalanced calls to begin/end appearance transitions for QLRemotePreviewContentController

I'm using Objective-C. I want to open a local file with quick look. And I have this code in my mainViewController:
- (IBAction)open:(id)sender {
QLPreviewController *myQlPreViewController = [[QLPreviewController alloc]init];
myQlPreViewController.delegate = self;
myQlPreViewController.dataSource = self;
[myQlPreViewController setCurrentPreviewItemIndex:0];
[self presentViewController:myQlPreViewController animated:YES completion:nil];
}
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
return 1;
}
- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
return [NSURL URLWithString:self.filePath];
}
But it shows a gray background and file name on it. How can I fix it to show the file content?
Thanks in advance!
In fact, it's because quick look can't open that file. So, it can only show a file name on the screen.

UIDocumentInteractionController remove Actions Menu

I've been working with the Apple sample code for viewing documents from here:
https://developer.apple.com/library/ios/samplecode/DocInteraction/Listings/ReadMe_txt.html
I have removed all the bits I don't need and got it working pretty much how I would like it to. The problem is I don't want users to have access to the "Actions" menu on the top right of the Document Controller. This appears every time you select a document from the list:
Ideally I would like to remove the button all together, though if I could disable it or disable all the options inside it that would also suffice. I found this question:
Open in + UIDocumentInteractionController : how to filter options in SDK iOS 6 (canPerformActions is deprecated)
But I couldn't figure out how to use the suggestion to disable the options inside the menu. I have uploaded the modified sample code here:
http://plasma.servebeer.com/DocSampleCode.zip
One final note is this will not be going on the App Store it is for private, personal use, so if there is an unofficial way then I would be interested in knowing that too.
Any help would be greatly appreciated, thanks in advance.
Plasma
Use UINavigationControllerDelegate
#interface DITableViewController () <UIDocumentInteractionControllerDelegate, UINavigationControllerDelegate>
Assign navigationController delegate to self
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.delegate = self;
}
Change documentInteractionControllerViewControllerForPreview
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)interactionController {
return self.navigationController;
}
Add this UINavigationControllerDelegate method
// Called when the navigation controller shows a new top view controller via a push, pop or setting of the view controller stack.
- (void)navigationController:(UINavigationController*)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ([viewController isKindOfClass:[QLPreviewController class]]) {
viewController.navigationItem.rightBarButtonItem = nil;
}
}
Update for MP4 files
In MP4 files the action button is on the UIToolbar
- (void)navigationController:(UINavigationController*)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ([viewController isKindOfClass:[QLPreviewController class]]) {
viewController.navigationItem.rightBarButtonItem.customView = [[UIView alloc] init];
UIBarButtonItem *item = viewController.toolbarItems.firstObject;
item.customView = [[UIView alloc] init];
}
}
N.B. This might not work in future versions of iOS
After creating QLPreviewController class you would need to set rightBarButtonItem to nil. Code snippet:
QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.navigationItem.rightBarButtonItem = nil;
I did download project and after execution "Action" button was shown not in the top navigation item, but in the toolbar. Then in this case you would need to subclass QLPreviewController and override viewWillAppear as shown below.
#implementation ExViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSMutableArray *a = [NSMutableArray arrayWithArray:#[]];
for (NSUInteger i = 0; i < self.toolbarItems.count; i++) {
if (i == 0) {
continue;
}
[a addObject:self.toolbarItems[i]];
}
}
#end
If you want to hide button the give answers will not work for iOS 10.0 and above in Swift language. You can use WKWebView. Hope it will save your time.

Wireless accessory configuration in iOS: EAWiFiUnconfiguredAccessoryBrowser will detect unconfigured accessories only once

I am using EAWiFiUnconfiguredAccessoryBrowser to detect EAWiFiUnconfiguredAccessory. The code to start the accessory search it's the following:
- (void)viewDidLoad {
[super viewDidLoad];
if (_accessories == nil) {
_accessories = [[NSMutableArray alloc] init];
}
if (_browser == nil) {
_browser = [[EAWiFiUnconfiguredAccessoryBrowser alloc] initWithDelegate:self queue:nil];
_browser.delegate = self;
}
}
Unfortunately it does find accessories only the first time the View loads. If I go back to the previous view and then reload the view it does not find them.
I tried:
recreating the browser accessory and restarting the search (does not work)
stopping the search and restarting it (does not work)
This is the latest code I got (refer to this together with the code above):
- (void) viewWillAppear:(BOOL)animated{
NSLog(#"view will appear");
if (_accessories != nil) {
[_accessories removeAllObjects];
}
[self.tableView reloadData];
[self initializeBrowswerAndStartSearch];
}
- (void) initializeBrowswerAndStartSearch{
if (_browser != nil) {
[_browser stopSearchingForUnconfiguredAccessories];
}
[_browser startSearchingForUnconfiguredAccessoriesMatchingPredicate:nil];
}
- (void) viewWillDisappear:(BOOL)animated{
[_browser stopSearchingForUnconfiguredAccessories];
}
It seems that the accessory list information is cached somewhere within the APP. If I restart the APP it will find them so I guess there is something that I am missing.
Any help?
so i have the same problem..you should use the unconfiguredAccessories array. Also, try keeping the instance of the browser alive. If you discover the device once, and you re-instantiate the browser, you wont find it again
EAWiFiUnconfiguredAccessoryBrowser has issues,and doesn't provide reliable result in certain use cases. i think you should try this
- (void) viewWillAppear:(BOOL)animated{
NSLog(#"view will appear");
if (_accessories != nil) {
[_accessories removeAllObjects];
}
[self.tableView reloadData];
[self initializeBrowswerAndStartSearch];
}
below method makes browser object nil and reinitialises it, in this case browser object will always return you updated(i.e, proper) values . it worked perfectly for me.
-(void) initializeBrowswerAndStartSearch
{
// Make EAWiFiUnconfiguredAccessoryBrowser object nil and reinitiate ,start searching again
_browser = nil;
_browser = [[EAWiFiUnconfiguredAccessoryBrowser alloc] initWithDelegate:self queue:nil];
[_browser startSearchingForUnconfiguredAccessoriesMatchingPredicate:nil];
}
anytime you feel EAWiFiUnconfiguredAccessoryBrowser isn't providing proper result , try this.
I also have this issue. So I build a singleton called WAC service, then you can keep this singleton alive during the app life cycle. Anywhere you want to load the unconfigured accissories. Just load it from [_browser unconfiguredAccessories].

UIDocumentInteractionController stopped working iOS 8

I have a PDF file saved in the document directory. The path to the file is stored in a NSString property 'self.URL'.
Here is my code to present the activity items:
-(void)presentDocumentInteractionController{
self.docController = [UIDocumentInteractionController interactionControllerWithURL:self.URL];
self.docController.delegate = self;
[_docController setUTI:#"com.adobe.pdf"];
[_docController presentOptionsMenuFromBarButtonItem:self.activityBarButton animated:YES];
}
Before iOS 8 this code worked fine. The user was presented with a list of items such as print, copy and email to choose from. After upgrading to iOS 8 /XCode 6, I'm getting this runtime error (it doesnt crash the app):
Unknown activity items supplied: (
"<UITextViewPrintFormatter: 0x7f908ba53ca0>"
)
How can I solve this problem?
I have the same problem and have switched to using UIActivityViewController. However this makes the apps capable of opening the PDF no longer show up, so maybe that's not what you want.
Minimal Solution:
If you want to do minimal work, you don't even need to read your PDF into NSData, use a NSURL as activity item and iOS seems to know what to do:
- (void)share:(id)sender
{
UIActivityViewController *activity =
[[UIActivityViewController alloc] initWithActivityItems:#[self.URL]
applicationActivities:nil];
if ([activity respondsToSelector:#selector(popoverPresentationController)]) {
activity.popoverPresentationController.barButtonItem = <# BAR BUTTON ITEM #>;
}
[self presentViewController:activity animated:YES completion:NULL];
}
Original Answer:
Make your view controller adhere to the UIActivityItemSource protocol, then you can do:
- (void)share:(id)sender
{
self.pdfData = [NSData dataWithContentsOfURL:self.URL];
UIActivityViewController *activity = [[UIActivityViewController alloc] initWithActivityItems:#[self] applicationActivities:nil];
if ([activity respondsToSelector:#selector(popoverPresentationController)]) {
activity.popoverPresentationController.barButtonItem = <# BAR BUTTON ITEM #>;
}
[self presentViewController:activity animated:YES completion:NULL];
}
Adhering to the protocol if you have a PDF file is relatively simple. You can of course optimize and return smaller NSData and even a preview image, but minimally do this:
- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
return _pdfData;
}
- (id)activityViewController:(UIActivityViewController *)activityViewController
itemForActivityType:(NSString *)activityType
{
return _pdfData;
}
- (NSString *)activityViewController:(UIActivityViewController *)activityViewController
subjectForActivityType:(NSString *)activityType
{
return self.title;
}
- (NSString *)activityViewController:(UIActivityViewController *)activityViewController
dataTypeIdentifierForActivityType:(NSString *)activityType
{
return #"com.adobe.pdf";
}
Use
- (BOOL)presentOpenInMenuFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated;
Instead of
- (BOOL)presentOptionsMenuFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated;
This solved the problem for me:
ObjC:
dispatch_async(dispatch_get_main_queue(), ^() {
[_docController presentOptionsMenuFromRect:button.bounds inView:button animated:YES];
});
Swift:
if let docController = UIDocumentInteractionController(URL: url) {
dispatch_async(dispatch_get_main_queue()) {
docController.presentPreviewAnimated(true)
}
} else {
// handle nil docController
}
I don't use swift so this code might not work.

Application tried to present inside popover with transition style other than UIModalTransitionStyleCoverVertical before printing

I am trying to print a txt file from within a iPad 8.x application. So, I have this code:
- (void)onOpenWith:(UIButton *)theButton path:(NSString *)path
{
NSURL *URL = [NSURL fileURLWithPath:path];
if (URL) {
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
self.documentInteractionController.delegate = self;
[self.documentInteractionController presentPreviewAnimated:YES];
}
}
#pragma mark - UIDocumentInteractionControllerDelegate
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
return self;
}
- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller
{
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller
{
return self.view.frame;
}
Now everything is going as expected, I see a preview of the file, then I touch the icon on the top right corner of the screen and I am able to share the document in those applications that can handle it. However, if I touch PRINT I get this error message:
Application tried to present inside popover with transition style
other than UIModalTransitionStyleCoverVertical
and the app crashes. Sure, i understand it, but to which viewcontroller should I apply this transition? I have no control on the popover showing the print dialog...
In iOS 7 (real iPad not simulator) everything works...
Can anybody help me?
Thanks
Fabio
I also came across this issue, and the solution was that I had to build and run my app with Xcode 6 installed.
On my older machine, I have Xcode 5.1.1, and when run from there, this same issue appeared, and some more issues from this view (like cannot dismiss mail controller when opened from the top right corner).

Resources