I wanna set the system timezone, date time in iOS by code. Any ideas or private apis help me? Example: Set time zone to GMT+8, and date time to Aug 10, 2013 8:30 pm. How to do it? Thanks!
-
2You cannot set system settings from within an app! (Might be possible on a jail-broken device) – Groot Aug 08 '13 at 01:59
-
1You may use privates apis to do that but this will not be accepted in app store. Are you going for in-house distribution? – cvsguimaraes Aug 08 '13 at 02:03
-
Yes, I think exist some private apis to do it, but I don't know, It just uses for testing inhouse! – Hieu Pham Aug 08 '13 at 02:14
3 Answers
As the others have said you can't edit the system timezone from within an app, however you can set a default timezone for your entire application using +setDefaultTimeZone:
on NSTimeZone
.
You mentioned this was for testing, so I'm going to assume this is for unit testing some custom date formatters and such, in which case you could do something like this in your unit test:
static NSTimeZone *cachedTimeZone;
@implementation DateUtilTests
+ (void)setUp
{
[super setUp];
cachedTimeZone = [NSTimeZone defaultTimeZone];
// Set to whatever timezone you want your tests to run in
[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
}
+ (void)tearDown
{
[NSTimeZone setDefaultTimeZone:cachedTimeZone];
[super tearDown];
}
// ... do some tests ...
@end
I hope that helps!

- 226
- 2
- 3
-
4Even if it's not documented, you can also avoid caching the time zone as `[NSTimeZone setDefaultTimezone:nil]` is enough to reset `defaultTimeZone` to the original value. – viteinfinite Aug 17 '15 at 18:55
-
3In swift, we now can use `NSTimeZone.resetSystemTimeZone()` to achieve this – Harry Bloom Nov 25 '16 at 16:11
-
1@beehive-bedlam per https://developer.apple.com/reference/foundation/nstimezone/1387189-resetsystemtimezone?language=objc it looks like `resetSystemTimeZone` has existed since iOS 2 and it's not Swift only. – Liron Yahdav Jan 03 '17 at 22:33
Still working on Xcode 11. The swift 5 implementation would look like:
override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
//All test by default will use GMT time zone. If different, change it within the func
TimeZone.ReferenceType.default = gmtTimeZone
}
override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
//Rest to current
TimeZone.ReferenceType.default = TimeZone.current
}
In my XCTestCase class I have my time zones are declared like:
private let gmtTimeZone = TimeZone(abbreviation: "GMT")!
private let gmtPlus1TimeZone = TimeZone(abbreviation: "GMT+1")!
private let gmtMinus1TimeZone = TimeZone(abbreviation: "GMT-1")!
I am defaulting all my test to GMT but I for a specific test I want to change it, then:
func test_Given_NotGMTTimeZone_ThenAssertToGMT() {
TimeZone.ReferenceType.default = gmtPlus1TimeZone
...
}
Added You can also work with time zone names like "British Summer Time"
private let britishSummerTimeZone = TimeZone(abbreviation: "BST")!
Added
This did not work for me due to time zone caching. I needed to reset the cache after each time zone change to stop the tests from interfering with each other. E.g.
TimeZone.ReferenceType.default = gmtTimeZone
TimeZone.ReferenceType.resetSystemTimeZone ()

- 3
- 2

- 4,278
- 40
- 52
How @Filip said in comments, that's not possible to edit system preferences inside a app. What you might do (don't know if it will be useful for you) is set the timezone of the NSDates that you are working with, inside your APP.
That's an example of how to do that:
NSString *dateString = @"2013-08-07 17:49:54";
NSDateFormatter *dateFormatter = [NSDateFormatter new];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Europe/London"]; //set here the timezone you want
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:timeZone];
NSDate *date = [dateFormatter dateFromString:dateString];
Here's a list of possible timezones to be used with timeZoneWithName:` : http://pastebin.com/ibNU2RcG

- 11,525
- 5
- 44
- 49
-
Thank you for this suggest, but how to set inside my app and that timezone & date will be set for all apps in iOS? – Hieu Pham Aug 08 '13 at 02:17
-
1Well, like I said before, for all system, all apps, etc, it seems that there's no way of doing that, sorry, you can only control date / timezone of your own app. – Lucas Eduardo Aug 08 '13 at 02:20