ios 从 iPhone 设备查找当前国家

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3940615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:55:11  来源:igfitidea点击:

Find current country from iPhone device

iosobjective-ciphoneapplication-settingsappsettings

提问by AppAspect

I have to get the current country in the iPhone settings. Can anyone tell me how to get the current country in iPhone application.

我必须在 iPhone 设置中获取当前国家/地区。谁能告诉我如何在 iPhone 应用程序中获取当前国家/地区。

I have to use the current country for parsing the RSS feed in which I need to pass the current country.

我必须使用当前国家/地区来解析我需要通过当前国家/地区的 RSS 提要。

Please help me out to find the country .

请帮我找到这个国家。

Thanks in advance.

提前致谢。

回答by kennytm

To find the country of the user's chosen language:

要查找用户所选语言的国家/地区:

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

In Swift:

在斯威夫特:

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

If you want to find the country code of the current timezone, see @chings228's answer.

如果要查找当前时区的国家/地区代码,请参阅@chings228 的回答

If you want to find the country code of the device's physical location, you will need CoreLocation with reverse geocoding. See this question for detail: How can I get current location from user in iOS

如果要查找设备物理位置的国家/地区代码,则需要具有反向地理编码的 CoreLocation。有关详细信息,请参阅此问题:How can I get current location from user in iOS

回答by Sachindra Pandey

NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSString *country = [locale displayNameForKey: NSLocaleCountryCode value: countryCode];

回答by Mehul Thakkar

For Swift 4.0,

对于 Swift 4.0,

You can simply use

你可以简单地使用

let countryCode = Locale.current.regionCode

let countryCode = Locale.current.regionCode

print(countryCode)

print(countryCode)

回答by user2248258

For devices with SIM card you can use this:

对于带有 SIM 卡的设备,您可以使用它:

  CTTelephonyNetworkInfo * network_Info = [CTTelephonyNetworkInfo new];
  CTCarrier * carrier = network_Info.subscriberCellularProvider;
  NSString  * countryCode = [carrier.isoCountryCode uppercaseString];

回答by Teja Kumar Bethina

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

Source Link.

源链接

回答by Heath Borders

If you're testing and want to use a different language from your system, it's easiest to change your scheme's Application Languageand Application Regionoptions instead of changing the language and region on your device or simulator.

如果您正在测试并希望使用与系统不同的语言,最简单的方法是更改​​您的方案Application LanguageApplication Region选项,而不是更改设备或模拟器上的语言和区域。

Go to Product->Scheme->Edit Scheme...->Run->Optionsand choose the Application Languageand Application Regionyou want to test.

转到Product-> Scheme-> Edit Scheme...-> Run->Options并选择要测试的Application LanguageApplication Region

Edit Application Language and Application Region within Scheme Run Options

在方案运行选项中编辑应用程序语言和应用程序区域

From the iOS Documentation: Testing Your Internationalized App

来自 iOS 文档:测试您的国际化应用程序

回答by chings228

NSTimeZone *gmt = [NSTimeZone localTimeZone];

NSLog(@"timezone gmt %@",gmt.abbreviation);

回答by Muhammad Nayab

Swift 3.0 latest working code is

Swift 3.0 最新的工作代码是

if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
    print(countryCode)
}

回答by Sourabh Sharma

Swift 3.0solution

Swift 3.0解决方案

if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
            print(countryCode)
        }