Search code examples
swiftuuid

New UUID generated on each run


I have this code in my viewDidLoad:

let udid = UUID().uuidString

But each time I run the application I get a new generated UUID. How come?

I need the same ID for each user to be able to identify them so should I save this in a UserDefault or how should I do to uniquely identify a user even when the user restarts the application?


Solution

  • The tool for what you're trying to do is UIDevice.current.identifierForVendor. This returns exactly the kind of UUID you're describing (and is intended for this purpose).