我正在使用WKWebView
来显示来自远程URL的pdf
文件。它在iOS 12
中工作正常,但在iOS 13
中仅显示空白屏幕。
我使用图片网址访问了同一个域,效果很好,但是仅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)
答案 0 :(得分:8)
我发现响应的内容类型是“应用程序/八位字节流” ,而不是“应用程序/ pdf” < / p>
因此,我将WKWebView
加载为:
if let myURL = URL(string:"somefileurl.pdf") {
if let data = try? Data(contentsOf: myURL) {
webViewPdf.load(data, mimeType: "application/pdf", characterEncodingName: "", baseURL: myURL)
}
}
答案 1 :(得分:0)
UIWebView也有同样的问题。固定为follow(objective-c):
[self.webView loadData:data MIMEType:@"application/pdf" textEncodingName:@"" baseURL:[NSURL URLWithString:@"FilePathOrUrlString"];
答案 2 :(得分:0)
只需实现WKNavigationDelegate中可用的definePolicyFor方法,如下所示,
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
decisionHandler(.allow)
}
并在网络视图中设置委托,如下所示,
yourWebView.navigationDelegate = self
答案 3 :(得分:-1)
对于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)
}
}