Tutorial description
Importance of device data
First, we need to clarify the question: why do we need to access device data?
If you encounter problems during use, mobile app users often send their experiences or help information to developers by email. In this case, the more detailed the email content, the larger the information, and the more specific the description of the device and system environment, the easier it is for developers to provide scientific solutions. For example, if your application needs to be connected to the Internet to run, and a user sends an email on a walking trip saying that the application cannot work properly, the conclusion becomes very clear: users cannot access the Internet through hot spots, so applications cannot be started smoothly. In addition, it is helpful to understand the iOS version, application version, and country and language settings used by users. This allows developers to quickly identify the device status of users and simplify the diagnostic process for application problems.
In addition to the bug diagnosis and problem tracking mentioned above, access information through UIDevice or NSLocale can also effectively improve the appearance and actual performance of the application interface, for example, automatically setting different styles based on the user's location.
Measure the test taker's understanding about the UIDevice class.
The initial meeting with the UIDevice class may come from the targeting function of the mobile device. However, in addition, UIDevice provides a variety of other attributes, class functions, and notification modes to help us fully understand the device status. From detecting battery power to locating the distance between a device and the user's face, UIDevice provides the application with all information about the user and the device. The UIDevice class can also collect various details about devices, such as models and iOS versions. In the next tutorial, you will find that most of the attributes play a positive role in the development work.
Understanding NSLocale class
NSLocale helps our applications provide the most satisfactory service results based on the user's culture and language habits. By promptly adjusting the currency type, decimal point separator, time and date format, the application can bring users more intelligent work than expected, and all of this is a humanistic factor that must be considered by successful software. In addition, NSLocale can also be used for the geographical location of users. When users contact developers for help, the location is also one of the important support information.
Get device information
Let's start with getting the iPhone, iPodTouch, and iPad device models) and iOS versions. The following code obtains the specific device model through UIDevice.
- UIDevice *currentDevice = [UIDevice currentDevice];
- NSString *model = [currentDevice model];
- NSString *systemVersion = [currentDevice systemVersion];
In the first line, UIDevice returns the real-time running status instance of the User device. Based on this, we can access the model, system version, and other property information to master the most critical device data.
Obtain the user language type
Next, we try to retrieve the user's language type and location settings.
- NSArray *languageArray = [NSLocale preferredLanguages];
- NSString *language = [languageArray objectAtIndex:0];
- NSLocale *locale = [NSLocale currentLocale];
- NSString *country = [locale localeIdentifier];
To obtain the user's preferred language, we need to use the prefferedLanguages class function in NSLocale, which can return information in the user's current language settings. The user's current location, country, and region information is obtained through the NSLocale object. In the instance, the class function localeIdentifier returns a specific code containing the country/region of the user.
Obtain Application version information
Finally, let's see which version of the application the user is using.
- NSString *appVersion = [[NSBundle mainBundle]
- objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];
The info. plist file in the application stores the current application version information. You only need to use the kCFBundleVersionKey to access the mainBundle and return the correct application version result.
Record Device data
Next, we will summarize and record the collected device data.
- NSString *deviceSpecs =
- [NSString stringWithFormat:@"%@ - %@ - %@ - %@ - %@",
- model, systemVersion, language, country, appVersion];
-
- NSLog(@"Device Specs --> %@",deviceSpecs);
In the preceding code example, each piece of information is packaged into a string so that it can be directly displayed on the console. In an instance application, you may want this information to be displayed in the feedback email in the form of a title or text.
Summary
In the communication with application users, the more device information the developer has, the more specific the device information, the easier it is to propose a solution. Users' technical skills are uneven, so it is often difficult to accurately express their troubles. By understanding their device status in detail, developers can better help them solve problems and provide users with better services and product fun. In addition to the reasons mentioned at the beginning of the article, accessing device data can also help us better coordinate the interaction between multiple applications in the same system environment. If you have any questions or suggestions, share them with other readers in the comment bar.
Http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-accessing-device-data-with-uidevice-and-nslocale/