Have you ever wanted a way to uniquely identify your user’s device? This is something that comes in handy when you are trying to implement a data syncing scheme or if you want to provide unique web based content to your user. If this sounds like something you want to do, read on to find out how.
Get A Reference to the Current Device
UIDevice
is a class that stores information about the physical device that is running your app. You use UIDevice
to get important information by using the currentDevice
function. This is return a UIDevice
object filled with information about the device that is running. Here is how to get this reference:
UIDevice *device = [UIDevice currentDevice];
Here is What UIDevice Will Tell You
UIDevice
objects will store more information that a simple unique identifier. You may use this object to get all kinds of information that will come in handy in this world of multiple devices. Here is what you get:
name
– what the user named the device model
– iPhone, iPod or iPad systemName
– pretty much “iPhone OS” systemVersion
– tells you what iPhone OS version the system is running orientation
– what direction the device is currently facing uniqueIdentifier
– a string
unique to each device based on information from the hardware itself
uniqueIdentifier Can Be Pretty Useful
You retrieve all of this information in the same way by accessing the UIDevice
object properties. Here is how to get the unique identifier from the device object we referenced above:
NSString *deviceID = device.uniqueIdentifier;
And of course, you may use this string
in the normal ways:
NSLog(@"%@", deviceID);
You will get something that looks a bit like this:
XXX70A50-ER31-X3X3-BDB2-3ED40EKIUN4D
Why Do It?
Of course, the why is up to you. Something that I have thought about is the situation where I want to be able to store user’s data for syncing purposes. You may also want to have your users share data or content with other apps, websites and so on. A simple way to start implementing this functionality could be to use this string
to create a folder (or database primary key) to store your user’s data. Of course, you will need to do a bit more work on top of this as well.
What do you think – useful trick?