Rohitesh
Rohitesh

Reputation: 987

Unique User ID for iOS

For my app, I want to be able to identify my user at the server end, for which I need a unique user identification on the device side. For Android, we are using Google sign-in. What can we use for iPhone? I don't want to use Google sign-in, as it is not native to iOS users. I want to use a native equivalent in the iOS ecosystem.

Upvotes: 0

Views: 4998

Answers (2)

David S.
David S.

Reputation: 6705

Apple would tell you to use the vendor Identifier:

UIDevice.current.identifierForVendor?.uuidString

It will be the same across all your apps on the device.

To answer about the notifications, you get an identifier in the delegate callback for those.

optional func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)

The deviceToken is documented as:

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.

Upvotes: 6

Sunil
Sunil

Reputation: 758

You can use CloudKit to access current user's Unique record/ID. This is supposed to be unique irrespective of the device as long user uses the same AppleID.

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

You can also fetch more info like name, email, phone, etc but with additional permission

 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)!)
     })
 }

Make sure you enable iCloud/CloudKit services in your project Capabilities setting

Upvotes: 8

Related Questions