我正在尝试允许我的应用程序的用户能够检索指向他们已经共享的记录的iCloud共享链接。创建共享时,可以通过使用let share = CKShare(rootRecord: CKRecord)
后跟Apple的UISharingController
来获取URL。但是,每次使用此方法时,旧链接都会失效,并将其他人踢出他们已经加入的共享。如何允许用户(记录所有者)随时获取记录CKShare
对象的可共享URL?
//Initially shares the record
let share = CKShare(rootRecord: newRecord)
share[CKShare.SystemFieldKey.title] = "Group" as CKRecordValue?
let modifyRecordsOperation = CKModifyRecordsOperation( recordsToSave: [newRecord, share], recordIDsToDelete: nil)
//Activates the UISharingController (code not shown)
答案 0 :(得分:1)
假设该记录已被共享,您可以通过以下方式获取邀请其他参与者的 URL:
if let shareReference = record.share {
database.fetch(withRecordID: shareReference.recordID) { (share, error) in
let shareURL = (share as? CKShare)?.url
}
}
如果您想为现有共享再次显示 UICloudSharingController
(而不生成新 URL),您可以这样做:
let sharingController = UICloudSharingController { (_, prepareCompletionHandler) in
func createNewShare() {
let operationQueue = OperationQueue()
operationQueue.maxConcurrentOperationCount = 1
let share = CKShare(rootRecord: record)
share[CKShare.SystemFieldKey.title] = "Title" as CKRecordValue
share.publicPermission = .readWrite
let modifyRecordsOp = CKModifyRecordsOperation(recordsToSave: [share, record], recordIDsToDelete: nil)
modifyRecordsOp.modifyRecordsCompletionBlock = { records, recordIDs, error in
prepareCompletionHandler(share, self, error)
}
modifyRecordsOp.database = database
operationQueue.addOperation(modifyRecordsOp)
}
if let shareReference = record.share {
database.fetch(withRecordID: shareReference.recordID) { (share, error) in
guard let share = share as? CKShare else {
createNewShare()
return
}
prepareCompletionHandler(share, self, nil)
}
} else {
createNewShare()
}
}
答案 1 :(得分:0)
听起来您这里有多个问题。我建议您仔细检查UICloudSharingController documentation。
第一个问题是您提供的每次创建新共享的代码,当您要做的是创建一次共享,然后在您希望与之交互的任何时候显示该共享(包括获取共享网址)。创建共享后,您可能需要以某种方式将其保留在设备上(可以像将其存储在UserDefaults中一样简单)。
第二个问题是听起来您没有正确实例化UICloudSharingController。最初创建CKShare时,必须使用UICloudSharingController init(preparationHandler:)初始化程序并在其中创建共享。
根据不同初始化程序(重点是我的)的文档:
重要
您必须使用正确的初始化程序来初始化控制器 方法。如果CKRecord是,请不要使用init(preparationHandler :) 已经共享。同样,如果以下情况也不要使用init(share:container :) 不共享CKRecord。使用错误的初始化程序会导致错误 保存记录时。
然后当您想要显示shareSheet时,请使用UICloudSharingController init(share:container:)初始化程序,并将您已持久存储的份额提供给设备。
这是UICloudSharingController文档中提供的用于创建CKShare的代码:
func presentCloudSharingController(_ sender: Any) { guard
let barButtonItem = sender as? UIBarButtonItem,
let rootRecord = self.recordToShare else {
return
}
let cloudSharingController = UICloudSharingController { [weak self] (controller, completion: @escaping (CKShare?, CKContainer?, Error?) -> Void) in
guard let `self` = self else {
return
}
self.share(rootRecord: rootRecord, completion: completion)
}
if let popover = cloudSharingController.popoverPresentationController {
popover.barButtonItem = barButtonItem
}
self.present(cloudSharingController, animated: true) {}
}
func share(rootRecord: CKRecord, completion: @escaping (CKShare?, CKContainer?, Error?) -> Void) {
let shareRecord = CKShare(rootRecord: rootRecord)
let recordsToSave = [rootRecord, shareRecord];
let container = CKContainer.default()
let privateDatabase = container.privateCloudDatabase
let operation = CKModifyRecordsOperation(recordsToSave: recordsToSave, recordIDsToDelete: [])
operation.perRecordCompletionBlock = { (record, error) in
if let error = error {
print("CloudKit error: \(error.localizedDescription)")
}
}
operation.modifyRecordsCompletionBlock = { (savedRecords, deletedRecordIDs, error) in
if let error = error {
completion(nil, nil, error)
} else {
completion(shareRecord, container, nil)
}
}
privateDatabase.add(operation)
}
下次您要显示它时,它会更加困难:
let share = // yourCKShare
let container = // yourCKContainer
let viewController = // yourViewController
let shareController = UICloudSharingController(share: share, container: container)
viewController.present(shareController, animated: true)
最后要回答您问题的URL部分,UICloudSharingController用于所有事物的共享-在控制器呈现共享时,所有者可以使用“复制链接”按钮(如果可能,用户也可以使用该按钮)您允许公共访问-由于我的资料是私人的,因此我没有考虑):