Swift - generate a one-time UUID

In quick, this code generates random uuid ...

let uuid = UUID().uuidString 

I have this when I click the button, it gets uuid and prints it to the console, however, when I click the button again, I get another uuid. I want to save the UUID that I get when I click the button on the line the first time ... Is this possible? Thanks!

+5
source share
2 answers

Move the let uuid declaration outside the scope of your button every time you click the button, you will get another unique identifier

+4
source

UUID always returns a value.

If you want to use only one time for your application, you need to save it to iPhone Keychain. Check out the Keychain tutorial.

Follow the instructions to save to Keychain: 1) Get data from the keychain.

2) If you do not receive the data, generate a new UUID and save it in Keychain and use it.

3) If you get a UUID, then use it right away.

You can kaychain demo and demo

+4
source

Source: https://habr.com/ru/post/1264623/


All Articles