我正在研究Apple今年发布的新CKShare
,我对此有一些疑问。我试图关注WWDC" CloudKit中的新功能"视频但部分代码在Xcode中不再有效。
我要做的是:用户将输入他的姓名和电话,点击UIButton
后,将与特定的人分享。这是代码:
class ViewController: UIViewController, UICloudSharingControllerDelegate {
@IBOutlet weak var nome: UITextField!
@IBOutlet weak var telefone: UITextField!
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func send(_ sender: AnyObject) {
print("Send button was clicked.\n")
let elder = CKRecord(recordType: "Elder")
elder["name"] = self.name.text as CKRecordValue?
elder["telephone"] = self.telephone.text as CKRecordValue?
let ckContainer = CKContainer.default()
let shareRecord = CKShare(rootRecord: elder)
shareRecord[CKShareTitleKey] = "Elder" as CKRecordValue?
let shareController = UICloudSharingController(share: shareRecord, container: ckContainer)
shareController.delegate = self
shareController.availablePermissions = [.allowReadOnly]
shareController.popoverPresentationController?.sourceView = self.button
self.present(shareController, animated: true)
}
func cloudSharingController(_ csc: UICloudSharingController, failedToSaveShareWithError error: Error) {
print("PROBLEM SAVING SHARE")
}
func cloudSharingControllerDidSaveShare(_ csc: UICloudSharingController) {
print("SHARE SAVED")
}
func itemTitle(for csc: UICloudSharingController) -> String? {
return "a" //don't know what this is for
}
}
我的应用程序继续打印"问题保存分享"即使出现CloudSharingController。此外,CKRecord elder 不会出现在CloudKit的仪表板中。
答案 0 :(得分:4)
所以,我弄清楚出了什么问题......我正在使用云套件为您创建的默认区域,而在使用CKShare时您无法做到这一点。 您所要做的就是创建一个私人定制CKZone并将CKRecord和CKShare保存在其中!
答案 1 :(得分:1)
我被告知您可以使用您选择仅在已创建共享时使用的初始化程序。对于新共享,请使用备用init
试试这段代码:
let csc = UICloudSharingController { controller,
preparationCompletionHandler in
let share = CKShare(rootRecord: elder)
let mro = CKModifyRecordsOperation(
recordsToSave: [elder, share],
recordIDsToDelete: nil)
mro.modifyRecordsCompletionBlock = {
records, recordIDs, error in
if error != nil {
//handle error
}
preparationCompletionHandler(share,CKContainer.default(), error)
}
myDB.add(mro)
}
csc.delegate = self
self.present(csc, animated:true, completion:nil)