Uniquely identifying an iOS user

后端 未结 10 1914
小蘑菇
小蘑菇 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: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.

提交回复
热议问题