【问题标题】:Error Domain=WebKitErrorDomain Code=102 "Frame load interrupted"错误域 = WebKitErrorDomain 代码 = 102“帧加载中断”
【发布时间】:2020-03-30 01:25:36
【问题描述】:

在 WebView 中显示 pdf 时出错

"Error Domain=WebKitErrorDomain Code=102 "帧加载中断"

但使用相同的代码图像填充正确。

【问题讨论】:

  • 面临同样的问题。
  • 您可以发布您尝试加载的网址吗?
  • 我可以分享格式但不能透露完整的网址,这里是 HOST/api/worklife/upload-files/download?filetoken=tnjGv78shDBtdIl50hepzigMv
  • 如果也出现这种情况,请用真机检查
  • 是的,我在不同的设备以及 UIWebview 和 WKWebview 上检查了它,但对我没有任何效果,当我在桌面浏览器上打开此 url 时,它会直接下载文件。

标签: ios objective-c swift


【解决方案1】:

根本原因在于Content-Type

此请求得到响应 Content-Type: binary/octet-stream.

有很多方法可以将 Content-Type 修改为正确的形式 Content-Type: application/pdf

那么 WKWebView 就会正常运行了。

This anwser 提供本地解决方案。

【讨论】:

    【解决方案2】:

    感谢您的贡献:)

    终于在花了几个小时之后,在这里我找到了这个常见的 Webview “帧加载中断”问题的解决方案:

    • 以字节形式下载文件
    • 将其存储在本地存储中
    • 使用本地路径在 Web 视图中加载文件,它可以工作

    尝试下面的代码来完成上述步骤

    //在Web视图中显示文档的方法

        func methodToShowDocumentInWebView(strUrl : String, fileName :  String, controller: UIViewController)  {
    
     //Get Request to download in bytes
           Service.shared()?.callAPI(withURLWithoutHandlingAndLoaderAndHttpStatusCode: strUrl, andLoaderenabled: true, method: "GET", parameters: [:], withController: self, completion: { (data, error, code) in
    
              if  let dataFile = data {
    
                 let (success , payslipPath) = self.methodToWriteFileLocally(data: dataFile as! Data, fileName: fileName, directory: "Leave")
                 if success {
                    webviewInstance.loadRequest(NSURLRequest(URL: NSURL(string: payslipPath)!))
                 }else{
                    //Handle Error case
                 }
              }
           })
        }
    

    //本地存储数据的方法

    func methodToWriteFileLocally(data : Data, fileName: String, directory : String) -> (success :Bool ,path :URL?) {
    
       let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
       let fileURL = documentsURL?.appendingPathComponent(directory)
       let payslipPath = fileURL?.appendingPathComponent(fileName)
    
       if !FileManager.default.fileExists(atPath: payslipPath!.path) {
          do{
             try FileManager.default.createDirectory(atPath: fileURL!.path, withIntermediateDirectories: true, attributes: nil)
          }
          catch{
             //Handle Catch
             return (false, nil)
    
          }
          let writeSuccess =  (data as AnyObject).write(to: payslipPath!, atomically: true)
          return (writeSuccess, payslipPath!)
       }
       else
       {
          let writeSuccess =  (data as AnyObject).write(to: payslipPath!, atomically: true)
          return (writeSuccess, payslipPath!)
    
       }
    }
    

    【讨论】:

    • 我有一个发往服务器的 POST 请求,它以 PDF 数据进行响应。扩展名为 .pdf 的文件名以 application/x-www-form-urlencoded 格式作为参数发送。我也收到 Error Domain=WebKitErrorDomain Code=102 “Frame load interrupted” 错误。如何解决?请帮忙。 TIA
    • @VaibhavJhaveri 你找到解决方案了吗??
    • 相反,您可以使用 Data(contentsOf: url) 获取字节,然后 webView.load(data, mimeType: "application/pdf", characterEncodingName: "utf-8", baseURL: url)根据您的情况调整参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    • 2015-02-28
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 2013-11-11
    相关资源
    最近更新 更多