我有作为CKAssets的PDF文件,这些文件在UIWebView中被调用和呈现,没有问题。我已经设法操纵了代码,以便CKAsset将出现在ActivityViewController中,但这在func方法内,我希望将此部分分配给Action按钮。我的问题是如何在函数中调用几行?还是在功能外部有足够的引用来使操作按钮起作用?
这是功能代码-
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不是该类的成员。
最佳答案
我想通了(我认为)。开始 -
我变了
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?”,但如果我遗漏了更根本的内容,我很乐意得到更正。