CKAsset fetch PDF and present in UIWebView Swift3

96 Views Asked by At

A have a CKRecord which contains details of a course, I can fetch all the records and manipulate them as required within a particular view. I am having a little problem with the CKAsset, which is a pdf. My image CKAsset fetch and display in a UIImageView works fine -

 let coverPhoto = record.object(forKey: "courseImage") as? CKAsset
                        if let asset = coverPhoto {
                            if asset != nil {
                                let photoData : NSData? = NSData(contentsOf:asset.fileURL)
                                self.cseImageView.image = UIImage(data:photoData! as Data)
                            }
                        }

However I am unsure how to finish the code when it comes to the pdf - this is my attempt, which is incomplete (indicated by the ?????)

if let asset = record.object(forKey: "courseDocument1") as? CKAsset {
                            if asset != nil {
                                let docData : NSData? = NSData(contentsOf:asset.fileURL)
                        self.couseDocWebView.?????? = (docData, MIMEType:"application/pdf")
                            }
                        }

I guess my question is, am I close or way off the mark, and if so - any pointer?

Thank you

1

There are 1 best solutions below

0
On

After much debate and trying a variety of scripts, the following code works in Swift 3 -

if let asset = record.object(forKey: "courseDocument1") as? CKAsset {
                            if asset != nil {
                        let doc1Data : NSData? = NSData(contentsOf:asset.fileURL)
                         self.courseDocWebView.load(doc1Data! as Data, mimeType: "application/pdf", textEncodingName: "UTF-8", baseURL: NSURL() as URL)

I think it was just the order in which the data is called and presented with a referring URL from the CKAsset path.