【问题标题】:iOS 13 WKWebView not showing pdf file anymoreiOS 13 WKWebView 不再显示 pdf 文件
【发布时间】:2019-09-24 05:19:51
【问题描述】:

我正在使用 WKWebView 显示来自远程 URL 的 pdf 文件。它在iOS 12 中运行良好,但在iOS 13 中它只是显示空白屏幕。 我使用图像 url 访问了同一个域并且效果很好,但它仅与 pdf 文件存在一些问题。

let myURL = URL(string:"somefileurl.pdf") // If I hit this url in safari, It will download a pdf file.
let myRequest = URLRequest(url: myURL!)
webViewPdf.load(myRequest)  

【问题讨论】:

    标签: ios swift wkwebview ios13


    【解决方案1】:

    我发现响应的 content-type"application/octet-stream" 而不是 "application/pdf"

    所以我将WKWebView 加载为:

    if let myURL = URL(string:"somefileurl.pdf") {
        if let data = try? Data(contentsOf: myURL) {
           webViewPdf.load(data, mimeType: "application/pdf", characterEncodingName: "", baseURL: myURL)
        }
    }
    

    【讨论】:

    • 我遇到了相反的问题。我使用 iOS 13 SDK 构建了我的应用程序,但在 iOS 12.4.1 上运行它时只看到带有 PDF 的空白屏幕。您是否对不同之处以及是否可以在外部进行更改(可能是下载标题?)有更多的了解。如果可以避免,我不想上传新版本
    • @MattLong 如果是本地文件,则需要使用loadFileURL(_:allowingReadAccessTo:)。将相同的文件 url 放入两个参数中。繁荣。适用于 iOS 12 和 13。
    • @MattLong 你得到解决方案了吗,我也遇到了同样的问题。
    • @Deepak 我将 fl034 的解决方案与 loadFileURL(_:allowingReadAccessTo:) 一起使用,(尽管几个月前我已将其实施)
    • @fl034 - 很棒的建议,现在支持 iOS 12、13 和 14
    【解决方案2】:

    UIWebView 也有同样的问题。修复如下(objective-c):

    [self.webView loadData:data MIMEType:@"application/pdf" textEncodingName:@"" baseURL:[NSURL URLWithString:@"FilePathOrUrlString"];
    

    【讨论】:

      【解决方案3】:

      只需实现 WKNavigationDelegate 中可用的 decisionPolicyFor 方法,如下所示,

      func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
         decisionHandler(.allow)
      }
      

      并在您的网络视图中设置委托,如下所示,

      yourWebView.navigationDelegate = self
      

      【讨论】:

        【解决方案4】:

        对于 PDF,请在我的案例中尝试此操作 100%

        func openFile(url:URL){
        self.displayDocument(filePath: url, type: "pdf")
        }
        
        
        
         extension Your ViewControler Name :UIDocumentInteractionControllerDelegate{
        
                    public func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
                        UINavigationBar.appearance().tintColor = UIColor.blue
                        return self
                    }
        
                    public func documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController) {
                        UINavigationBar.appearance().tintColor = UIColor.clear
                        self.documentInteractionController = nil
                    }
                    func documentInteractionControllerDidDismissOptionsMenu(_ controller: UIDocumentInteractionController) {
                        UINavigationBar.appearance().tintColor = UIColor.clear
                        self.navigationController?.dismiss(animated: false, completion: nil)
                    }
        
                    func displayDocument(filePath:URL,type:String){
                        SVProgressHUD.dismiss()
        
                        self.documentInteractionController = UIDocumentInteractionController(url:filePath )
                        // Preview PDF
                        self.documentInteractionController.delegate = self
                        self.documentInteractionController.presentPreview(animated: true)
                    }
                }
        

        【讨论】:

        • 这与在 WKWebView 中查看远程 PDF 有什么关系?
        • 此代码对于在默认文档控制器中打开 PDF 很有用。所以不需要 WKWebView
        • 但问题是关于在 Web 视图中加载远程 PDF(非本地)。
        • 它对远程 PDF 很有用 你还必须传递 PDF URL。 PDF 已打开
        • @Amul4608 问题不是关于在远程或本地打开 PDF。它是关于在WKWebView 中打开 PDF。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-23
        • 1970-01-01
        • 1970-01-01
        • 2020-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多