This tutorial demonstrates how to access key iOS device and user information, such as the device model, iOS version number, the user’s selected language, and more. This information can be useful in diagnosing application problems or in providing customized user experiences within your app.
Importance of Device Data
So, why do you want access the device data?
App users often e-mail developers with feedback or to get help if they have a problem. It can be useful to get more information about the user’s device and environment without having to ask them. For example, if your app requires Internet connectivity and a user with an iPod touch emails you saying the app is not working properly while hiking, you can quickly deduce that this is likely because the user did not have Internet access. In addition, knowing the iOS version, app version, and the user’s country and language gives you even more insight into the user, his device, and what may be contributing to the problem the user encountered.
In addition to the bug and issue tracking scenario presented above, accessing information from UIDevice or NSLocale may also prove useful for customizing your application interface or behavior.
Understanding the UIDevice
Class
You may recognize the UIDevice
class from encounters with device orientation, but UIDevice
offers a variety of properties, methods, and notifications that offer insight into a device. From tracking battery levels to determining the device’s proximity to the user’s face, the purpose of UIDevice
is to provide data about a user’s device. The UIDevice
class also provides accessible properties relating to identifying specifics about a device, such as the model and iOS version. You will see some of these properties in action as we get into the tutorial.
Insight Into the NSLocale
Class
The NSLocale
class helps your app conform to the cultural and linguistic conventions of a user’s region. By providing the means to adjust formatting for things like currency symbols, decimal separators, time, or date, an app will function as the user expects. In addition, NSLocale
can be used to find out which locale a user is set to, a helpful piece of information when a user emails you for support.
Getting the Device Information
Let’s start by getting the device’s model (iPhone, iPod touch, or iPad) and iOS version. The following code provides device specific information from UIDevice
:
UIDevice *currentDevice = [UIDevice currentDevice]; NSString *model = [currentDevice model]; NSString *systemVersion = [currentDevice systemVersion];
In the first line, UIDevice
returns an instance of the device in its current state. From here, we access the model and system version properties to get the device’s model and system version.
Getting the User’s Language
Next, we retrieve specifics about the user’s language and region settings.
NSArray *languageArray = [NSLocale preferredLanguages]; NSString *language = [languageArray objectAtIndex:0]; NSLocale *locale = [NSLocale currentLocale]; NSString *country = [locale localeIdentifier];
The user’s primary language can be obtained by using the NSLocale
method preferredLanguages
, which returns the user’s current language setting. The user’s locale, or country/region, can be obtained from the returned NSLocale
object. The instance method localeIdentifier
returns a specific code that represents the user’s country/region.
Getting the App Version
Finally, let’s find out what version of the app the user is running.
NSString *appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];
The app’s info.plist file stores the current app version information, and by accessing the mainBundle
using the kCFBundleVersionKey
, the app version will be returned.
Logging the Device Data
Next we’ll log the data collected.
NSString *deviceSpecs = [NSString stringWithFormat:@"%@ - %@ - %@ - %@ - %@", model, systemVersion, language, country, appVersion]; NSLog(@"Device Specs --> %@",deviceSpecs);
In this example, each piece of information is packaged into a string to be displayed in the console. In practical use, you may want to display it in the subject line or body of a feedback email.
Conclusion
When corresponding with app users, it is better to know more about the device if you need to offer solutions. Users often have a hard time articulating the scenario they are having trouble with. By knowing more about their device, you will be better able to help them with their issue, ultimately providing better service and generating happier users. Accessing device data is also useful for creating custom tailored interactions within your app. Questions or comments? Leave them in the comments section below or message me on Twitter @aaron_crabtree.