iPhone:Create a unique identifier (UUID) for ios using CFUUIDCreate | Coding Cluster - using asp.net, c#, mvc 4, iphone, php, ios, javascript, in asp.net mvc 3 & more
 

iPhone:Create a unique identifier (UUID) for ios using CFUUIDCreate

Friday

 How to Create Unique identifier for an iPhone app:
                                                                      The UIDevice uniqueIdentifier property is deprecated in iOS 5 and above and it raise the following questions in our mind.

How to create a GUID/UUID using the iPhone SDK?

iOS 5 deprecates UDID as identifier for developers - What To Do Now?  


                                            The answer is simple and same for all above questions. That is, we can create UUID by our own code to solve this problems. Call the CFUUIDCreate function to create a UUID.
Because,

[[UIDevice currentDevice] uniqueIdentifier];

   used to returns the Unique ID of our iPhone. This is now deprecated and apps are being rejected from the App Store for using it. Create UUID is now the preferred approach.

                       The UUID created by CFUUIDCreate is unique if a user uninstalls and re-installs the app, you will get a new one each time. It means the newly created UUID doesn't change until you delete the app. If you delete the app the UUID also gone and get new one when you re-install the app (It will not change while close and re-open the apps).

Create a UUID, and write it to the defaults database using the NSUserDefaults class:
                      To create a unique identifier specific to your app, you can call the CFUUIDCreate function to create a UUID, and write it to the defaults database using the NSUserDefaults class.

Here is a code for create UUID,

// Create unique identifier for this app(UUID) by using CFUUIDCreate and stored into the NSUserDefaults

- (NSString *)createUUID { 
    
    NSString *uIdentifier = [[NSUserDefaults standardUserDefaults] objectForKey:@"Unique identifier for myApp"];
 
    if (!uIdentifier) {
        
        CFUUIDRef uuidRef = CFUUIDCreate(NULL);
        
        CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
        
        CFRelease(uuidRef);
        
        uIdentifier = [NSString stringWithString:(NSString *)uuidStringRef];
        
        CFRelease(uuidStringRef);
        
        [[NSUserDefaults standardUserDefaults] setObject:uIdentifier forKey:@"Unique identifier for myApp"];
        
        [[NSUserDefaults standardUserDefaults] synchronize];
        
    }
    
    return uIdentifier;    
}

And get the UUID,

NSString *myAppUniqueID =[self createUUID]; 


If this post  useful to you :  like codingcluster on face book . Thanks!.

0 comments:

Post a Comment

Share your thoughts here...

 
 
 

RECENT POSTS

Boost

 
Blogger Widgets