[Objective-C] UUID on iOS 5

Hi all,
today a simple way to get device UID from code because in iOS5, function uniqueIdentifier was deprecated.

Src: UIDevice/uniqueIdentifier

This is my way, using CFUUID class (reference here), but there is surely a better method to do this.
This method is valid until user not delete app from device and reinstall!:

CODE

– Create a new UUID
– Store it to UserDefault
– Get it from UserDefault

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]+ (NSString*) GetUID {
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
return [(NSString *)string autorelease];
}

NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) {
[standardUserDefaults setObject:[YourClass GetUID] forKey:@"UID"];
[standardUserDefaults synchronize];
}

+ (NSString*) UID {
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) return [standardUserDefaults objectForKey:@"UID"];
}[/code]

Make sure you’re saving only ONE time uid to local storage. GetUID method, create a new one every time.


LIBRARY

If you want an easy library to include in your projects, you can download version 0.1 from zip.

:: DOWNLOAD ::

How to use?

:: Add in your project framework the library (libAPUUID.a file)
:: Add APUUID.h in your project.

To get UUID:

[code lang=”cpp” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]#import "APUUID.h"
[…]
NSLog(@"%@", [APUUID uniqueIdentifier]);[/code]

easy way. It works on iOS >= 3.0. On Device and Simulator.

Soon on github…

Hope this helps.

 

Alberto Pasca

Software engineer @ Pirelli & C. S.p.A. with a strong passion for mobile  development, security, and connected things.