繁体   English   中英

为什么我的WKWebview或PDFViewer仅适用于iPhone XR模拟器

[英]Why my WKWebview or PDFViewer only works in iPhone XR simulator

我在文档目录中有本地pdf文件。 我的代码调用每个使用WKWebviewPDFViewer显示,但它只显示在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。 下载到设备模拟器文档目录时,不要指望它可以在同一文档目录中为另一个模拟器设备找到相同的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM