CKAsset(NSData)到UIWebView与ActivityViewController共享

时间:2017-03-08 00:31:31

标签: ios swift xcode8 uiactivityviewcontroller

我将PDF文件作为CKAssets,在UIWebView中调用和显示没有问题。我设法操纵代码,因为CKAsset将出现在ActivityViewController中,但这是在func方法中,我希望将此部分分配给Action按钮。我的问题是如何在函数中调用几行?或者在func之外有足够的引用来使动作按钮有效?

这是功能代码 -

func queryRecord() {

    let container = CKContainer.default()
    let publicDatabase = container.publicCloudDatabase
    let predicate = NSPredicate(format: "recordID = %@", CKRecordID(recordName : documentID))
    let query = CKQuery(recordType: "Documents", predicate: predicate)

    publicDatabase.perform(query, inZoneWith: nil, completionHandler: ({results, error in

        if (error != nil) {
            DispatchQueue.main.async() {
                self.notifyUser("Cloud Access Error", message: error!.localizedDescription)
            }

        } else {
            if results!.count > 0 {

                let record = results![0]
                print(record)

                DispatchQueue.main.async() {

                let docTitle = record.object(forKey: "documentName") as! String

                    self.title = "\(docTitle)"

                    let docType = record.object(forKey: "documentType") as! String

                    if  docType == "PDF" || docType == "pdf" {


                    if let asset1 = record.object(forKey: "documentFile") as? CKAsset {
                        let doc1Data : NSData? = NSData(contentsOf:asset1.fileURL)
                        self.docWebView.load(doc1Data! as Data, mimeType: "application/pdf", textEncodingName: "UTF-8", baseURL: NSURL() as URL)
                        let filenameURL = [(asset1.fileURL)]

                        let activityController = UIActivityViewController(activityItems: filenameURL, applicationActivities: nil)
                        self.present(activityController, animated: true, completion: nil)

                        }

                    } else {

我曾尝试在代码之外引用filenameURL,但它无法识别filenameURL不是该类的成员。

1 个答案:

答案 0 :(得分:0)

我想通了(我想)。在这里 -

我改变了

let filenameURL = [(asset1.fileURL)]

self.filenameURL = [(asset1.fileURL)]

然后添加

var filenameURL : Any? 

到ViewController类。这允许我将相关代码添加到actionButton -

@IBAction func activityVCButton(_ sender: UIBarButtonItem) {
        let activityController = UIActivityViewController(activityItems: (filenameURL as? [Any])!, applicationActivities: nil)
        self.present(activityController, animated: true, completion: nil)
         }

我认为我似乎缺少的关键是Any?,但如果有一些我缺少的基础知识,我很高兴得到纠正。