ios - 为什么我的 WKWebview 或 PDFViewer 仅适用于 iPhone XR 模拟器
问题描述
我在文档目录中有本地 pdf 文件。WKWebview
我的代码使用or调用每个来显示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
解决方案
马特帮助我找出问题所在。谢谢马特。请看评论。模拟器中的每个 iOS 设备都有不同的文件夹 ID。当您下载到设备模拟器文档目录时,不要指望它会在同一文档目录中找到另一个模拟器设备。
推荐阅读
- c++ - 如何使用自定义 STL 向量类避免智能指针 delete[] 异常?
- python - 我没有得到 c 列表的打印输出
- php - 服务提供商如何为现有 IDP 完成 SAML 2.0 SSO?
- php - 如何使用 php 创建目录并将其 chmod 为 0777?
- python - 当源是 Kafka Avro 格式时如何在 Memsql 中创建转换
- google-chrome-extension - 当我尝试访问扩展程序中的本地存储时发生意外错误
- c# - LINQ 将实体的属性与字符串数组进行比较
- twitter-bootstrap - 如何使引导程序用于缩放
- java - JTransform FFT 返回无穷大
- php - 表单输入 POST 在 MySQL 中不显示任何结果