Creating UUID and UDID in iOS 7
Asked Answered
C

5

5

I want to create UUID, I have code below which can create UUID, how can I create UDID with multiple vendors same ID in iOS7?

+ (NSString*) stringWithNewUUID
{
    CFUUIDRef uuidObj = CFUUIDCreate(nil);
    NSString *newUUID = (NSString*)CFUUIDCreateString(nil, uuidObj);
    CFRelease(uuidObj);
    return newUUID;
}
Correlate answered 9/10, 2013 at 6:41 Comment(5)
Can you tell us please why do you want to do that?Withstand
I have some business requirementCorrelate
@SandeepKhade Why do you say it contains timestamp? CFUUID documentation does not mention anything about timestamp.Selfinductance
CFUUID is generated using the unique identifier of the computer(MAC address) and the 100-nanosecond interval - so in that case why don't you just take the MAC address and create your own UUID?Withstand
I think he is referring to version 1 UUIDs (which include the MAC address and timestamp) vs. version 4 UUIDs (which are random). If you look at the CFUUIDCreate() source code, it should use uuid_generate_random() to generate the UUID as long as you are linking on 10.4+.Last
C
5

I have created a vendor ID and then saved it with keychain, which I am retaining for next time using KeychainWrapper keychainStringFromMatchingIdentifier:...

Correlate answered 11/10, 2013 at 5:35 Comment(2)
Did Apple approve this method?Wein
Yes, If you do need the new UUID to last beyond app deletion and reinstall, the value could be stored in iOS’s keychain insteadHampshire
R
5

The CFUUIDCreate function produces a version 4 UUID which is taken entirely from a pseudo-random number generator. There are no timestamps or MAC addresses embedded in this type of UUID. (That refers to the little-used version 1 flavour.) These are safe to use for nearly all applications.

Rotten answered 9/10, 2013 at 6:59 Comment(2)
Yep, see opensource.apple.com/source/CF/CF-744/CFUUID.c Technically, CFUUIDCreate() can create version 1 uuids, but the default is version 4 (just tested on iOS 7 and OS X 10.8)Last
Thanks I was looking to create UDID for device not for vendor IDCorrelate
P
5

This method returns random UUID in iOS 6 and above

[[UIDevice currentDevice]identifierForVendor]
Philosophical answered 9/10, 2013 at 9:53 Comment(5)
Thanks Arun,I am looking to create unique identifier for my Device not for vendorCorrelate
That isn't what it means. -identifierForVendor ID that is identical between apps from the same developer.Basrhin
@CrossphireDevelopment: The identifierForVendor method does return different values in different devices regardless of the vendor. Apple Documentation: "The value of this property is the same for apps that come from the same vendor running on the same device. A different value is returned for apps on the same device that come from different vendors, and for apps on different devices regardless of vendor."Fatima
@ArminM I wasn't referring to multiple devices, just the current device. It would be useless if it remained the same across devices, I was talking about consistency on the same device which would make it behave like a UDID.Basrhin
Word of warning, identifierForVendor will change if ALL of that vendor's apps are removed from the device before installing another.Inflection
C
5

I have created a vendor ID and then saved it with keychain, which I am retaining for next time using KeychainWrapper keychainStringFromMatchingIdentifier:...

Correlate answered 11/10, 2013 at 5:35 Comment(2)
Did Apple approve this method?Wein
Yes, If you do need the new UUID to last beyond app deletion and reinstall, the value could be stored in iOS’s keychain insteadHampshire
F
2

The UUID that the code produces above does not have a recoverable time stamp in it. It's just a string that looks something like this: E1D87006-7CD0-4E28-9768-624DA92F75D6

Frontpage answered 9/10, 2013 at 6:50 Comment(1)
yeah @claireware it's in form of stringCorrelate
C
0

I followed Sandeep Khade's answer and made following code using PDKeychainBindings. It's same as using NSUserDefaults but it keeps identifier in keychain which saves data even when application is deleted.

+ (NSString *)uniqueVendor {

    PDKeychainBindings *keychain = [PDKeychainBindings sharedKeychainBindings];
    NSString *uniqueIdentifier = [keychain objectForKey:kKeyVendor];

    if (!uniqueIdentifier || !uniqueIdentifier.length) {

        NSUUID *udid = [[UIDevice currentDevice] identifierForVendor];
        uniqueIdentifier = [udid UUIDString];
        [keychain setObject:uniqueIdentifier forKey:kKeyVendor];
    }

    return uniqueIdentifier;
}
Cellulose answered 26/2, 2014 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.