【发布时间】:2010-12-14 13:18:50
【问题描述】:
如何在iPhone 模拟器中更改时间和时区?
【问题讨论】:
-
更改系统时间和日期是没有办法的。这会影响所有 Mac 应用程序,包括 xcode 时间戳。正确的行为是模拟器位置的任何更改都必须更改时间和时区。所有其他事情都只是解决方法。希望苹果能尽快解决这个问题
标签: objective-c xcode timezone ios-simulator
如何在iPhone 模拟器中更改时间和时区?
【问题讨论】:
标签: objective-c xcode timezone ios-simulator
我猜它使用您的系统时区,所以在系统偏好设置中更改 TZ 可能会解决问题
【讨论】:
您可以在 Xcode 方案中设置 TZ 环境变量来为该应用设置时区。
您可以使用UTC、PST、EST,以及基于地点的时区名称,例如America/Los_Angeles。它没有很好的记录,但我怀疑任何time zone name 都应该工作。
没有很好的记录,但来源是Apple Developer Support rep on the developer forums。
【讨论】:
更改系统日期时间首选项后重新启动模拟器,您将看到所反映的更改。它对我有用。
【讨论】:
如果 Apple 在提供“模拟位置”菜单时提供“模拟日期”会很有用。我所做的是在获取日期后立即设置断点(将其保存在变量中),然后修改值。或者只是修改我检查日期更改的位置并使其返回 true。
【讨论】:
这是至少可从 iOS 13 和 Xcode 11 获得的解决方案。 (没有用以前的版本测试) [编辑] 这只会改变 cmets 中的状态栏!
默认情况下,iOS 模拟器会显示 Mac 上的任何时间,但是,您可以使用 Xcode 的命令行在终端中使用以下命令覆盖它:
xcrun simctl status_bar "iPhone 11 Pro Max" override --time '9:41'
将模拟器名称替换为您要更改的设备。
对于状态栏,你有这个覆盖:
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.
时间可以是任何字符串。但是,如果您希望设备显示您需要的日期,请使用ISO format。 例如,一个有效的 ISO 日期字符串是 '2007-01-09T10:41:00+01:00'
否则,您可以将时间参数用作字符串,它会显示您传入的任何内容。
感谢 Paul Hudson 的原帖,这里是 the link!
【讨论】:
更改时区时,我发现最简单的方法是单击菜单栏中的时钟。然后选择“打开日期和时间首选项”,然后选择选项卡时区。
或者 System Preferences -> Date and Time 并选择选项卡 Time Zone。
对于那些可能不了解 OSX 的人来说,这只是一个指针。
【讨论】:
我已经提出了一种自动解决时间更改问题的方法,其中包括 hacky 方法调配:https://stackoverflow.com/a/34793193/829338。我认为这也应该适用于相应地更改时区。
我需要自动测试我的应用,这需要更改系统时间。我做到了,正如 Tom 建议的那样:快乐的 hacky 方法 swizzling。
出于演示目的,我只更改了[NSDate date],而不更改[NSDate dateWithTimeIntervalSince1970:]。
首先,您需要创建用作新 [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];
}
到目前为止,很容易。有趣的来了。我们从类和交换实现中获取方法(它在 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);
【讨论】:
我的构建服务器是 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
【讨论】:
更改系统日期时间首选项后,我必须选择Hardware > Reset All Content And Settings。
只有这在 10.3 版(SimulatorApp-880.5 CoreSimulator-681.5.4)中对我有用。
【讨论】: