54

iPhoneの設定で現在の国を取得する必要があります。iPhoneアプリケーションで現在の国を取得する方法を教えてもらえますか?

現在の国を通過する必要があるRSSフィードを解析するには、現在の国を使用する必要があります。

国を見つけるのを手伝ってください。

前もって感謝します。

4

9 に答える 9

141

ユーザーが選択した言語の国を検索するには:

NSLocale *currentLocale = [NSLocale currentLocale];  // get the current locale.
NSString *countryCode = [currentLocale objectForKey:NSLocaleCountryCode];
// get country code, e.g. ES (Spain), FR (France), etc.

Swiftの場合:

let currentLocale = NSLocale.currentLocale()
let countryCode = currentLocale.objectForKey(NSLocaleCountryCode) as? String

現在のタイムゾーンの国コードを検索する場合は、@chings228の回答を参照してください。

デバイスの物理的な場所の国コードを検索する場合は、逆ジオコーディングを使用したCoreLocationが必要になります。詳細については、この質問を参照してください:iOSのユーザーから現在地を取得するにはどうすればよいですか?

于 2010-10-15T08:32:28.373 に答える
18
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSString *country = [locale displayNameForKey: NSLocaleCountryCode value: countryCode];
于 2012-05-19T11:21:59.407 に答える
13

SIMカードを搭載したデバイスの場合、これを使用できます。

  CTTelephonyNetworkInfo * network_Info = [CTTelephonyNetworkInfo new];
  CTCarrier * carrier = network_Info.subscriberCellularProvider;
  NSString  * countryCode = [carrier.isoCountryCode uppercaseString];
于 2017-04-06T11:53:46.820 に答える
11

Swift 4.0の場合、

簡単に使用できます

let countryCode = Locale.current.regionCode

print(countryCode)

于 2018-08-18T08:42:16.527 に答える
9
NSLocale *countryLocale = [NSLocale currentLocale];  
NSString *countryCode = [countryLocale objectForKey:NSLocaleCountryCode];
NSString *country = [countryLocale displayNameForKey:NSLocaleCountryCode value:countryCode];
NSLog(@"Country Code:%@ Name:%@", countryCode, country);
//Country Code:IN Name:India

ソースリンク

于 2015-03-17T10:51:56.323 に答える
6

テストしていて、システムとは異なる言語を使用したい場合は、デバイスまたはシミュレーターの言語と地域を変更するよりも、スキームApplication Languageとオプションを変更するのが最も簡単です。Application Region

Product-> Scheme-> Edit Scheme...-> Run->に移動しOptions、を選択してテストしますApplication LanguageApplication Region

スキーム実行オプション内のアプリケーション言語とアプリケーション領域の編集

iOSドキュメントから:国際化されたアプリのテスト

于 2015-12-11T22:07:20.767 に答える
5
NSTimeZone *gmt = [NSTimeZone localTimeZone];

NSLog(@"timezone gmt %@",gmt.abbreviation);
于 2011-12-02T14:36:49.070 に答える
4

Swift3.0の最新の動作コードは

if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
    print(countryCode)
}
于 2017-07-07T13:52:45.460 に答える
3

Swift3.0ソリューション

if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
            print(countryCode)
        }
于 2016-10-19T08:57:16.463 に答える