Jak utworzyć GUID/UUID za pomocą iPhone SDK

Chcę mieć możliwość tworzenia GUID/uuid na iPhonie i iPadzie.

Intencją jest możliwość tworzenia kluczy dla rozproszonych danych, które są unikalne. Czy istnieje sposób, aby to zrobić z iOS SDK?

Author: Basil Bourque, 2009-01-09

8 answers

[[UIDevice currentDevice] uniqueIdentifier]

Zwraca unikalny identyfikator twojego iPhone ' a.

EDIT: -[UIDevice uniqueIdentifier] jest teraz przestarzały, a aplikacje są odrzucane ze sklepu App Store za korzystanie z niego. Metoda poniżej jest obecnie preferowanym podejściem.

Jeśli chcesz utworzyć kilka UUID, po prostu użyj tej metody (Z ARC):

+ (NSString *)GetUUID
{
  CFUUIDRef theUUID = CFUUIDCreate(NULL);
  CFStringRef string = CFUUIDCreateString(NULL, theUUID);
  CFRelease(theUUID);
  return (__bridge NSString *)string;
}

Edycja: Sty, 29 2014: Jeśli celujesz w system iOS 6 lub nowszy, możesz teraz użyć znacznie prostszej metody:]}

NSString *UUID = [[NSUUID UUID] UUIDString];
 312
Author: Stephan Burlot,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-18 02:12:29

Oto prosty kod, którego używam, zgodny z ARC.

+(NSString *)getUUID
{
    CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault);
    NSString * uuidString = (__bridge_transfer NSString*)CFUUIDCreateString(kCFAllocatorDefault, newUniqueId);
    CFRelease(newUniqueId);

    return uuidString;
}
 100
Author: trillions,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-23 00:51:12

W iOS 6 możesz łatwo użyć:

NSUUID  *UUID = [NSUUID UUID];
NSString* stringUUID = [UUID UUIDString];

Więcej szczegółów w Dokumentacja Apple

 85
Author: Arian Sharifian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-23 05:19:40

Przeglądając dokumentację Apple Developer znalazłem obiekt CFUUID jest dostępny na iPhone OS 2.0 i nowszych.

 46
Author: Henk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-15 12:09:31

Najprostszą techniką jest użycie NSString *uuid = [[NSProcessInfo processInfo] globallyUniqueString]. Zobacz odniesienie do klasy NSProcessInfo .

 22
Author: Ryan McCuaig,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-03 21:48:33

In Swift :

var uuid: String = NSUUID().UUIDString
println("uuid: \(uuid)")
 20
Author: King-Wizard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-30 23:07:53

In Swift 3.0

var uuid = UUID().uuidString
 13
Author: Radu Diță,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-01 14:14:22

Wgrałem tutaj moją prostą, ale szybką implementację klasy Guid dla ObjC: Obj-C GUID

Guid* guid = [Guid randomGuid];
NSLog("%@", guid.description);

Może również parsować do i z różnych formatów łańcuchów.

 7
Author: tumtumtum,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-18 11:34:09