objective-c 如何在 iPhone 模拟器中更改时间和时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1699671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change time and timezone in iPhone simulator?
回答by nduplessis
I'm guessing it uses your system timezone, so changing the TZ in System Preferences would probably do the trick
我猜它使用您的系统时区,因此更改系统偏好设置中的 TZ 可能会成功


回答by Vishal Shah
Restart the Simulator after changing the system date time preferences and you shall see the changes reflected. It worked for me.
更改系统日期时间首选项后重新启动模拟器,您将看到反映的更改。它对我有用。
回答by LevinsonTechnologies
It would be useful if Apple provided a "Simulate date" as they provided a "Simulate Location" menu. What I do is set a breakpoint immediately after getting the date (saving it in a variable) and then modify the value. Or just modify the spot where I check for a date change and make it return true.
如果 Apple 提供“模拟日期”,因为他们提供了“模拟位置”菜单,那将会很有用。我所做的是在获取日期后立即设置断点(将其保存在变量中),然后修改该值。或者只是修改我检查日期更改的位置并使其返回true。
回答by tadija
For the purpose of taking a screenshot, Apple finally made it possible to override time on the status bar of iOS simulator (since Xcode 11) by using simctltool:
为了截屏,Apple 终于可以通过使用simctl工具覆盖 iOS 模拟器(自Xcode 11 起)状态栏上的时间:
xcrun simctl status_bar "iPhone Xs" override --time "21:08"
回答by multitudes
Here is a solution available from iOS 13 and Xcode 11 at least. (did not test with previous versions)
这是至少在 iOS 13 和 Xcode 11 中可用的解决方案。(没有用以前的版本测试)
By default the iOS Simulator shows whatever the time is on your Mac, however, you can use Xcode's command line to override that with this command in the terminal:
默认情况下,iOS 模拟器显示 Mac 上的任何时间,但是,您可以使用 Xcode 的命令行在终端中使用以下命令覆盖它:
xcrun simctl status_bar "iPhone 11 Pro Max" override --time '9:41'
Replace the simulator name with the device you want to change.
将模拟器名称替换为您要更改的设备。
For the status bar you have this overrides:
对于状态栏,您有这个覆盖:
You may specify any combination of these flags (at least one is required):
--time <string>
Set the date or time to a fixed value.
If the string is a valid ISO date string it will also set the date on relevant devices.
--dataNetwork <dataNetworkType>
If specified must be one of 'wifi', '3g', '4g', 'lte', 'lte-a', or 'lte+'.
--wifiMode <mode>
If specified must be one of 'searching', 'failed', or 'active'.
--wifiBars <int>
If specified must be 0-3.
--cellularMode <mode>
If specified must be one of 'notSupported', 'searching', 'failed', or 'active'.
--cellularBars <int>
If specified must be 0-4.
--batteryState <state>
If specified must be one of 'charging', 'charged', or 'discharging'.
--batteryLevel <int>
If specified must be 0-100.
The time can be any string. But if you want the device to show the date you will need use the ISO format. For example a valid ISO date string would be '2007-01-09T10:41:00+01:00'
时间可以是任何字符串。但是,如果您希望设备显示日期,则需要使用ISO 格式。例如,有效的 ISO 日期字符串将是 '2007-01-09T10:41:00+01:00'
Otherwise you can use the time parameter as a string and it will display whatever you pass in it.
否则,您可以将时间参数用作字符串,它会显示您传入的任何内容。
With thanks to the original post by Paul Hudson Here's the link!
感谢 Paul Hudson 的原帖,这是链接!
回答by Lasse
When changing the timezone, I found the easiest way to do it was by clicking the clock in the menubar. And then selecting "Open Date & Time Preferences" then select the tab Time Zone.
更改时区时,我发现最简单的方法是单击菜单栏中的时钟。然后选择“打开日期和时间首选项”,然后选择选项卡时区。
Alternatively System Preferences -> Date and Time and select the tab Time Zone.
或者,系统偏好设置 -> 日期和时间,然后选择选项卡时区。
Just a pointer for anyone that might not know their way around OSX.
对于可能不了解 OSX 的任何人来说,这只是一个指针。
回答by Ohmy
This is an old thread but it is closest to my question. I need to simulate time zone for Europe, this method works for me. Go to 'TimeZone' tap instead of 'Date&Time' tap. Uncheck the 'Set time zone automatically using current location' box and slide the vertical rectangle bar (with blue dot on it) to simulate your system time.
这是一个旧线程,但它最接近我的问题。我需要模拟欧洲的时区,这种方法对我有用。转到“时区”点击而不是“日期和时间”点击。取消选中“使用当前位置自动设置时区”框并滑动垂直矩形条(上面有蓝点)来模拟您的系统时间。
回答by Sebastian Hojas
I have proposed an automatic solution to the problem of changing the time that includes hacky method swizzling: https://stackoverflow.com/a/34793193/829338. I assume that should also work for changing the time zone accordingly.
我已经提出了一种自动解决更改时间问题的方法,其中包括 hacky 方法 swizzling:https://stackoverflow.com/a/34793193/829338 。我认为这也应该适用于相应地更改时区。
I needed to test my app automatically, which required changing the sys time. I did, what Tom suggested: happy hacky method swizzling.
我需要自动测试我的应用程序,这需要更改系统时间。我做了,汤姆建议的:快乐的哈克方法调配。
For demonstrative purposes, I only change [NSDate date]but not [NSDate dateWithTimeIntervalSince1970:].
出于演示目的,我只更改[NSDate date]但不更改[NSDate dateWithTimeIntervalSince1970:]。
First your need to create your class method that serves as the new [NSDate date]. I implemented it to simply shift the time by a constant timeDifference.
首先,您需要创建用作新 [NSDate 日期] 的类方法。我实现了它以简单地将时间移动一个恒定的 timeDifference。
int timeDifference = 60*60*24; //shift by one day
NSDate* (* original)(Class,SEL) = nil;
+(NSDate*)date{
NSDate* date = original([NSDate class], @selector(date));
return [date dateByAddingTimeInterval:timeDifference];
}
So far, pretty easy. Now comes the fun part. We get the methods from both classes and exchange implementation (it worked for me in the AppDelegate, but not in my UITests class). For this you will need to import objc/runtime.h.
到目前为止,很容易。有趣的来了。我们从两个类和交换实现中获取方法(它在 AppDelegate 中对我有用,但在我的 UITests 类中不起作用)。为此,您需要导入objc/runtime.h.
Method originalMethod = class_getClassMethod([NSDate class], @selector(date));
Method newMethod = class_getClassMethod([self class], @selector(date));
//save the implementation of NSDate to use it later
original = (NSDate* (*)(Class,SEL)) [NSDate methodForSelector:@selector(date)];
//happy swapping
method_exchangeImplementations(originalMethod, newMethod);
回答by robmathers
You can set the TZenvironment variable in an Xcode Scheme to set the time zone just for that app.
您可以TZ在 Xcode Scheme 中设置环境变量来为该应用程序设置时区。
You can use UTC, PST, EST, as well as place-based timezone names such as America/Los_Angeles. It's not well documented, but I suspect any time zone nameshould work.
您可以使用UTC、PST、EST以及基于地点的时区名称,例如America/Los_Angeles. 它没有很好的记录,但我怀疑任何时区名称都应该有效。
It's not well documented, but the source is an Apple Developer Support rep on the developer forums.
它没有很好的文档记录,但来源是开发者论坛上的 Apple 开发者支持代表。
回答by odyth
My build server is UTC and some of my unit tests needed the timezone to be PST. Using a category on NSTimeZone you can override Apple's implementation to use your code. Works for swift only projects too.
我的构建服务器是 UTC,我的一些单元测试需要时区为 PST。使用 NSTimeZone 上的类别,您可以覆盖 Apple 的实现以使用您的代码。也适用于仅 swift 的项目。
//NSTimeZone+DefaultTimeZone.h
#import <Foundation/Foundation.h>
@interface NSTimeZone (DefaultTimeZone)
+(NSTimeZone *)defaultTimeZone;
@end
//NSTimeZone+DefaultTimeZone.m
#import "NSTimeZone+DefaultTimeZone.h"
@implementation NSTimeZone (DefaultTimeZone)
+(NSTimeZone *)defaultTimeZone
{
return [NSTimeZone timeZoneWithName:@"America/Los_Angeles"];
}
@end

