从 iPhone 设备查找当前国家/地区

发布于 2024-09-27 06:26:28 字数 140 浏览 4 评论 0原文

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

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

请帮我找到那个国家。

提前致谢。

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

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

Please help me out to find the country .

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

我只土不豪 2024-10-04 06:26:28

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

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。有关详细信息,请参阅此问题:How can I get current location from user in iOS

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.

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

天荒地未老 2024-10-04 06:26:28
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSString *country = [locale displayNameForKey: NSLocaleCountryCode value: countryCode];
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSString *country = [locale displayNameForKey: NSLocaleCountryCode value: countryCode];
疯狂的代价 2024-10-04 06:26:28

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

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

For devices with SIM card you can use this:

  CTTelephonyNetworkInfo * network_Info = [CTTelephonyNetworkInfo new];
  CTCarrier * carrier = network_Info.subscriberCellularProvider;
  NSString  * countryCode = [carrier.isoCountryCode uppercaseString];
停滞 2024-10-04 06:26:28

对于 Swift 4.0,

您可以简单地使用

letcountryCode = Locale.current.regionCode

print(countryCode)

For Swift 4.0,

You can simply use

let countryCode = Locale.current.regionCode

print(countryCode)

念三年u 2024-10-04 06:26:28
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

源链接

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.

烟凡古楼 2024-10-04 06:26:28

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

转到产品->方案->编辑方案...->运行-> 选项并选择您要测试的应用程序语言应用程序区域

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

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

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

Go to Product->Scheme->Edit Scheme...->Run->Options and choose the Application Language and Application Region you want to test.

Edit Application Language and Application Region within Scheme Run Options

From the iOS Documentation: Testing Your Internationalized App

み格子的夏天 2024-10-04 06:26:28
NSTimeZone *gmt = [NSTimeZone localTimeZone];

NSLog(@"timezone gmt %@",gmt.abbreviation);
NSTimeZone *gmt = [NSTimeZone localTimeZone];

NSLog(@"timezone gmt %@",gmt.abbreviation);
花想c 2024-10-04 06:26:28

Swift 3.0最新的工作代码是

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

Swift 3.0 latest working code is

if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
    print(countryCode)
}
澉约 2024-10-04 06:26:28

Swift 3.0解决方案

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

Swift 3.0 solution

if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
            print(countryCode)
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文