IOS SDK User Guide: Use UIDevice and NSLocale to access device data

Source: Internet
Author: User

Tutorial description

  • Tools used: iOS SDK

  • Execution difficulty: normal

  • Operation Time: 15 to 30 minutes

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.

 
 
  1. UIDevice *currentDevice = [UIDevice currentDevice]; 
  2. NSString *model = [currentDevice model]; 
  3. 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.

 
 
  1. NSArray *languageArray = [NSLocale preferredLanguages]; 
  2. NSString *language = [languageArray objectAtIndex:0]; 
  3. NSLocale *locale = [NSLocale currentLocale]; 
  4. 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.

 
 
  1. NSString *appVersion = [[NSBundle mainBundle] 
  2. 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.

 
 
  1. NSString *deviceSpecs = 
  2.     [NSString stringWithFormat:@"%@ - %@ - %@ - %@ - %@", 
  3.     model, systemVersion, language, country, appVersion]; 
  4.   
  5. 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/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.