I have local pdf files in a document directory. My code calls each one to display using WKWebview
or PDFViewer
, but it only shows on an iPhone XR simulator, but not on any other simulator devices.
Any devices which are older than XR, it shows an error:
libMobileGestalt MobileGestalt.c:890: MGIsDeviceOneOfType is not supported on this platform.
Devices with XS, XS Max, it shows the error:
failed to open pdf ...
as seen in the code.
Does anyone know why?
I use Xcode 10.2.
Here is the code that I use for WKWebviewer
@IBOutlet weak var webView: WKWebView!
//only work on XR, don't know why
override func viewDidLoad() {
super.viewDidLoad()
let documentDir = FileManager.SearchPathDirectory.documentDirectory
let userDir = FileManager.SearchPathDomainMask.userDomainMask
let paths = NSSearchPathForDirectoriesInDomains(documentDir, userDir, true)
if let dirPath = paths.first
{
let pdfURL = URL(fileURLWithPath: dirPath).appendingPathComponent("my file.pdf")
do {
//only work in Xr
let data = try Data(contentsOf: pdfURL)
webView.load(data, mimeType: "application/pdf", characterEncodingName:"", baseURL: pdfURL.deletingPathExtension())
//not working for Xs, Xs Max... later iphone
//webView.loadFileURL(pdfURL, allowingReadAccessTo: pdfURL)
//webView.load(URLRequest(url:pdfURL))
//webView.loadFileURL(pdfURL, allowingReadAccessTo: pdfURL)
print("open pdf file....")
}
catch {
print("failed to open pdf \(dirPath)" )
}
return
}
and for PDFViewer
:
let documentDir = FileManager.SearchPathDirectory.documentDirectory
let userDir = FileManager.SearchPathDomainMask.userDomainMask
let paths = NSSearchPathForDirectoriesInDomains(documentDir, userDir, true)
if let dirPath = paths.first
{
let pdfURL = URL(fileURLWithPath: dirPath).appendingPathComponent("myfile2.pdf")
view.addSubview(pdfView)
pdfView.autoresizesSubviews = true
pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleLeftMargin]
pdfView.displayDirection = .vertical
//pdfView.autoScales = true
pdfView.displayMode = .singlePageContinuous
pdfView.displaysPageBreaks = true
if let document = PDFDocument(url: pdfURL) {
pdfView.document = document
}
pdfView.maxScaleFactor = 4.0
pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit