iPhoneの設定で現在の国を取得する必要があります。iPhoneアプリケーションで現在の国を取得する方法を教えてもらえますか?
現在の国を通過する必要があるRSSフィードを解析するには、現在の国を使用する必要があります。
国を見つけるのを手伝ってください。
前もって感謝します。
iPhoneの設定で現在の国を取得する必要があります。iPhoneアプリケーションで現在の国を取得する方法を教えてもらえますか?
現在の国を通過する必要があるRSSフィードを解析するには、現在の国を使用する必要があります。
国を見つけるのを手伝ってください。
前もって感謝します。
ユーザーが選択した言語の国を検索するには:
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のユーザーから現在地を取得するにはどうすればよいですか?
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSString *country = [locale displayNameForKey: NSLocaleCountryCode value: countryCode];
SIMカードを搭載したデバイスの場合、これを使用できます。
CTTelephonyNetworkInfo * network_Info = [CTTelephonyNetworkInfo new];
CTCarrier * carrier = network_Info.subscriberCellularProvider;
NSString * countryCode = [carrier.isoCountryCode uppercaseString];
Swift 4.0の場合、
簡単に使用できます
let countryCode = Locale.current.regionCode
print(countryCode)
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
テストしていて、システムとは異なる言語を使用したい場合は、デバイスまたはシミュレーターの言語と地域を変更するよりも、スキームApplication Language
とオプションを変更するのが最も簡単です。Application Region
Product
-> Scheme
-> Edit Scheme...
-> Run
->に移動しOptions
、を選択してテストしますApplication Language
。Application Region
iOSドキュメントから:国際化されたアプリのテスト
NSTimeZone *gmt = [NSTimeZone localTimeZone];
NSLog(@"timezone gmt %@",gmt.abbreviation);
Swift3.0の最新の動作コードは
if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
print(countryCode)
}
Swift3.0ソリューション
if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
print(countryCode)
}