Generate a UUID on iOS from Swift
Asked Answered
N

8

336

In my iOS Swift app I want to generate random UUID (GUID) strings for use as a table key, and this snippet appears to work:

let uuid = CFUUIDCreateString(nil, CFUUIDCreate(nil))

Is this safe?

Or is there perhaps a better (recommended) approach?

Natoshanatron answered 26/6, 2014 at 10:35 Comment(3)
You can also use let uuid = NSUUID.UUID().UUIDStringTshirt
Take a look at #3774276Tshirt
Instead of your edit, please accept one of the answers. Since you're doing it the way @AhemdAlHafoudh proposed, I suggest you accept his answer.Cree
H
696

Try this one:

let uuid = NSUUID().uuidString
print(uuid)

Swift 3/4/5

let uuid = UUID().uuidString
print(uuid)
Hyder answered 26/6, 2014 at 10:47 Comment(9)
In XCode 6.1, I found this error: 'UUID()' is unavilable: use object construction 'NSUUID()'. We should change it to NSUUID().UUIDStringFinicking
The above suggestion is no different than calling NSUUID.init().UUIDString, right?Akim
As of Xcode 8 (beta), iOS 10 and Swift 3, this has been renamed to UUID().uuidStringHogarth
Mind adding this to the answer?Hyder
Hi I used this method UUID().uuidString but it seems to return different string everytime i use it, I want it to be 1 value only for the app that I install. Any insight?Indifferentism
@Indifferentism - for getting the same value, use following code: let uuid = UIDevice.current.identifierForVendor?.uuidString it returns same string all time for the same device.Rashid
@Rashid - Not exactly, as per the documentation: "The value in this property remains the same while the app (or another app from the same vendor) is installed on the iOS device. The value changes when the user deletes all of that vendor’s apps from the device and subsequently reinstalls one or more of them...... Therefore, if your app stores the value of this property anywhere, you should gracefully handle situations where the identifier changes." - developer.apple.com/reference/uikit/uidevice/…Mechelle
@Mechelle - Yes, now I got exact meaning, thanks for the clarification :)Rashid
Note that the resultant string is uppercase. This is called out in @Celil's answer, but figured it'd be worth commenting here since it differs from other languages, and I've been bitten by it.Enclasp
B
31

You could also just use the NSUUID API:

let uuid = NSUUID()

If you want to get the string value back out, you can use uuid.UUIDString.

Note that NSUUID is available from iOS 6 and up.

Boneyard answered 26/6, 2014 at 10:45 Comment(0)
D
24

For Swift 4;

let uuid = NSUUID().uuidString.lowercased()
Davy answered 3/12, 2017 at 21:20 Comment(3)
Graveyard nitpick here but I don't know why lowercase UUID is preferable to a regular UUID.Requite
Because they are supposed to be lower case. as per spec. UUIDs can be read in either case, but should output lower case. iOS is the only platform that outputs upper case.Coquille
UUIDs are written in base 16 which uses numbers 0-9 and characters a-f. There is no distinction between upper and lowercase letters. However, RCF 4122 tools.ietf.org/html/rfc4122#section-3 requires that UUID generators output in lowercase BUT systems accept UUIDs in upper and lowercase.Seitz
G
17

For Swift 3, many Foundation types have dropped the 'NS' prefix, so you'd access it by UUID().uuidString.

Guadalupe answered 29/9, 2016 at 21:38 Comment(0)
P
11

Also you can use it lowercase under below

let uuid = NSUUID().UUIDString.lowercaseString
print(uuid)

Output

68b696d7-320b-4402-a412-d9cee10fc6a3

Thank you !

Painless answered 26/8, 2016 at 11:19 Comment(5)
Thanks. it's now NSUUID().uuidString.lowercased()Hold
What is the benefit of using lowercase like this?Repine
Perhaps it is more pleasing to the eye than uppercase?Mutter
the benefit is that it's supposed to output lower case, as all other platforms output lower case based on the spec. Apple is doing it wrong.Coquille
UUIDs are written in base 16 which uses numbers 0-9 and characters a-f. There is no distinction between upper and lowercase letters. However, RCF 4122 tools.ietf.org/html/rfc4122#section-3 requires that UUID generators output in lowercase BUT systems accept UUIDs in upper and lowercase.Seitz
N
9

Each time the same will be generated:

if let uuid = UIDevice.current.identifierForVendor?.uuidString {
    print(uuid)
}

Each time a new one will be generated:

let uuid = UUID().uuidString
print(uuid)
Northrup answered 3/10, 2019 at 21:42 Comment(1)
So, which one should i use for GADMobileAds?Schaller
P
3

UUID is a simple structure, which has the property uuidString. uuidString - returns a string created from the UUID, such as “E621E1F8-C36C-495A-93FC-0C247A3E6E5F”.

UUID is guaranteed to be unique.

Swift code:

let identifier = UUID().uuidString
Swift.print(identifier) // Result: "6A967474-8672-4ABC-A57B-52EA809C5E6D"

Apple official documentation about UUID.
Full article https://tonidevblog.com/posts/how-to-generate-a-random-unique-identifier-with-uuid/

Peay answered 11/9, 2021 at 11:27 Comment(0)
L
1

Under macOS 13 and Swift 5.7, Foundation's UUID() method does indeed return an upper case UUID, and Vapor Fluent is at least one framework that uses it as is. So, when you create a new record with an id=nil, Fluent will get a new uppercase UUID and store the new record with it. MySQL, on the other hand, generates lowercase UUIDs, and will store the lowercase UUID if you create the record in MySQL. MySQL and SQLite both treat the uppercase and lowercase keys as DIFFERENT. Caveat Emptor.

BUT... if you use the UUID from a previous data read, it shouldn't matter if it was saved with an upper or lower case UUID. You just have to be sure not to change the key's case.

Latinize answered 5/8, 2022 at 22:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.