I want to update the timezone of my iOS app while its running. I want to update the timezone when user change region from the "Settings" app "Settings->General->Language & Region->Region".
I am receiving region change notification using and NSCurrentLocaleDidChangeNotification and selector method "updateTimeZone" is being called.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTimeZone) name:NSCurrentLocaleDidChangeNotification object:nil];
- (void)updateTimeZone
{
NSLog(@"Zone1: %@", [NSTimeZone systemTimeZone]);
NSLog(@"Zone2: %@", [NSTimeZone localTimeZone]);
NSLog(@"Zone3: %@", [NSTimeZone defaultTimeZone]);
[NSTimeZone resetSystemTimeZone];
NSLog(@"Zone11: %@", [NSTimeZone systemTimeZone]);
NSLog(@"Zone22: %@", [NSTimeZone localTimeZone]);
NSLog(@"Zone33: %@", [NSTimeZone defaultTimeZone]);
}
Output
2015-01-07 16:23:02.992 A[1677:105206] Zone1: Asia/Kolkata (GMT+5:30) offset 19800
2015-01-07 16:23:02.993 A[1677:105206] Zone2: Local Time Zone (Asia/Kolkata (GMT+5:30) offset 19800)
2015-01-07 16:23:02.994 A[1677:105206] Zone3: Asia/Kolkata (GMT+5:30) offset 19800
2015-01-07 16:23:02.999 A[1677:105206] Zone11: Asia/Kolkata (GMT+5:30) offset 19800
2015-01-07 16:23:02.999 A[1677:105206] Zone22: Local Time Zone (Asia/Kolkata (GMT+5:30) offset 19800)
2015-01-07 16:23:03.001 A[1677:105206] Zone33: Asia/Kolkata (GMT+5:30) offset 19800
I tried to change many regions from Settings but my application always shows my current time zone. I am already using [NSTimeZone resetSystemTimeZone]; to remove the cached timezone but still always getting my current time zone. How can I fetch the timezone set in iOS region (systemtimezone)?
When we update region, it updates the locale(AM/PM or 24HOUR format) for the device. We can get this updated locale in application using [NSLocale autoupdatingCurrentLocale] but we need to pop the view controller from navigation controller and push it again for the new locale. I want to update this new time format without pop out of view-controller from navigation controller?
If the locale changes and you want to update the UI to reflect the current locale (for things like AM/PM vs 24 hour time formats), you just need to listen for locale changes and then ask your NSDateFormatter
for new values of whatever strings you're using. First, observe NSCurrentLocaleDidChangeNotification
. When you receive that notification, ask your NSDateFormatter
for new string values and use those strings in your UI, by calling stringForDate:
again.
If the date/time strings are in a table view, this probably means reloading the table view. If they're in some other UI element, it depends on how you configured that element in the first place. The date formatter will give current values automatically-- no need to mess around with autoupdatingCurrentLocale
.
None of this has anything to do with time zones. Normally, the iPhone time zone updates automatically if the phone moves to a new time zone. But user preferences (like date formats) don't change when this happens. Those preferences are tied to the user's locale setting, and stay the same regardless of the time zone.