确定用户的“温度单位”。在iOS 10上设置(摄氏/华氏) [英] Determine user's "Temperature Unit" setting on iOS 10 (Celsius / Fahrenheit)

查看:647
本文介绍了确定用户的“温度单位”。在iOS 10上设置(摄氏/华氏)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS 10增加了用户在设置>常规>语言和设置下设置温度单位选项的功能。区域>温度单位。

iOS 10 adds the ability for the user to set their "Temperature Unit" choice under Settings > General > Language & Region > Temperature Unit.

我的应用程序如何以编程方式确定此设置,以便显示正确的温度单位?我倾注了NSLocale.h并且没有看到任何相关内容。

How can my app programmatically determine this setting so it can display the right temperature unit? I poured through NSLocale.h and didn't see anything relevant.

(在iOS 10之前,测试该区域设置是否使用度量标准并假设用户想要的度量标准就足够了使用Celsius。现在不再是这种情况了。)

(Before iOS 10, it was sufficient to test if the locale used metric and assume that metric users want to use Celsius. This is no longer the case.)

推荐答案

Alexandre Colucci发表的这篇文章我发现: http://blog.timac.org/?tag=nslocaletemperatureunit

There is this article by Alexandre Colucci that I found: http://blog.timac.org/?tag=nslocaletemperatureunit

首先,公开NSLocaleTemperatureUnit NSLocaleKey:

First, expose the NSLocaleTemperatureUnit NSLocaleKey:

FOUNDATION_EXPORT NSLocaleKey const NSLocaleTemperatureUnit;

然后检查单位:

temperatureUnit = [[NSLocale currentLocale] objectForKey:NSLocaleTemperatureUnit]

或者Swift(2.3):

Or Swift (2.3):

if let temperatureUnit = NSLocale.currentLocale().objectForKey(NSLocaleTemperatureUnit) {
    ...
}

它将返回一个字符串,即Celcius或Fahrenheit 。

It will return a string which is either "Celcius" or "Fahrenheit".

但是存在一个问题:它不会向后兼容早于10的iOS版本。如果您在iOS 9或更早版本的设备上运行您的应用,那么您将会在应用启动期间收到错误dyld:未找到符号:_NSLocaleTemperatureUnit。

But there is an issue: it's not backwards compatible with iOS versions earlier than 10. If you run your app on an iOS 9 or earlier device, you'll get an error "dyld: Symbol not found: _NSLocaleTemperatureUnit" during app startup.

解决方案是对NSLocaleTemperatureUnit变量定义使用弱链接。像这样:

The solution is to use weak linking for the NSLocaleTemperatureUnit variable definition. Like this:

FOUNDATION_EXPORT NSLocaleKey const NSLocaleTemperatureUnit  __attribute__((weak_import));

这将让应用程序通过dyld检查。但是现在你必须在使用NSLocaleTemperatureUnit之前检查操作系统版本,否则你的应用程序会因异常而崩溃。

This will let the app pass the dyld checks. But now you will have to check for the OS version before using NSLocaleTemperatureUnit, or your app will crash with an exception.

if #available(iOS 10,*) {
    if let temperatureUnit = NSLocale.currentLocale().objectForKey(NSLocaleTemperatureUnit) {
        ...
    }
}

编辑:

我在我的应用程序中尝试过,但Apple拒绝了我上传到Testflight时的应用程序。所以他们绝对不希望我们使用我们自己的格式化程序类的设置。我发现这很烦人。

I tried it in my app, but Apple rejected the app for it when I uploaded it to Testflight. So they definitely don't want us to use the setting for our own formatter classes. I find that pretty annoying.

这篇关于确定用户的“温度单位”。在iOS 10上设置(摄氏/华氏)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆