错误域=WebKitErrorDomain 代码=102“帧加载中断”

Arj*_*tir 11 objective-c ios swift

在 WebView 中显示 pdf 时出错

“错误域=WebKitErrorDomain 代码=102”帧加载中断”

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

Arj*_*tir 5

感谢您的贡献:)

最后在花了几个小时之后,在这里我找到了这个常见的 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
             }
          }
       })
    }
Run Code Online (Sandbox Code Playgroud)

//本地存储数据的方法

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

   }
}
Run Code Online (Sandbox Code Playgroud)