Immensely helpful. Thank you!
For anyone who stumbles upon this, here is how I went about doing it:
@IBAction func shareButtonTapped(_ sender: UIButton) {
let shareController = UICloudSharingController { shareController,
preparationCompletionHandler in
let zoneID = CKRecordZoneID(zoneName: "MyCustomZoneName",
ownerName: CKCurrentUserDefaultName)
let zone = CKRecordZone(zoneID: zoneID)
let modZonesOp = CKModifyRecordZonesOperation(recordZonesToSave: [zone],
recordZoneIDsToDelete: nil)
modZonesOp.timeoutIntervalForRequest = 10
modZonesOp.timeoutIntervalForResource = 10
modZonesOp.modifyRecordZonesCompletionBlock = { zone, _, error in
if let error = error as? CKError {
print("modZonesOp error:", error)
preparationCompletionHandler(nil, nil, error)
return
}
// Get the record ID; I include it in the model and capture upon initial save to iCloud
let recordID = self.list?.recordID
self.privateDB.fetch(withRecordID: recordID!,
completionHandler: { (record, error) in
if error != nil {
print("Error fetching record:", error)
} else {
print("Found record, creating share...")
let share = CKShare(rootRecord: record!)
let listType = self.list?.type.rawValue
share[CKShareTitleKey] = "\(listType!) List" as CKRecordValue
let modRecordsOp = CKModifyRecordsOperation(recordsToSave: [record!, share],
recordIDsToDelete: nil)
modRecordsOp.timeoutIntervalForRequest = 10
modRecordsOp.timeoutIntervalForResource = 10
modRecordsOp.modifyRecordsCompletionBlock = { records, recordIDs, error in
if error != nil {
print("modifyRecordsOp error:", error)
} else {
print("Successfully modified records")
}
preparationCompletionHandler(share, CKContainer.default(), error)
}
self.privateDB.add(modRecordsOp)
}
})
}
self.privateDB.add(modZonesOp)
}
shareController.availablePermissions = [.allowPrivate, .allowReadWrite]
shareController.popoverPresentationController?.sourceView = shareButton
present(shareController, animated: true)
}