I am creating an app to display a PDF file selected by UIDocumentPickerViewController
for iPads.
Although the following code works fine with simulators of Xcode, for some reason, it won't work with my test iPad (iPad Mini, 5th gen). I can select a file, and its URL seems to be obtained appropriately; however, the PDF file is not displayed.
I would appreciate it if you could help me figure out what is happening and how to fix this.
import UIKit
import PDFKit
class ViewController: UIViewController, UIGestureRecognizerDelegate, UIDocumentPickerDelegate {
@IBOutlet weak var pdfView: PDFView!
override func viewDidAppear(_ animated: Bool) {
//document picker
let documentPicker = UIDocumentPickerViewController(documentTypes: ["com.adobe.pdf"], in: .import)
documentPicker.delegate = self
documentPicker.allowsMultipleSelection = false
present(documentPicker, animated: true, completion: nil)
}
internal func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
print(url)
if let pdfDocument = PDFDocument(url: url)
{
pdfView.autoScales = true
pdfView.document = pdfDocument
pdfView.displayDirection = .horizontal
pdfView.usePageViewController(true, withViewOptions: [UIPageViewController.OptionsKey.interPageSpacing: 20])
} else{
print ("error, which is what I am seeing in my iPad now")
}
}
override func viewDidLoad(){
super.viewDidLoad()
}
details of the environment:
- iPad mini: 5th gen, iOS 14
- Xcode: 14
- Simulators: iOS 14 (I tried the majority of simulators and the app works well with them all)