将CKAsset加载到UIWebView中

时间:2017-09-05 18:51:24

标签: ios swift3 uiwebview xcode8 cloudkit

我正在尝试将存储在publicDatabase中的PDF文件作为CKAsset加载,该代码在去年发布的现有应用程序上运行良好。但是我现在得到以下错误

无法将'NSData'类型的值转换为强制

中的'数据'类型

这是代码 -

if  docType == "PDF" || docType == "pdf" {
   if let asset1 = record.object(forKey: "documentFile") as? CKAsset {
      let doc1Data : NSData? = NSData(contentsOf:asset1.fileURL)
      let path = (NSTemporaryDirectory() as NSString).appendingPathComponent(filename)
      let fileURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(filename)
       do {
       try doc1Data!.write(to: URL(fileURLWithPath: path), options: .atomic)
       } catch {
       print(error)
       }
       self.docWebView.load(doc1Data! as Data, mimeType: "application/pdf", textEncodingName: "UTF-8", baseURL: NSURL() as URL)
       self.filenameURL = [(fileURL)]
}

我确信必须有一个简单的解释,但我看不出问题

先谢谢。

2 个答案:

答案 0 :(得分:0)

尝试更改以下行:

let doc1Data : NSData? = NSData(contentsOf:asset1.fileURL)

let doc1Data = NSData(contentsOf:asset1.fileURL)

答案 1 :(得分:0)

看起来答案很简单,将加载数据更改为loadRequest并引用从CKAsset获取文档的URL

if  docType == "PDF" || docType == "pdf" {
if let asset1 = record.object(forKey: "documentFile") as? CKAsset {
let doc1Data : NSData! = NSData(contentsOf:asset1.fileURL)
let path = (NSTemporaryDirectory() as NSString).appendingPathComponent(filename)
do {
   try doc1Data!.write(to: URL(fileURLWithPath: path), options: .atomic)
   let url = URL(fileURLWithPath: path)
   let urlRequest = URLRequest(url: url)
   self.docWebView?.loadRequest(urlRequest)
  } catch {
   print(error)
   }
}