Uniquely identifying an iOS user

后端 未结 10 1900
小蘑菇
小蘑菇 2020-12-02 12:27

I\'d like to create a user account on the server for new users of an app, but I\'d also like to not ask the user to type in anything. Ideally, I\'d like this to be automatic

相关标签:
10条回答
  • 2020-12-02 13:07

    You can use third party solutions like SECUREUDID also check out this

    0 讨论(0)
  • 2020-12-02 13:12

    Generate a UUID with this:

    NSString *UUID() {
        CFUUIDRef cfuuid = CFUUIDCreate(NULL); 
        NSString *uuid =  (__bridge_transfer NSString *)CFUUIDCreateString(NULL, cfuuid); 
        CFRelease(cfuuid);
        return uuid;
    }
    

    Nothing here is deprecated or frowned on by Apple -- in fact, it is the way they suggest you do it. Store the generated UUID in the keychain and it will be there -- even if the user uninstalls and reinstalls your app. The UUID is unique for the device and the time it was generated.

    You can then use various schemes to have the user group their devices together -- iCloud, or some sort of key that you deliver from the server.

    Good luck!

    Addition:

    Here's how I store it in the keychain, using the uuid as a username and generating a random password:

    uuid = UUID();
    [keychainItemWrapper setObject:uuid forKey:(__bridge_transfer id)kSecAttrAccount];
    NSString *pass_token = randomString(10);
    [keychainItemWrapper setObject:pass_token forKey:(__bridge_transfer id)kSecValueData];
    

    Note that all of this can be done without any input from the user.

    Update:

    MCSMKeychainItem has a great solution to UUID generation and storage with [MCSMApplicationUUIDKeychainItem applicationUUID]. The library also has [MCSMGenericKeychainItem genericKeychainItemWithService:service username:username password:password]. Together, these functions take care of everything mentioned above. Easy to install with CocoaPods too.

    0 讨论(0)
  • 2020-12-02 13:13

    An alternative to Erik's suggestion is a third-party library called OpenUDID. You can generate a unique identifier on a per-device basis, and it's dead simple:

    #include "OpenUDID.h"
    NSString* openUDID = [OpenUDID value];
    
    0 讨论(0)
  • 2020-12-02 13:14

    Generating a UUID like Erik suggested is definitely a solid solution. The only (small) caveat might be that in the event of a full iDevice restore, the keychain will be wiped too. The application will generate a new UUID in this scenario and the iDevice cannot be related to an existing user anymore.

    Another approach to uniquely identify an iDevice is to generate a unique id based on the MAC address of the network interface and combine it with the app bundle id. This methods yields a unique id that is application specific and durable. It will not change when the iDevice is wiped, restored or when the app is removed and reinstalled. Because of this, there is no need to store this id in the keychain.

    Fortunately there are already some people that have created a little library to achieve this. The code can be found on GitHub:

    https://github.com/gekitz/UIDevice-with-UniqueIdentifier-for-iOS-5

    0 讨论(0)
提交回复
热议问题