如何在ios中检测系统时间格式的更改?

如何在ios中检测系统时间格式的更改?,ios,objective-c,nsdate,nsdateformatter,nslocale,Ios,Objective C,Nsdate,Nsdateformatter,Nslocale,我想检测系统设置中的时间格式更改。我使用了下面的代码,但它总是给我旧的格式。如何获取新的时间格式 #pragma mark #pragma mark - application change time format -(void)applicationSignificantTimeChange:(UIApplication *)application { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [f

我想检测系统设置中的时间格式更改。我使用了下面的代码,但它总是给我旧的格式。如何获取新的时间格式

#pragma mark
#pragma mark - application change time format
-(void)applicationSignificantTimeChange:(UIApplication *)application
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setLocale:[NSLocale currentLocale]];
    [formatter setDateStyle:NSDateFormatterNoStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setTimeZone:[NSTimeZone localTimeZone]];
    NSString *dateString = [formatter stringFromDate:[NSDate date]];
    NSLog(@"dataString ::%@",dateString);
    NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
    NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];
    is12Hour = (amRange.length > 0 || pmRange.length > 0);
}

存储系统的时间格式。将其存储为NSUserDefaults,例如PtimeFormat
下次启动应用程序时,请检查PtimeFormat是否存在。再次获取系统的时间格式,并匹配两种格式,无论它们是否相同。

存储系统的时间格式。将其存储为NSUserDefaults,例如PtimeFormat
下次启动应用程序时,请检查PtimeFormat是否存在。再次获取系统的时间格式,并匹配这两种格式,无论它们是否相同。

我不知道如果日期发生显著变化,为什么您会期望日期格式发生变化。
当时间(即
[NSDate date]
发生变化时,将触发重大时间变化事件。例如,如果新的一天开始,如果用户更改时区,或者如果夏令时开始或结束。
但这些事件不会改变日期格式

我认为您希望监视区域设置的更改。有一个通知:
NSCurrentLocaleDidChangeNotification

像这样的方法应该会奏效:

j
是一个模板,通过日期模板方法将其替换为
ha
(12小时格式)或
h
(24小时格式)

id localeDidChangeNotification = [[NSNotificationCenter defaultCenter] addObserverForName:NSCurrentLocaleDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
    NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
    if ([dateFormat rangeOfString:@"h"].location != NSNotFound) {
        // 12 hour
    }
    else {
        // 24 hour
    }
}];

// Don't forget to remove the notification in the appropriate place
// [[NSNotificationCenter defaultCenter] removeObserver:localeDidChangeNotification];

如果日期发生重大变化,我不确定您为什么希望日期格式发生变化。
当时间(即
[NSDate date]
发生变化时,将触发重大时间变化事件。例如,如果新的一天开始,如果用户更改时区,或者如果夏令时开始或结束。
但这些事件不会改变日期格式

我认为您希望监视区域设置的更改。有一个通知:
NSCurrentLocaleDidChangeNotification

像这样的方法应该会奏效:

j
是一个模板,通过日期模板方法将其替换为
ha
(12小时格式)或
h
(24小时格式)

id localeDidChangeNotification = [[NSNotificationCenter defaultCenter] addObserverForName:NSCurrentLocaleDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
    NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
    if ([dateFormat rangeOfString:@"h"].location != NSNotFound) {
        // 12 hour
    }
    else {
        // 24 hour
    }
}];

// Don't forget to remove the notification in the appropriate place
// [[NSNotificationCenter defaultCenter] removeObserver:localeDidChangeNotification];

但它总是给旧的时间形成新的。所以当我再次启动应用程序时,它会给我相同的时间格式,我们保存在NSUserDefaults中。谢谢你的回答好的,我没有试过。做一件事,在更改时间格式后,重新启动设备,然后启动应用程序。它是否进行了任何更改?是的,在我的应用程序中,我的视图是根据时间格式更改进行更改,因此这是不可能的。但它始终提供旧的时间格式,而不是新的格式。所以当我再次启动应用程序时,它会给我相同的时间格式,我们保存在NSUserDefaults中。谢谢你的回答好的,我没有试过。做一件事,更改时间格式后,请重新启动设备,然后启动应用程序。它是否进行了任何更改?是的,在我的应用程序中,我的视图是按时间格式更改,因此这是不可能的。首先感谢您在我的应用程序中回答我的视图是按时间格式更改,因此我希望检测系统时间格式更改。非常感谢您的帮助这个答案很有效。我想投赞成票,但我没有足够的理由。所以非常感谢。首先感谢您在我的应用程序中回答我的观点是随时间格式的变化而变化,所以我想检测系统时间格式的变化。非常感谢您的回答,它工作正常。我想投赞成票,但我没有足够的理由。非常感谢。