Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过代码设置iOS的系统时区?_Ios_Objective C - Fatal编程技术网

如何通过代码设置iOS的系统时区?

如何通过代码设置iOS的系统时区?,ios,objective-c,Ios,Objective C,我想用代码设置系统时区,iOS中的日期时间。有什么想法或私人API可以帮助我吗? 示例:将时区设置为GMT+8,日期时间设置为2013年8月10日晚上8:30。怎么做?谢谢 正如@Filip在评论中所说,在应用程序中编辑系统首选项是不可能的。你可能要做的(不知道它是否对你有用)是在你的应用程序中设置你正在使用的NSDates的时区 这是一个如何做到这一点的例子: NSString *dateString = @"2013-08-07 17:49:54"; NSDateFormatter *dat

我想用代码设置系统时区,iOS中的日期时间。有什么想法或私人API可以帮助我吗?
示例:将时区设置为GMT+8,日期时间设置为2013年8月10日晚上8:30。怎么做?谢谢

正如@Filip在评论中所说,在应用程序中编辑系统首选项是不可能的。你可能要做的(不知道它是否对你有用)是在你的应用程序中设置你正在使用的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];

这里列出了可能与timezone一起使用的时区名称:`:

正如其他人所说,您不能从应用程序中编辑系统时区,但是您可以使用
NSTimeZone
为整个应用程序设置默认时区

您提到这是为了测试,所以我假设这是为了单元测试一些自定义日期格式化程序,在这种情况下,您可以在单元测试中执行以下操作:

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

我希望这有帮助

仍在使用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
    ...
}
已添加 您还可以使用时区名称,如“英国夏季时间”


您不能在应用程序内设置系统设置!(可能是在一个越狱的设备上)您可以使用privates API来实现这一点,但app store不会接受这一点。你打算内部发布吗?是的,我想有一些私有API可以实现,但我不知道,它只是用于内部测试!谢谢你的建议,但是如何在我的应用程序中设置,并且iOS中的所有应用程序都将设置时区和日期?正如我之前所说,对于所有系统、所有应用程序等,似乎没有办法做到这一点,抱歉,你只能控制自己应用程序的日期/时区。即使没有文档记录,您还可以避免缓存时区,因为
[NSTimeZone setDefaultTimezone:nil]
足以将
defaultTimeZone
重置为原始值。在swift中,我们现在可以使用
NSTimeZone.resetSystemTimeZone()
实现this@beehive-疯人院从iOS 2开始就存在,它看起来像是
resetSystemTimeZone
,而不仅仅是Swift。
private let britishSummerTimeZone = TimeZone(abbreviation: "BST")!