swift 使用WKWebView加载网络PDF

 var image = WKWebView()
 var link:String!

func down(){
        //指定下载路径和保存文件名
        var z:URL = URL(string:link)!
        let destination: DownloadRequest.DownloadFileDestination = { _, _ in
            let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
            let fileURL = documentsURL.appendingPathComponent("file1/\(self.link)")
            z = fileURL
            //两个参数表示如果有同名文件则会覆盖,如果路径中文件夹不存在则会自动创建
            return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
        }
        
        
    
        //开始下载
        Alamofire.download(link, to: destination)
            .downloadProgress { progress in
        
                SVProgressHUD.showProgress(Float(progress.completedUnitCount/progress.totalUnitCount), status: "正在下载")
                print("已下载:\(progress.completedUnitCount/1024)KB")
                print("总大小:\(progress.totalUnitCount/1024)KB")
            }
            .responseData { response in
                if let data = response.result.value {
                    print("下载完毕!")
                    SVProgressHUD.dismiss()
                    let image2 = UIImage(data: data)
                    
                    DispatchQueue.main.async {

                      if let path = response.destinationURL?.path{
                        
                          let urlStr = URL.init(fileURLWithPath:path);
                          let data = try! Data(contentsOf: urlStr)
                          self.image.load(data, mimeType: "application/pdf", characterEncodingName: "utf-8", baseURL: NSURL() as URL)
                      }
                        
                    }
                }
        }
    }
    
  

你可能感兴趣的:(swift 使用WKWebView加载网络PDF)