检测iPhone是否在12小时或24小时模式下显示时间?

Mic*_*all 10 iphone localization regional

如何检测iPhone是否在12小时或24小时模式下显示时间?

我当前监视当前区域的变化,但这不是影响时间显示的唯一设置.日期和时间设置中的24小时切换会覆盖当前区域设置.

有没有办法检测这个12/14小时的设置?

Mic*_*all 15

我已经想出了一个很好的方法来确定这一点,我已经添加了一个类别的函数NSLocale.它看起来非常准确,并且在测试多个地区时没有发现任何问题.

@implementation NSLocale (Misc)
- (BOOL)timeIs24HourFormat {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterNoStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    NSString *dateString = [formatter stringFromDate:[NSDate date]];
    NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
    NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];
    BOOL is24Hour = amRange.location == NSNotFound && pmRange.location == NSNotFound;
    [formatter release];
    return is24Hour;
}
@end
Run Code Online (Sandbox Code Playgroud)