I am trying to preview pdf file in QLPreviewController and using the below code. It works fine on iOS7 and for other type of files (JPG/PNG) on iOS8 as well but when I try to open pdf it shows blank page instead content on iOS8. Its weird that it still shows name of pdf in title view.
Code:
QLPreviewController *previewer = [[QLPreviewController alloc] init];
previewer.dataSource = self;
previewer.currentPreviewItemIndex = 0;
[self presentViewController:previewer animated:NO completion:nil];
And QLPreviewControllerDataSource methods:
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
return 1;
}
- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
return [NSURL fileURLWithPath:self.pdfUrl];
}
I had a problem (and still have) with presenting documents from bundle (xCode 9.2, iOS 11). It only displays images. For other documents (pdf, key, rtf, pages, docx, txt ...) it only shows the name and not the content. I'm presenting controller modally and the datasource is implemented like this:
Of course, it shows that file CAN be previewed, but it doesn't preview it. Currently I'm solving this by copying file to documents directory and passing that url to datasource.
There seems to be a bug when placing a QLPreviewController inside of a UINavigationController. It just shows up as a black view even if the document is loaded.
The solution is to not use a navigation controller. QLPreviewController overrides the navigation bar anyways.
I had the same problem, and I needed to call
reloadData()
after I set thedataSource
to a new value.Works fine with me on iOS8.1
Make sure you download the doc file into a proper file path
and load it into your QLPreviewControl
And make sure that you open the QLPreviewControl with presentViewController don't push it into your navigation controller
This is actually a known issue in iOS 8 Beta 5.
See the URL under QuickLook https://developer.apple.com/library/prerelease/ios/releasenotes/General/RN-iOSSDK-8.0/
There are probably two things here involved.
For those implementing QLPreviewController in a UINavigationController, there's probably an issue there.
For those still having the issue without the UINavigationController, read on.
In iOS 8.0, file path to Data has changed. So if you are using older bundle path to access your resources you might have an issue.
We did investigate this and it turns out:
For instance, our problem was that our PDF file was opened from an outside application using the traditional "Open In".
This was resulting in the file being copied to the Inbox directory inside the Documents directory of the App.
The file IS displayed correctly on the simulator, regardless of the QLPreviewController being embedded in a UINavigationController or not.
The file IS NOT displayed correctly on a real iOS Device.
Before sending the file URL to the QLPReviewController, we did copy the file to the root of the Documents directory, passing this new URL to the QLPreviewController. And then bingo, it worked.
So in order to stop this issue, make sure you copy the resource in a directory you actually own. Until someone can explain how to play around with the rights on the Inbox (probably shared) directory.