У меня есть локальные файлы PDF в каталоге документов.Мой код вызывает каждый из них для отображения с использованием WKWebview
или PDFViewer
, но он отображается только на симуляторе iPhone XR, но не на любых других устройствах симулятора.
На всех устройствах старше XR отображается сообщение об ошибке:
libMobileGestalt MobileGestalt.c:890: MGIsDeviceOneOfType is not supported on this platform.
Для устройств с XS Max X отображается ошибка:
failed to open pdf ...
каквидно в коде.
Кто-нибудь знает почему?
Я использую Xcode 10.2.
Вот код, который я использую для 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
}
и для 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