iOS開發(fā)-獲取設(shè)備信息和應(yīng)用信息

目錄

  1. UIDevice
    1. 設(shè)備相關(guān)信息
    2. 設(shè)備電量
    3. 近距離傳感器
    4. 設(shè)備性能、界面模式
  2. NSBundle
    1. 獲取應(yīng)用名稱
    2. 應(yīng)用短版本號
    3. 應(yīng)用Build版本號
    4. 應(yīng)用identifier
  3. NSLocale
    1. 創(chuàng)建本地化對象
    2. 獲取系統(tǒng)本地化信息
    3. 獲取本地標(biāo)識信息和語言碼信息
    4. 獲取當(dāng)前語言的排版方向和字符方向
    5. 獲取用戶的語言偏好設(shè)置列表
    6. 監(jiān)聽用戶本地化設(shè)置的消息
    7. 以本地化方式獲取國際化信息的顯示名稱

UIDevice

UIDevice提供了多種屬性、類函數(shù)及狀態(tài)通知,幫助我們?nèi)轿涣私庠O(shè)備狀況。從檢測電池電量到定位設(shè)備與臨近感應(yīng),UIDevice所做的工作就是為應(yīng)用程序提供用戶及設(shè)備的一些信息UIDevice的屬性如下:

  • 設(shè)備相關(guān)信息
// 手機(jī)中關(guān)于本機(jī)中設(shè)置的名稱  @"My iPhone" 
@property(nonatomic,readonly,strong) NSString    *name;     
// 設(shè)備模式  @"iPhone", @"iPod touch",@"iPad"      
@property(nonatomic,readonly,strong) NSString    *model;
// 本地設(shè)備模式,返回值與model相同,暫不清楚兩者區(qū)別
@property(nonatomic,readonly,strong) NSString    *localizedModel;
// 系統(tǒng)名字  @"iOS"
@property(nonatomic,readonly,strong) NSString    *systemName;
// 系統(tǒng)版本號  @"10.2.1"
@property(nonatomic,readonly,strong) NSString    *systemVersion;
// 獲取設(shè)備唯一標(biāo)識,同一個開發(fā)商的APP獲取到的標(biāo)識是相同的
// 與UDID不同的是,在我們刪除了設(shè)備上,同一個開發(fā)商的所有APP之后,下次獲取到的將是不同的標(biāo)識
@property(nullable, nonatomic,readonly,strong) NSUUID      *identifierForVendor NS_AVAILABLE_IOS(6_0);
// 設(shè)備方向. UIDeviceOrientation是一個枚舉
typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,             // 未知
    UIDeviceOrientationPortrait,            // 豎屏,home鍵在下方
    UIDeviceOrientationPortraitUpsideDown,  // 豎屏,home鍵在上方
    UIDeviceOrientationLandscapeLeft,       // 橫屏,home鍵在右方
    UIDeviceOrientationLandscapeRight,      // 橫屏,home鍵在左方
    UIDeviceOrientationFaceUp,              // 平放,屏幕朝上
    UIDeviceOrientationFaceDown             // 平放,屏幕朝下
} __TVOS_PROHIBITED;
@property(nonatomic,readonly) UIDeviceOrientation orientation __TVOS_PROHIBITED;

獲取設(shè)備方向

UIDevice *device = [UIDevice currentDevice];
// 判斷設(shè)備是否生成設(shè)備轉(zhuǎn)向通知
BOOL generatesDeviceOrientationNotifications = device.isGeneratingDeviceOrientationNotifications;
// 開啟設(shè)備轉(zhuǎn)向通知
// 通過調(diào)用該方法通知設(shè)備:如果用戶改變了設(shè)備的朝向,我們想獲悉這一點(diǎn)
// 在注冊設(shè)備方向通知時,需要先調(diào)用該方法
[device beginGeneratingDeviceOrientationNotifications];
// 注冊屏幕方向變化通知
[[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(deviceOrientationDidChanged:) 
                                                 name:@"UIDeviceOrientationDidChangeNotification"
                                               object:nil];
// 獲取設(shè)備方向
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
// 停止設(shè)備轉(zhuǎn)向通知
// 在移除設(shè)備方向通知后,需要調(diào)用該方法
[device endGeneratingDeviceOrientationNotifications];

獲取設(shè)備硬件類型,有三種方法,如下:
1)這種是在較高層次獲取設(shè)備類型,返回的是 iPhone , iPod , iPad 。適合要求不高的。

NSString *deviceType = [[UIDevice currentDevice] model];

2)這是Linux中獲取設(shè)備類型的方法,主要是C語言的方法,注意引入頭文件#include <sys/sysctl.h>。輸入底層獲取設(shè)備類型的方法。

size_t size;
// 獲取machine name的長度
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
// 獲取machine name
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
// 設(shè)備硬件類型
NSString *platform = [NSString stringWithFormat:@"%s", machine];
free(machine);

3)這和2)一樣,是Linux中獲取設(shè)備類型的方法,主要是C語言的方法,注意引入頭文件#import "sys/utsname.h"。輸入底層獲取設(shè)備類型的方法。

struct utsname systemInfo;
uname(&systemInfo);
// 設(shè)備硬件類型
NSString *platform = [NSString stringWithFormat:@"%s", systemInfo.machine];

以上2),3)方法中返回的platform(設(shè)備硬件類型)的對應(yīng)關(guān)系如下:
(官方參數(shù)表鏈接:https://www.theiphonewiki.com/wiki/Models

// Simulator
@"i386":@"Simulator",
@"x86_64":@"Simulator",

// iPhone
@"iPhone1,1":@"iPhone 1",
@"iPhone1,2":@"iPhone 3",
@"iPhone2,1":@"iPhone 3S",
@"iPhone3,1" || @"iPhone3,2" || @"iPhone3,3":@"iPhone 4",
@"iPhone4,1":@"iPhone 4S",
@"iPhone5,1":@"iPhone 5",
@"iPhone5,2":@"iPhone 5",         // iPhone 5 (GSM+CDMA)
@"iPhone5,3":@"iPhone 5C",      // iPhone 5c (GSM)
@"iPhone5,4":@"iPhone 5C",      // iPhone 5c (GSM+CDMA)
@"iPhone6,1":@"iPhone 5S",       // iPhone 5s (GSM)
@"iPhone6,2":@"iPhone 5S",      // iPhone 5s (GSM+CDMA)
@"iPhone7,1":@"iPhone 6 Plus",
@"iPhone7,2":@"iPhone 6",
@"iPhone8,1":@"iPhone 6s",
@"iPhone8,2":@"iPhone 6s Plus",
@"iPhone8,3" || @"iPhone8,4":@"iPhone SE",
@"iPhone9,1":@"iPhone 7",       // 國行、日版、港行iPhone 7
@"iPhone9,3":@"iPhone 7",       // 美版、臺版iPhone 7
@"iPhone9,2":@"iPhone 7 Plus",  // 港行、國行iPhone 7 Plus
@"iPhone9,4":@"iPhone 7 Plus",  // 美版、臺版iPhone 7 Plus
@"iPhone10,1":@"iPhone 8",         // 國行(A1863)、日行(A1906)iPhone 8
@"iPhone10,4":@"iPhone 8",        // 美版(Global/A1905)iPhone 8
@"iPhone10,2":@"iPhone 8 Plus",  // 國行(A1864)、日行(A1898)iPhone 8 Plus
@"iPhone10,5":@"iPhone 8 Plus",  // 美版(Global/A1897)iPhone 8 Plus
@"iPhone10,3":@"iPhone X",          // 國行(A1865)、日行(A1902)iPhone X
@"iPhone10,6":@"iPhone X",          // 美版(Global/A1901)iPhone X  
@"iPhone11,2":@"iPhone XS", 
@"iPhone11,4":@"iPhone XS Max",  // 國行iPhone XS Max
@"iPhone11,6":@"iPhone XS Max",  // 美版iPhone XS Max
@"iPhone11,8":@"iPhone XR",  
@"iPhone12,1":@"iPhone 11",
@"iPhone12,3":@"iPhone 11 Pro",
@"iPhone12,5":@"iPhone 11 Pro Max", 
@"iPhone12,8":@"iPhone SE 2",
@"iPhone13,1":@"iPhone 12 mini", 
@"iPhone13,2":@"iPhone 12", 
@"iPhone13,3":@"iPhone 12 Pro",
@"iPhone13,4":@"iPhone 12 Pro Max", 
@"iPhone14,2":@"iPhone 13 Pro",
@"iPhone14,3":@"iPhone 13 Pro Max", 
@"iPhone14,4":@"iPhone 13 mini", 
@"iPhone14,5":@"iPhone 13",
@"iPhone14,6":@"iPhone SE 3", 
@"iPhone14,7":@"iPhone 14", 
@"iPhone14,8":@"iPhone 14 Plus",
@"iPhone15,2":@"iPhone 14 Pro", 
@"iPhone15,3":@"iPhone 14 Pro Max",

// iPod Touch
@"iPod1,1":@"iPod Touch 1G",
@"iPod2,1":@"iPod Touch 2G",
@"iPod3,1":@"iPod Touch 3G",
@"iPod4,1":@"iPod Touch 4G",
@"iPod5,1":@"iPod Touch (5 Gen)",
@"iPod7,1":@"iPod Touch 6",

// iPad
@"iPad1,1":@"iPad 1",
@"iPad1,2":@"iPad 3G",
@"iPad2,1":@"iPad 2",        // iPad 2 (WiFi)
@"iPad2,2" || @"iPad2,4" : @"iPad 2",
@"iPad2,3":@"iPad 2",        // iPad 2 (CDMA)
@"iPad2,5":@"iPad Mini 1",   // iPad Mini (WiFi)
@"iPad2,6":@"iPad Mini 1",
@"iPad2,7":@"iPad Mini 1",   // iPad Mini (GSM+CDMA)
@"iPad3,1":@"iPad 3",        // iPad 3 (WiFi)
@"iPad3,2":@"iPad 3",        // iPad 3 (GSM+CDMA)
@"iPad3,3":@"iPad 3",
@"iPad3,4":@"iPad 4",        // iPad 4 (WiFi)
@"iPad3,5":@"iPad 4",
@"iPad3,6":@"iPad 4",        // iPad 4 (GSM+CDMA)
@"iPad4,1":@"iPad air",      // iPad Air (WiFi)
@"iPad4,2":@"iPad air",      // iPad Air (Cellular)
@"iPad4,3":@"iPad air",
@"iPad4,4":@"iPad mini 2",   // iPad Mini 2 (WiFi)
@"iPad4,5":@"iPad mini 2",   // iPad Mini 2 (Cellular)
@"iPad4,6":@"iPad mini 2",
@"iPad4,7" || @"iPad4,8" || @"iPad4,9" : @"iPad mini 3",
@"iPad5,1":@"iPad mini 4",   // iPad Mini 4 (WiFi)
@"iPad5,2":@"iPad mini 4",   // iPad Mini 4 (LTE)
@"iPad5,3" || @"iPad5,4" : @"iPad air 2",
@"iPad6,3" || @"iPad6,4" : @"iPad Pro 9.7",
@"iPad6,7" || @"iPad6,8" : @"iPad Pro 12.9",
@"iPad6,11":@"iPad 5",        // iPad 5 (WiFi)
@"iPad6,12":@"iPad 5",        // iPad 5 (Cellular)
@"iPad7,1":@"iPad Pro 12.9 inch 2nd gen", // iPad Pro 12.9 inch 2nd gen (WiFi)
@"iPad7,2":@"iPad Pro 12.9 inch 2nd gen", // iPad Pro 12.9 inch 2nd gen (Cellular)
@"iPad7,3":@"iPad Pro 10.5 inch", // iPad Pro 10.5 inch (WiFi)
@"iPad7,4":@"iPad Pro 10.5 inch", // iPad Pro 10.5 inch (Cellular)
@"iPad7,5":@"iPad 6th gen", 
@"iPad7,6":@"iPad 6th gen",
@"iPad7,11":@"iPad 7th gen", 
@"iPad7,12":@"iPad 7th gen", 
@"iPad11,6":@"iPad 8th gen",
@"iPad11,7":@"iPad 8th gen", 
@"iPad12,1":@"iPad 9th gen", 
@"iPad12,2":@"iPad 9th gen",
@"iPad13,18":@"iPad 10th gen", 
@"iPad13,19":@"iPad 10th gen",
@"iPad11,3":@"iPad Air 3rd gen",
@"iPad11,4":@"iPad Air 3rd gen", 
@"iPad13,1":@"iPad Air 4th gen", 
@"iPad13,2":@"iPad Air 4th gen",
@"iPad13,16":@"iPad Air 5th gen", 
@"iPad13,17":@"iPad Air 5th gen", 
@"iPad8,1":@"iPad Pro 11 inch",
@"iPad8,2":@"iPad Pro 11 inch", 
@"iPad8,3":@"iPad Pro 11 inch", 
@"iPad8,4":@"iPad Pro 11 inch",
@"iPad8,5":@"iPad Pro 12.9 inch 3rd gen", 
@"iPad8,6":@"iPad Pro 12.9 inch 3rd gen",
@"iPad8,7":@"iPad Pro 12.9 inch 3rd gen", 
@"iPad8,8":@"iPad Pro 12.9 inch 3rd gen",
@"iPad8,9":@"iPad Pro 11 inch 2nd gen", 
@"iPad8,10":@"iPad Pro 11 inch 2nd gen",
@"iPad8,11":@"iPad Pro 12.9 inch 4th gen", 
@"iPad8,12":@"iPad Pro 12.9 inch 4th gen",
@"iPad13,4":@"iPad Pro 11 inch 3rd gen", 
@"iPad13,5":@"iPad Pro 11 inch 3rd gen",
@"iPad13,6":@"iPad Pro 11 inch 3rd gen", 
@"iPad13,7":@"iPad Pro 11 inch 3rd gen",
@"iPad13,8":@"iPad Pro 12.9 inch 5th gen", 
@"iPad13,9":@"iPad Pro 12.9 inch 5th gen",
@"iPad13,10":@"iPad Pro 12.9 inch 5th gen", 
@"iPad13,11":@"iPad Pro 12.9 inch 5th gen",
@"iPad11,1":@"iPad mini 5th gen", 
@"iPad11,2":@"iPad mini 5th gen",
@"iPad14,1":@"iPad mini 6th gen", 
@"iPad14,2":@"iPad mini 6th gen"

// AppleTV
@"AppleTV1,1":@"Apple TV 1",
@"AppleTV2,1":@"Apple TV 2",
@"AppleTV3,1":@"Apple TV 3", 
@"AppleTV3,2":@"Apple TV 3",
@"AppleTV5,3":@"Apple TV 4",
@"AppleTV6,2":@"Apple TV 4K",
  • 設(shè)備電量
// 通過UIDevice類,可以取得電池剩余量以及充電狀態(tài)的信息,首先需要設(shè)置batteryMonitoringEnabled為YES  
@property(nonatomic,getter=isBatteryMonitoringEnabled) BOOL batteryMonitoringEnabled NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;// 默認(rèn)為NO
// 電池狀態(tài), UIDeviceBatteryState是個枚舉類型
typedef NS_ENUM(NSInteger, UIDeviceBatteryState) {
    UIDeviceBatteryStateUnknown,     // 無法取得充電狀態(tài)情況
    UIDeviceBatteryStateUnplugged,   // 非充電狀態(tài)
    UIDeviceBatteryStateCharging,    // 充電狀態(tài)[電量小于100%]
    UIDeviceBatteryStateFull,        // 充滿狀態(tài)[連接充電器充滿狀態(tài)]
} __TVOS_PROHIBITED;               // available in iPhone 3.0
@property(nonatomic,readonly) UIDeviceBatteryState batteryState NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; 
// 電池電量,取值范圍為0.0-1.0,當(dāng)電池狀態(tài)為UIDeviceBatteryStateUnknown時為-1.0
@property(nonatomic,readonly) float batteryLevel NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;

監(jiān)視設(shè)備電池狀態(tài)、電池電量變化情況

// 判斷設(shè)備是否開啟電池監(jiān)控
BOOL batteryMonitoringEnabled = device.isBatteryMonitoringEnabled;
// 開啟電池監(jiān)控
device.batteryMonitoringEnabled = YES;
// 注冊電池狀態(tài)變化通知
[[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(deviceBatteryStateDidChange) 
                                                 name:@"UIDeviceBatteryStateDidChangeNotification" 
                                               object:nil];
// 獲取設(shè)備電池狀態(tài)
UIDeviceBatteryState batteryState = device.batteryState;
// 注冊電池電量變化通知
[[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(deviceBatteryLevelDidChange) 
                                                 name:@"UIDeviceBatteryLevelDidChangeNotification" 
                                               object:nil];
// 獲取電池電量
float batteryLevel = device.batteryLevel; 
  • 近距離傳感器
// 設(shè)備接近狀態(tài)監(jiān)控開關(guān)(判斷設(shè)備是否開啟接近狀態(tài)監(jiān)控)
@property(nonatomic,getter=isProximityMonitoringEnabled) BOOL proximityMonitoringEnabled NS_AVAILABLE_IOS(3_0);// 默認(rèn)為NO
// 獲取接近狀態(tài)(如果設(shè)備不具備接近感應(yīng)器,則總是返回NO)
// 傳感器已啟動前提條件下,如果用戶接近近距離傳感器,此時屬性值為YES,并且屏幕已關(guān)閉(非休眠)。
@property(nonatomic,readonly) BOOL proximityState NS_AVAILABLE_IOS(3_0);

監(jiān)控設(shè)備接近狀態(tài)

// 判斷設(shè)備是否開啟接近狀態(tài)監(jiān)控
BOOL proximityMonitoringEnabled = device.isProximityMonitoringEnabled;
// 開啟接近狀態(tài)監(jiān)控
device.proximityMonitoringEnabled = YES;
// 注冊接近狀態(tài)變化通知
[[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(deviceProximityStateDidChange) 
                                                 name:@"UIDeviceProximityStateDidChangeNotification" 
                                               object:nil];
// 獲取接近狀態(tài)
BOOL proximityState = device.proximityState;
  • 設(shè)備性能、界面模式
// 判斷設(shè)備是否支持多任務(wù)
@property(nonatomic,readonly,getter=isMultitaskingSupported) BOOL multitaskingSupported NS_AVAILABLE_IOS(4_0);
// 獲取用戶界面模式
typedef NS_ENUM(NSInteger, UIUserInterfaceIdiom) {
    UIUserInterfaceIdiomUnspecified = -1,
    UIUserInterfaceIdiomPhone NS_ENUM_AVAILABLE_IOS(3_2),  // iPhone和iPod touch界面風(fēng)格
    UIUserInterfaceIdiomPad NS_ENUM_AVAILABLE_IOS(3_2),    // iPad界面風(fēng)格
    UIUserInterfaceIdiomTV NS_ENUM_AVAILABLE_IOS(9_0),     // Apple TV界面風(fēng)格
    UIUserInterfaceIdiomCarPlay NS_ENUM_AVAILABLE_IOS(9_0),// CarPlay界面風(fēng)格
};
@property(nonatomic,readonly) UIUserInterfaceIdiom userInterfaceIdiom NS_AVAILABLE_IOS(3_2); 

NSBundle

bundle是一個目錄,其中包含了程序會使用到的資源. 這些資源包含了如圖像,聲音,編譯好的代碼,nib文件(用戶也會把bundle稱為plug-in)。對應(yīng)bundle,cocoa提供了類NSBundle。一個應(yīng)用程序看上去和其他文件沒有什么區(qū)別,但是實(shí)際上它是一個包含了nib文件,編譯代碼,以及其他資源的目錄, 我們把這個目錄叫做程序的main bundle(當(dāng)前的可執(zhí)行app在根目錄下的絕對路徑)。通過這個路徑可以獲取到應(yīng)用的信息,例如應(yīng)用名、版本號等。常用的信息如下:

  • 獲取應(yīng)用名稱(顯示在手機(jī)屏幕上的應(yīng)用名字)。
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]; 

在TARGETS下面的Info里面有以下幾個屬性

[這里創(chuàng)建一個名為CJDeviceInfo的工程為例]
// App安裝到設(shè)備里的應(yīng)用文件夾名(CJDeviceInfo)
Bundle Name - ${PRODUCT_NAME}
// 應(yīng)用包名(com.circus.CJDeviceInfo)
Bundle Identifier - $(PRODUCT_BUNDLE_IDENTIFIER)
// 手機(jī)屏幕上的應(yīng)用名字,默認(rèn)為PRODUCT_NAME
Bundle Display Name - ${PRODUCT_NAME}
// 執(zhí)行程序名,默認(rèn)與PRODUCT_NAME一致
Executable File - ${EXECUTABLE_NAME}

想要修改顯示在手機(jī)屏幕上的應(yīng)用名字,可以直接修改info里的Bundle Display Name的值;但是如果要國際化應(yīng)用名稱,則需要創(chuàng)建InfoPlist.strings,步驟如下:
1)創(chuàng)建一個空文件,取名為InfoPlist.strings
2)對InfoPlist.strings進(jìn)行本地化(Get Info -> Make Localization),然后設(shè)置需要的語言(如中文zh)
3)編輯不同的InfoPlist.strings文件,設(shè)置顯示名字CFBundleDisplayName = "名字";
4)編輯Info.plist,添加一個新的屬性Application has localized display name,設(shè)置其類型為boolean,并將其value設(shè)置為true
注意: 上面獲取應(yīng)用名稱的方法,只能獲取到直接在info中設(shè)置的Bundle Display Name信息,獲取不到在InfoPlist.strings中設(shè)置的CFBundleDisplayName的信息。

  • 獲取InfoPlist.strings設(shè)置的國際化的應(yīng)用名稱方法如下:
NSLocalizedStringFromTableInBundle(@"CFBundleDisplayName", @"InfoPlist", [NSBundle mainBundle], comment);
  • 獲取應(yīng)用短版本號,如1.0(用戶看到的版本號,AppStore上的版本號)
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; 
  • 獲取應(yīng)用Build版本號,如1.0.1(公司內(nèi)部使用的版本號,在AppStore修改同一個版本的IPA文件時,可以通過自增Build版本號來實(shí)現(xiàn)上傳多個)
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];  
  • 獲取應(yīng)用identifier,如com.Circus.CJDeviceInfo(常用于需要同一份代碼打包成不同應(yīng)用,通過判斷identifier來實(shí)現(xiàn)使用不同的第三方key)
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"]; 

NSLocale

本地化封裝了關(guān)于語言,文化以及技術(shù)約定和規(guī)范的信息。用于提供與用戶所處地域相關(guān)的定制化信息和首選項(xiàng)信息的設(shè)置。NSLocale類封裝了本地化相關(guān)的各種信息,NSLocale可以獲取用戶的本地化信息設(shè)置,例如貨幣類型,國家,語言,數(shù)字,日期格式的格式化,提供正確的地理位置顯示等等。

  • 創(chuàng)建本地化對象
// 根據(jù)本地標(biāo)識符創(chuàng)建本地化對象
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
// 獲取當(dāng)前用戶設(shè)置的本地化對象
NSLocale *currentLocale = [NSLocale currentLocale];
  • 獲取系統(tǒng)本地化信息
// 獲取系統(tǒng)所有本地化標(biāo)識符數(shù)組列表
[NSLocale availableLocaleIdentifiers];
// 獲取所有已知合法的國家代碼數(shù)組列表
[NSLocale ISOCountryCodes];
// 獲取所有已知合法的ISO貨幣代碼數(shù)組列表
[NSLocale ISOCurrencyCodes];
// 獲取所有已知合法的ISO語言代碼數(shù)組列表
[NSLocale ISOLanguageCodes];
  • 獲取本地標(biāo)識信息和語言碼信息
// 本地標(biāo)識,如:en_US
[[NSLocale currentLocale] localeIdentifier];
等價于
[[NSLocale currentLocale] objectForKey:NSLocaleIdentifier];
// 語言碼,如:en
[[NSLocale currentLocale] objectForKey: NSLocaleLanguageCode];
  • 獲取當(dāng)前語言的排版方向和字符方向
// 語言的排版方向
[NSLocale lineDirectionForLanguage:[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode];
// 字符方向
[NSLocale characterDirectionForLanguage:[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode];
// 返回結(jié)果為枚舉類型
typedef NS_ENUM(NSUInteger, NSLocaleLanguageDirection) {
    NSLocaleLanguageDirectionUnknown = kCFLocaleLanguageDirectionUnknown,         // 未知
    NSLocaleLanguageDirectionLeftToRight = kCFLocaleLanguageDirectionLeftToRight, // 從左向右
    NSLocaleLanguageDirectionRightToLeft = kCFLocaleLanguageDirectionRightToLeft, // 從右向左
    NSLocaleLanguageDirectionTopToBottom = kCFLocaleLanguageDirectionTopToBottom, // 從上到下
    NSLocaleLanguageDirectionBottomToTop = kCFLocaleLanguageDirectionBottomToTop  // 從下到上
};
  • 獲取用戶的語言偏好設(shè)置列表,該列表對應(yīng)于iOS中Setting>General>Language彈出的面板中的語言列表,列表的第一個元素即為當(dāng)前用戶設(shè)置的語言。
[NSLocale preferredLanguages]
  • 監(jiān)聽用戶本地化設(shè)置的消息
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(localChangedHandler:)
                                             name:NSCurrentLocaleDidChangeNotification object:nil];
  • 以本地化方式獲取國際化信息的顯示名稱
// 以美國地區(qū),英文語言為本地化(en_US)
NSLocale *curLocal = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSLog(@"%@",[curLocal displayNameForKey:NSLocaleIdentifier value:@"fr_FR"] );// French (France)
// 以中國地區(qū),簡體中文語言為本地化(zh-Hans)
curLocal = [[NSLocale alloc] initWithLocaleIdentifier:@"zh-Hans"];
NSLog(@"%@",[curLocal displayNameForKey:NSLocaleIdentifier value:@"fr_FR"] );// 法文(法國)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容