iOS 的唯一用户 ID

Roh*_*esh 0 ios swift

对于我的应用程序,我希望能够在服务器端识别我的用户,为此我需要在设备端有一个唯一的用户标识。对于 Android,我们使用 Google 登录。我们可以为 iPhone 使用什么?我不想使用 Google 登录,因为它不是 iOS 用户的本机。我想在 iOS 生态系统中使用本机等价物。

Sun*_*nil 7

您可以使用 CloudKit 访问当前用户的唯一记录/ID。无论设备如何,只要用户使用相同的 AppleID,该 ID 就应该是唯一的。

 let container = CKContainer.default()
 container.fetchUserRecordID { (recordId, error) in
     if error != nil {
         print("Handle error", error)
     } else{
         print("recordId", recordId, recordId.recordName)
     }
 }
Run Code Online (Sandbox Code Playgroud)

您还可以获取更多信息,例如姓名、电子邮件、电话等,但需要额外许可

 container.requestApplicationPermission(.userDiscoverability) { (status, error) in
     container.discoverUserIdentity(withUserRecordID: recordId!, completionHandler: { (userID, error) in
                  print(userID?.hasiCloudAccount)
                  print(userID?.lookupInfo?.phoneNumber)
                  print(userID?.lookupInfo?.emailAddress)
                  print((userID?.nameComponents?.givenName)! + " " + (userID?.nameComponents?.familyName)!)
     })
 }
Run Code Online (Sandbox Code Playgroud)

确保在项目功能设置中启用 iCloud/CloudKit 服务

  • 我哪里说过“必须”了?“可能”是关键字:)如果您感兴趣,一些相关信息:https://www.stuff.co.nz/technology/gadgets/72729126/apple-quietly-makes-icloud-optout-for-iphones-ipads (4认同)

Dav*_* S. 5

Apple 会告诉您使用vendor Identifier

UIDevice.current.identifierForVendor?.uuidString
Run Code Online (Sandbox Code Playgroud)

它在设备上的所有应用程序中都是相同的。

要回答通知,您可以在委托回调中获得一个标识符。

optional func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
Run Code Online (Sandbox Code Playgroud)

deviceToken记录为

deviceToken
A globally unique token that identifies this device to APNs. Send this token to the server that you use to generate remote notifications. Your server must pass this token unmodified back to APNs when sending those remote notifications.

APNs device tokens are of variable length. Do not hard-code their size.
Run Code Online (Sandbox Code Playgroud)