iOS 13中的WKWebView不再显示PDF文件

7

我使用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)  
4个回答

15
我发现响应的 内容类型"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)
    }
}

2
我遇到了相反的问题。我使用iOS 13 SDK构建了我的应用程序,但在iOS 12.4.1上运行时只能看到PDF的空白屏幕。您是否有更多的见解,以了解有什么不同之处,以及这是否可能在外部进行更改(例如下载标头)?如果可以避免上传新版本,则最好不要上传新版本。 - Matt Long
3
如果这是一个本地文件,你需要使用 loadFileURL(_:allowingReadAccessTo:)。将相同的文件 URL 放入两个参数中即可。完成。在 iOS 12 和 13 中都可以工作。 - heyfrank
@MattLong,你解决了吗?我也遇到了同样的问题。 - Deepak
@Deepak 我使用了 fl034 的解决方案,使用了 loadFileURL(_:allowingReadAccessTo:) 方法(尽管我是几个月前就开始使用了)。 - Matt Long
1
@fl034 - 非常好的建议,现在它支持iOS 12、13和14。 - Shawn Frank

0

UIWebView也有同样的问题。解决方法如下(Objective-C):

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

0

只需按照下面所示实现WKNavigationDelegate中提供的decidePolicyFor方法即可:

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

并将您的web视图代理设置如下:

yourWebView.navigationDelegate = self

-1

尝试这个PDF方法,在我的情况下百分之百有效

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)
            }
        }

2
这与在WKWebView中查看远程PDF有什么关系? - rmaddy
这段代码可用于在默认文档控制器中打开PDF,因此不需要WKWebView。 - Amul4608
3
问题是如何在 web 视图中加载远程 PDF 文件(而不是本地文件)。 - rmaddy
对于远程PDF也很有用,您还需要传递PDF URL。并且PDF是打开的。 - Amul4608
3
这个问题不是关于在远程或本地打开PDF文件,而是关于在WKWebView内打开PDF文件。 - Kamran
有人找到解决方案了吗?在iOS 13上,PDF无法在WKWebView中加载。 - Bharath

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接