【问题标题】:How to set the system timezone for iOS by code?如何通过代码设置iOS系统时区?
【发布时间】:2021-10-05 18:45:08
【问题描述】:

我想通过代码在 iOS 中设置系统时区、日期时间。任何想法或私人api对我有帮助吗? 示例:将时区设置为 GMT+8,日期时间设置为 2013 年 8 月 10 日晚上 8:30。怎么做?谢谢!

【问题讨论】:

  • 您不能在应用程序中设置系统设置! (可能在越狱设备上可行)
  • 您可以使用 privates api 来执行此操作,但应用商店不接受此操作。您打算进行内部分销吗?
  • 是的,我认为存在一些私有 api 可以做到这一点,但我不知道,它只是用于内部测试!

标签: ios objective-c


【解决方案1】:

正如其他人所说,您不能在应用程序内编辑系统时区,但是您可以使用+setDefaultTimeZone: 上的NSTimeZone 为整个应用程序设置默认时区。 p>

你提到这是为了测试,所以我假设这是为了对一些自定义日期格式化程序等进行单元测试,在这种情况下你可以在你的单元测试中做这样的事情:

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

希望对您有所帮助!

【讨论】:

  • 即使没有记录,您也可以避免缓存时区,因为[NSTimeZone setDefaultTimezone:nil] 足以将defaultTimeZone 重置为原始值。
  • 在swift中,我们现在可以使用NSTimeZone.resetSystemTimeZone()来实现这个
  • @beehive-bedlam per developer.apple.com/reference/foundation/nstimezone/… 看起来 resetSystemTimeZone 自 iOS 2 以来就已经存在,而且不仅仅是 Swift。
【解决方案2】:

仍在开发 Xcode 11。swift 5 实现如下所示:

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
}

在我的 XCTestCase 类中,我的时区声明如下:

private let gmtTimeZone = TimeZone(abbreviation: "GMT")!
private let gmtPlus1TimeZone = TimeZone(abbreviation: "GMT+1")!
private let gmtMinus1TimeZone = TimeZone(abbreviation: "GMT-1")!

我将所有测试都默认为 GMT,但对于特定测试我想更改它,然后:

func test_Given_NotGMTTimeZone_ThenAssertToGMT() {
    TimeZone.ReferenceType.default = gmtPlus1TimeZone
    ...
}

已添加 您还可以使用时区名称,例如“British Summer Time”

private let britishSummerTimeZone = TimeZone(abbreviation: "BST")!

已添加

由于时区缓存,这对我不起作用。我需要在每次更改时区后重置缓存以阻止测试相互干扰。例如

TimeZone.ReferenceType.default = gmtTimeZone
TimeZone.ReferenceType.resetSystemTimeZone ()

【讨论】:

    【解决方案3】:

    @Filip 如何在 cmets 中说,无法在应用程序内编辑系统首选项。您可能会做的(不知道它是否对您有用)是在您的 APP 中设置您正在使用的 NSDates 的时区。

    这是一个如何做到这一点的例子:

    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];
    

    以下是与 timeZoneWithName 一起使用的可能时区列表:`:http://pastebin.com/ibNU2RcG

    【讨论】:

    • 感谢您的建议,但是如何在我的应用程序中设置以及将为 iOS 中的所有应用程序设置时区和日期?
    • 好吧,就像我之前说的,对于所有系统,所有应用程序等,似乎没有办法这样做,抱歉,您只能控制自己应用程序的日期/时区。跨度>
    猜你喜欢
    • 2011-12-05
    • 1970-01-01
    • 2010-10-22
    • 2011-06-05
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 2011-01-28
    • 2011-05-30
    相关资源
    最近更新 更多