[英]Date time object with time zone not working in objective C app development
[英]Is there any way to find difference between Current time zone date or time and system time in objective c
如果用户时间手动更改了当前设备的日期或时间,则需要计算当前设备所在的时区时间与系统时间(iphone和ipad)之间的时差。 如果将其设置为自动,我可以获取,但是如果用户手动更改日期或时间,我将遇到问题。
应用程序或用户可以随时更改应用程序的默认时区(使用+ [NSTimeZone setDefaultTimeZone])或系统时区(使用“系统偏好设置”)。 + [NSTimeZone localTimeZone]返回的代理将始终充当该应用程序的当前默认时区,即使该默认时区已更改。 您可以更改应用程序的默认时区,以使其表现出与其他时区相同的效果。
+ [NSTimeZone systemTimeZone]返回当前系统时区(使用“系统偏好设置”设置)。 在大多数情况下,这些都是相同的(我相信应用程序的默认时区设置为应用程序启动时的系统时区)。
如果您想知道系统的时区设置,则可能要使用+ [NSTimeZone systemTimeZone]。 如果您只想为应用程序使用正确的时区,则可能需要+ [NSTimeZone localTimeZone]。
引用链接: NSTimeZone
用户更改时区或时间时,请务必小心。
UIApplication.h
类中有一个委托方法。
- (void)applicationSignificantTimeChange:(UIApplication *)application; // midnight, carrier time update, daylight savings time change
在AppDelegate.m
重写此方法。 每当使用更改设备时区时,它将调用。
因此,您可以将以前的时间存储在NSUserDefaults
,然后用这种方法测量新时间的时差。
还有一个通知观察者UIApplicationSignificantTimeChangeNotification
。 将此观察者添加到您的视图控制器。
例如:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(timeChanged:) name:UIApplicationSignificantTimeChangeNotification object:nil];
- (void)timeChanged:(NSNotification *)notification {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.