两个应用共享两个 CloudKit CKContainers:需要两个 UserRecordID

Two apps sharing two CloudKit CKContainers: Need both UserRecordIDs

我有一个应用程序使用它自己的本机 CKContainer。我正在构建第二个应用程序,它使用第一个应用程序的本机 CKContainer 以及它自己的本机容器。我想使用下面的函数访问两个容器的用户 ID。但是,我只取回第二个应用程序的本机 CKContainer UserID。我在第二个应用程序中成功使用了第一个应用程序的 public 数据库。当我尝试在第一个应用程序中执行特定于用户的查询时,我只是在挣扎。

有谁知道这是否可行and/or如何找到第二个 CKContainer 的用户 ID?

func findUserRecordID() {
    CKContainer.defaultContainer().fetchUserRecordIDWithCompletionHandler { (userID, error) -> Void in
        if error == nil {
            // obtain and store id
            if let userID = userID {

                SRData.sharedInstance.currentUser = userID




            }
        } else if error != nil {
            // handle errors

            print("There was an error" + "\(error?.localizedDescription)")
        }
    }
}

您需要两个 CKContainer 个实例,每个容器一个。这两个容器不共享。每个都有自己的 public 数据库、自己的私有数据库和自己的用户。

要创建另一个容器,请使用:

CKContainer(containerIdentifier: "the-name-of-the-other-container")

供大家参考的是有效的代码。

CKContainer(identifier: "iCloud.XXXX.YYYYY").fetchUserRecordIDWithCompletionHandler { (userID, error) in

        //

        if error == nil {

            // obtain and store id

            if let userID = userID {



                SRData.sharedInstance.currentV2User = userID



            }

        } else if error != nil {

            // handle error

            print("Error FindUserRecordID " + "\(error?.localizedDescription)")

        }



    }