如何在iPhone模拟器中更改时间和时区?

问题描述 投票:0回答:12

如何在 iPhone 模拟器中更改时间和时区?

xcode timezone ios-simulator
12个回答
262
投票

我猜它使用您的系统时区,因此更改系统偏好设置中的 TZ 可能会成功

Screenshot for description


135
投票

您可以在 Xcode 方案中设置

TZ
环境变量来仅为该应用程序设置时区。

您可以使用

UTC
PST
EST
,以及基于地点的时区名称,例如
America/Los_Angeles
。它没有很好的记录,但我怀疑任何时区名称都应该有效。

没有详细记录,但来源是开发者论坛上的 Apple 开发者支持代表


49
投票

更改系统日期时间首选项后重新启动模拟器,您将看到反映的更改。这对我有用。


36
投票

为了截图的目的,Apple终于可以使用simctl工具来覆盖iOS模拟器状态栏上的时间(自Xcode 11):

xcrun simctl status_bar "iPhone Xs" override --time "21:08"

30
投票

这是至少在 iOS 13 和 Xcode 11 上可用的解决方案。 (未测试过之前的版本) [编辑]这只会改变评论中的状态栏!

默认情况下,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 格式。 例如,有效的 ISO 日期字符串为 '2007-01-09T10:41:00+01:00'

否则,您可以将时间参数用作字符串,它将显示您传入的任何内容。

感谢 Paul Hudson 的原始帖子,这是链接


8
投票

这是一个旧线程,但它最接近我的问题。我需要模拟欧洲时区,这种方法对我有用。转到“时区”点击而不是“日期和时间”点击。取消选中“使用当前位置自动设置时区”框,然后滑动垂直矩形条(上面有蓝点)来模拟您的系统时间。


3
投票

更改时区时,我发现最简单的方法是单击菜单栏中的时钟。然后选择“打开日期和时间首选项”,然后选择“时区”选项卡。

或者系统偏好设置 -> 日期和时间,然后选择时区选项卡。

仅供那些可能不了解 OSX 的人参考。


3
投票

目前唯一可行的解决方案。 XCode 确实提供了为特定应用程序设置时区的选项。

在 XCode 中,单击您的应用程序,

Edit Scheme -> Run configuration -> Arguments Tab -> Add Environment Variables

创建一个变量,名称为:

TZ
,值:
CST
(任何其他标准格式。XCode 没有明确提及允许的值。但您也可以使用 America/Chicago)


2
投票

我提出了一种自动解决更改时间问题的方法,其中包括 hacky 方法调整:https://stackoverflow.com/a/34793193/829338。我认为这也应该适用于相应地更改时区。


我需要自动测试我的应用程序,这需要更改系统时间。我按照汤姆的建议做了:快乐的 hacky 方法混合。

出于演示目的,我仅更改

[NSDate date]
,但不更改
[NSDate dateWithTimeIntervalSince1970:]

首先,您需要创建用作新的 [NSDate 日期] 的类方法。我实现它是为了简单地将时间移动恒定的时间差。

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

1
投票

我的构建服务器是 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

0
投票

更改系统日期时间首选项后,我必须选择

Hardware
>
Reset All Content And Settings

只有这在版本 10.3 中对我有效(SimulatorApp-880.5 CoreSimulator-681.5.4)。


0
投票

在大多数情况下,我会推荐另外两种解决方案。

1. 实时打印时间戳和变化时的时间戳计算

2. 在视图中创建一个按钮,当您不进行测试时将其注释掉,按下时,它会绕过您正在测试的任何时间限制条件。

© www.soinside.com 2019 - 2024. All rights reserved.