CloudKit Sharing

后端 未结 2 1309
孤独总比滥情好
孤独总比滥情好 2021-01-01 02:18

I am having trouble understanding some of the CloudKit sharing concepts and the WWDC 2016 \"What\'s new in CloudKit\" video doesn\'t appear to explain everything that is req

相关标签:
2条回答
  • 2021-01-01 02:58

    I didn't understand quite well when you want to get those informations. I'm with the same problem as you, but I only can get the shared data by clicking the URL... To do that you'll need two functions. First one in AppDelegate:

    func application(_ application: UIApplication, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShareMetadata) {
    
        let acceptSharesOperation = CKAcceptSharesOperation(shareMetadatas: [cloudKitShareMetadata])
        acceptSharesOperation.perShareCompletionBlock = {
            metadata, share, error in
            if error != nil {
                print(error?.localizedDescription)
            } else {
                let viewController: ViewController = self.window?.rootViewController as! ViewController
                viewController.fetchShare(cloudKitShareMetadata)
            }
        }
        CKContainer(identifier: cloudKitShareMetadata.containerIdentifier).add(acceptSharesOperation)
    }
    

    in ViewConroller you have the function that will fetch this MetaData:

    func fetchShare(_ metadata: CKShareMetadata) {
        let operation = CKFetchRecordsOperation(recordIDs: [metadata.rootRecordID])
        operation.perRecordCompletionBlock = { record, _, error in
            if error != nil {
                print(error?.localizedDescription)
            }
            if record != nil {
                DispatchQueue.main.async() {
                    self.currentRecord = record
                    //now you have your Shared Record
                }
            }
        }
        operation.fetchRecordsCompletionBlock = { _, error in
            if error != nil {
                print(error?.localizedDescription)
            }
        }
        CKContainer.default().sharedCloudDatabase.add(operation)
    }
    

    As I said before, I'm now trying to fetch the ShareDB without accessing the URL. I don't want to depend on the link once I already accepted the share. Hope this helps you!

    0 讨论(0)
  • 2021-01-01 03:17

    Dude, I got it: First you need to get the CKRecordZone of that Shared Record. You do it by doing the following:

    let sharedData = CKContainer.default().sharedCloudDatabase
        sharedData.fetchAllRecordZones { (recordZone, error) in
            if error != nil {
                print(error?.localizedDescription)
            }
            if let recordZones = recordZone {
                // Here you'll have an array of CKRecordZone that is in your SharedDB!
            }
        }
    

    Now, with that array in hand, all you have to do is fetch normally:

    func showData(id: CKRecordZoneID) {
    
        ctUsers = [CKRecord]()
    
        let sharedData = CKContainer.default().sharedCloudDatabase
        let predicate = NSPredicate(format: "TRUEPREDICATE")
        let query = CKQuery(recordType: "Elder", predicate: predicate)
    
        sharedData.perform(query, inZoneWith: id) { results, error in
            if let error = error {
                DispatchQueue.main.async {
                    print("Cloud Query Error - Fetch Establishments: \(error)")
                }
                return
            }
            if let users = results {
                print(results)
                self.ctUsers = users
                print("\nHow many shares in cloud: \(self.ctUsers.count)\n")
                if self.ctUsers.count != 0 {
                    // Here you'll your Shared CKRecords!
                }
                else {
                    print("No shares in SharedDB\n")
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题