【问题标题】:How to get the current user ID in CloudKit?如何在 CloudKit 中获取当前用户 ID?
【发布时间】:2015-06-01 15:52:38
【问题描述】:

我想在公共数据库的表格中为每个用户最多保存一条记录(即评分)。为此,我需要保存当前的用户 ID 或设备 ID。但是我怎样才能得到它呢?

【问题讨论】:

标签: ios cloudkit


【解决方案1】:

更紧凑的 Swift 5 代码解决方案:

CKContainer.default().fetchUserRecordID(completionHandler: { (recordId, error) in
    if let name = recordId?.recordName {
       print("iCloud ID: " + name)
    }
    else if let error = error {
       print(error.localizedDescription)
    }
})

对于特定的 CloudKit 容器,请使用以下代码:

CKContainer(identifier: "<Your CloudKit container identifier>").fetchUserRecordID(completionHandler: { (recordId, error) in
    if let name = recordId?.recordName {
       print("iCloud ID: " + name)
    }
    else if let error = error {
       print(error.localizedDescription)
    }
})

【讨论】:

  • 由于某种原因,这会为我返回一个错误:“无法从服务器获取容器配置“iCloud.com.my_container_name”。知道为什么吗?我在用户记录中有用户记录在 Cloudkit 仪表板中显示的类型
【解决方案2】:

这里是 Swift 3 的代码 sn-p

import CloudKit

/// async gets iCloud record ID object of logged-in iCloud user
func iCloudUserIDAsync(complete: @escaping (_ instance: CKRecordID?, _ error: NSError?) -> ()) {
    let container = CKContainer.default()
    container.fetchUserRecordID() {
        recordID, error in
        if error != nil {
            print(error!.localizedDescription)
            complete(nil, error as NSError?)
        } else {
            print("fetched ID \(recordID?.recordName)")
            complete(recordID, nil)
        }
    }
}

// call the function above in the following way:
// (userID is the string you are interested in!)
iCloudUserIDAsync { (recordID: CKRecordID?, error: NSError?) in
    if let userID = recordID?.recordName {
        print("received iCloudID \(userID)")
    } else {
        print("Fetched iCloudID was nil")
    }
}

【讨论】:

  • 我收到错误,例如,由于未捕获的异常“CKException”而终止应用程序,原因:“应用程序缺少必需的权利 com.apple.developer.icloud-services”
  • 您是否在“功能”选项卡中启用 iCloud?
  • @Yi-HsiuLee 我可以使用这个rec​​ordID来唯一地识别不同设备上具有相同icloud帐户的用户吗?我的意思是如果用户安装应用程序并写一些笔记。然后我将这些笔记保存在 firebase 上。然后该用户使用相同的 icloud 帐户在另一台设备上安装该应用程序,我可以检索已经用另一台设备编写的笔记吗?
【解决方案3】:

使用 Swift 2 获取 iCloud ID / CloudKit 记录 ID

这是一个 sn-p,我一直用它来获取 iOS 应用程序的 iPhone 用户的 iCloud ID(Apple 称之为 CloudKit Record ID)。

您在 Xcode 中唯一需要做的就是激活项目的 iCloud 功能中的“CloudKit”复选框。您根本不需要主动使用 CloudKit - 它只是 iOS 8 以来所有 iCloud 活动的核心。

重要的是要知道,Apple 从不直接公开真实的 iCloud ID,而总是只返回 iCloud ID 和您的应用 ID 的安全哈希值。但这不应该让您担心,因为该字符串对于您应用的每个用户而言在他的所有设备上仍然是唯一的,并且可以用作登录替换

我的函数是异步的,并返回一个可选的 CKRecordID 对象。 CKRecordID 对象最有趣的属性是 recordName。

CKRecordID.recordName 是一个 33 个字符的字符串,其中第一个字符始终是下划线,后跟 32 个唯一字符(== 为您的应用编码的用户 iCloud ID)。它看起来类似于:"_cd2f38d1db30d2fe80df12c89f463a9e"

import CloudKit

/// async gets iCloud record name of logged-in user
func iCloudUserIDAsync(complete: (instance: CKRecordID?, error: NSError?) -> ()) {
    let container = CKContainer.defaultContainer()
    container.fetchUserRecordIDWithCompletionHandler() {
        recordID, error in
        if error != nil {
            print(error!.localizedDescription)
            complete(instance: nil, error: error)
        } else {
            print("fetched ID \(recordID?.recordName)")
            complete(instance: recordID, error: nil)
        }
    }
}


// call the function above in the following way:
// (userID is the string you are intersted in!)

iCloudUserIDAsync() {
    recordID, error in
    if let userID = recordID?.recordName {
        print("received iCloudID \(userID)")
    } else {
        print("Fetched iCloudID was nil")
    }
}

【讨论】:

  • 当我执行 CKContainer.defaultContainer() libc++abi.dylib: terminating with uncaught exception of type CKException 时出现异常
  • @Sebastian 我可以使用此记录ID 来唯一标识在不同设备上具有相同 icloud 帐户的用户吗?我的意思是如果用户安装应用程序并写一些笔记。然后我将这些笔记保存在 firebase 上。然后该用户使用相同的 icloud 帐户在另一台设备上安装该应用程序,我可以检索已经用另一台设备编写的笔记吗?
  • @SalmanAli 是的,这是它最重要的方面之一。几年前我写了一篇关于它的博客文章:medium.com/@skreutzb/…
  • @Sebastian 有时返回的 recordName 为 nil,退出并再次登录 icloud 可以解决问题,但是对于此问题是否有任何原因或更好的编程解决方案
【解决方案4】:

您需要调用 -[CKContainer fetchUserRecordIDWithCompletionHandler:] 来获取当前用户记录 ID:

【讨论】:

  • 当我执行 CKContainer.defaultContainer() libc++abi.dylib: terminating with uncaught exception of type CKException 时出现异常
猜你喜欢
  • 1970-01-01
  • 2018-10-03
  • 1970-01-01
  • 1970-01-01
  • 2022-08-02
  • 1970-01-01
  • 1970-01-01
  • 2015-01-27
  • 2022-01-22
相关资源
最近更新 更多