1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

iCloudにログインしているユーザーのユーザーIDを取得する

Posted at

CloudKitを利用すると、iCloudでログインしているユーザーのIDを取得することができます。

以下のように、CKContainerに対してfetchUserRecordIDを実行すると、ユーザーIDがコールバックで取得できます。

let container = CKContainer(identifier: "iCloud.xxx.xxx.xxx") // 作成したコンテナのID
container.fetchUserRecordID { [weak self] (id, error) in
    DispatchQueue.main.async {
        if let error = error as? CKError {
            switch error.errorCode {
            case CKError.notAuthenticated.rawValue:
               // 認証していない場合の処理
            case CKError.managedAccountRestricted.rawValue:
               // 利用制限をかけている場合の処理
            default:
               // その他のエラー処理
            }
        } else {
            // IDが取得できた時の処理
        }
    }
}

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?