Just My Life & My Work

Archive for 2014 年 10 月 15 日

[iOS] 偏好語言 (Prefer Language)

一個能夠跨國際使用的App,需要多國語言化(Localization),之前介紹的是系統運行時自動轉換字串的語言,然而我們想要知道目前使用者的偏好語言 (Prefer Language),來做更進一步的運用,比如將語言碼當作參數傳遞。

/**
 Theme: Language
 IDE: Xcode 6
 Language: Objective C
 Date: 103/10/14
 Author: HappyMan
 Blog: https://cg2010studio.wordpress.com/
 */
    // 記錄語言碼
    NSString *languageStr = [[NSLocale preferredLanguages] objectAtIndex:0];
//    NSString *languageStr = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];

    if ([languageStr isEqualToString:@"zh-Hant"]) {// 繁中
        self.languageCode = @"2";
    }
    if ([languageStr isEqualToString:@"zh-Hans"]) {// 簡中
        self.languageCode = @"1";
    }
    if ([languageStr isEqualToString:@"en"]) {// 英語
        self.languageCode = @"0";
    }

有兩種用法可行,注意事項請看文件說明!

使用說明

+ (NSArray *)preferredLanguages NS_AVAILABLE(10_5, 2_0);
note that this list does not indicate what language the app is actually running in; the [NSBundle mainBundle] object determines that at launch and knows that information

@property (readonly, copy) NSArray *preferredLocalizations;
a subset of this bundle’s localizations, re-ordered into the preferred order for this process’s current execution environment; the main bundle’s preferred localizations indicate the language (of text) the user is most likely seeing in the UI

參考:Getting current device language in iOS?

廣告

[iOS] 加入聯絡人資料到通訊錄

想要將顯示在App上的聯絡人資料加入到通訊錄,但是不想要讓使用者自己到通訊錄一個字一個字慢慢打,這時候就可以用這些複雜的程式碼解決囉!

Address Book1

(繼續閱讀…)

標籤雲

%d 位部落客按了讚: