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;
}
Sandeep Khade
  • 2,832
  • 3
  • 21
  • 37
  • Can you tell us please why do you want to do that? – ivan_yorgov Oct 09 '13 at 06:42
  • I have some business requirement – Sandeep Khade Oct 09 '13 at 06:43
  • @SandeepKhade Why do you say it contains timestamp? [CFUUID documentation](https://developer.apple.com/library/mac/documentation/corefoundation/Reference/CFUUIDRef/Reference/reference.html#//apple_ref/c/func/CFUUIDCreate) does not mention anything about timestamp. – Amar Oct 09 '13 at 06:51
  • 1
    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? – ivan_yorgov Oct 09 '13 at 06:55
  • 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+. – iccir Oct 09 '13 at 06:56

5 Answers5

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.

gavinb
  • 19,278
  • 3
  • 45
  • 60
  • 1
    Yep, see https://www.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) – iccir Oct 09 '13 at 07:04
  • 1
    Thanks I was looking to create UDID for device not for vendor ID – Sandeep Khade Oct 11 '13 at 05:34
5

This method returns random UUID in iOS 6 and above

[[UIDevice currentDevice]identifierForVendor]
Arun_
  • 1,806
  • 2
  • 20
  • 40
  • Thanks Arun,I am looking to create unique identifier for my Device not for vendor – Sandeep Khade Oct 11 '13 at 05:33
  • 1
    That isn't what it means. -identifierForVendor ID that is identical between apps from the same developer. – John McKnight Oct 14 '13 at 01:52
  • 1
    @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." – Armin Mar 10 '14 at 06:23
  • @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. – John McKnight Mar 12 '14 at 22:56
  • 1
    Word of warning, identifierForVendor will change if ALL of that vendor's apps are removed from the device before installing another. – Ash May 06 '14 at 07:44
5

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

Eric Platon
  • 9,819
  • 6
  • 41
  • 48
Sandeep Khade
  • 2,832
  • 3
  • 21
  • 37
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

kamprath
  • 2,220
  • 1
  • 23
  • 28
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;
}
Josip B.
  • 2,434
  • 1
  • 25
  • 30