【发布时间】:2019-09-18 12:50:08
【问题描述】:
我在文档目录中有本地 pdf 文件。我的代码使用WKWebview 或PDFViewer 调用每个显示,但它只显示在iPhone XR 模拟器上,而不显示在任何其他模拟器设备上。
任何早于 XR 的设备,都会显示错误:
libMobileGestalt MobileGestalt.c:890: MGIsDeviceOneOfType is not supported on this platform.
带 XS、XS Max 的设备,显示错误:
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
【问题讨论】:
-
加载失败时,是否提示“打开pdf失败”?
-
无法打开 pdf 仅适用于 XS、XS max、... XR 及以上版本。任何版本较旧的 XR,生成的 libMobileGestalt MobileGestalt.c:890: MGIsDeviceOneOfType 在此平台上不受支持。是否需要为每个模拟器版本安装 iOS 11
-
你没有回答我的问题。您正在使用 do/try/catch 模式。我在问你:当数据加载失败时,你抓到了吗?
-
是的,如果加载失败,我确实看到了它。 XR 没有失败
-
好的。所以在你的
catch子句中,在print("failed to open pdf \(dirPath)" )之后,添加print(error)。现在您将获得实际错误的打印输出,并且您将获得更多关于这里出了什么问题的线索。基本上,通过不打印error,您会丢弃错误信息并破坏try/catch架构的全部优势。
标签: ios swift wkwebview pdfview