7

在视图顶部显示 UICloudSharingController 时,它会显示屏幕,当我选择消息选项以向我想与之共享的人发送消息时,它会提供一个带有“上传”消息的旋转轮并消失 - 附加截屏

但是,当我转到 cloudkit 仪表板时,根记录已被共享。但我不能与特定的人分享。是因为它已经共享全球了吗?我该如何解决?

self.shareInfraRecord(zoneID: appDelegate.privateContactZoneID, completion: { (status) in
     if ( status == false) {
             return
      }
    })

func shareInfraRecord(zoneID: CKRecordZone.ID, completion: @escaping(Bool) -> Void) {
    
    if let rootRecord = self.rootRecord {
        if self.rootRecord?.share == nil {
            let sharingController = UICloudSharingController { (controller, preparationHandler: @escaping (CKShare?, CKContainer?, Error?) -> Void) in
                
                let shareID = CKRecord.ID(recordName: UUID().uuidString, zoneID: zoneID)
                var share = CKShare(rootRecord: rootRecord, shareID: shareID)
               
                share[CKShare.SystemFieldKey.title] = Cloud.ShareInfrastructure.ContactShareTitleKey as CKRecordValue?
                share[CKShare.SystemFieldKey.shareType] = Cloud.ShareInfrastructure.ContactShareTypeKey as CKRecordValue?
               
                let modifyRecZoneOp = CKModifyRecordsOperation(recordsToSave:[rootRecord, share], recordIDsToDelete: nil)
                modifyRecZoneOp.modifyRecordsCompletionBlock = { (records, recordID, error) in
                    if error != nil {
                        if let ckerror = error as? CKError {
                            if let serverVersion = ckerror.serverRecord as? CKShare {
                                share = serverVersion
                            }
                            completion(false)
                        }
                    }
                    preparationHandler(share, self.defaultContainer, error)

                }
                self.privateDB?.add(modifyRecZoneOp)
            }
        
            sharingController.availablePermissions = [.allowReadOnly, .allowPrivate]
            sharingController.delegate = self
            sharingController.popoverPresentationController?.sourceView = self.view
        
            self.present(sharingController, animated:true, completion:nil)

    } else {
            let shareRecordID = rootRecord.share!.recordID
            let fetchRecordsOp = CKFetchRecordsOperation(recordIDs: [shareRecordID])
        
            fetchRecordsOp.fetchRecordsCompletionBlock = { recordsByRecordID, error in
                guard error == nil, let share = recordsByRecordID?[shareRecordID] as? CKShare  else {
                    if let ckerror = error as? CKError {
                        self.aErrorHandler.handleCkError(ckerror: ckerror)
                        //self.saveToCloudKitStatus(recordName: myRecordName, success: false)
                    }
                    completion(false)
                    return
                }
               
                DispatchQueue.main.async {
                    let sharingController = UICloudSharingController(share: share, container: self.defaultContainer!)
                    completion(true)
                    //completionHandler(sharingController)
                }
            }
            self.privateDB?.add(fetchRecordsOp)
        }
    }
}
4

1 回答 1

1

这可能有点晚了,但我也遇到了这个问题,在使用NSPersistentCloudKitContainer时,似乎问题只是确保我在设置的 Capabilities 部分中的 iCloud 容器名称与我的应用程序包名称匹配,即iCloud.com.goddamnyouryan.MyApp

于 2021-03-26T22:31:33.940 回答