如何在iPhone模拟器中更改时间和时区?
在更改时区时,我发现最简单的方法是点击菜单栏中的时钟。然后选择“打开日期和时间首选项”,然后选择时区选项卡。
或者选择“System Preferences -> Date and Time”,选择“Time Zone”页签。
只是给那些不知道如何使用OSX的人一个提示。
这是一个老帖子,但它最接近我的问题。我需要模拟欧洲的时区,这个方法适合我。转到“时区”,而不是“日期和时间”。取消选中“使用当前位置自动设置时区”框,并滑动垂直矩形条(上面有蓝点)来模拟系统时间。
我已经提出了一个自动解决改变时间的问题,包括黑客方法swizzling: https://stackoverflow.com/a/34793193/829338。我想这也适用于相应地改变时区。
我需要自动测试我的应用程序,这需要改变系统时间。我照汤姆的建议去做了:快乐的手工搅拌法。
出于演示目的,我只改变了[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上的一个类别,你可以覆盖苹果的实现来使用你的代码。也适用于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
为了截屏,苹果最终通过simctl工具实现了在iOS模拟器的状态栏上覆盖时间的功能(从Xcode 11开始):
xcrun simctl status_bar "iPhone Xs" override --time "21:08"
以下是至少在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-09 t10 . 41:00+01:00'
否则,您可以使用时间参数作为字符串,它将显示您传递的任何内容。
感谢Paul Hudson的原创文章,这里是链接!
在更改系统日期时间首选项后,我不得不选择硬件>重置所有内容和设置。 只有在10.3版(SimulatorApp-880.5 CoreSimulator-681.5.4)中,我才这样做。
你可以在Xcode Scheme中设置TZ环境变量来为该应用程序设置时区。
您可以使用UTC、PST、EST以及基于位置的时区名称,如America/Los_Angeles。虽然没有详细的文档,但我认为任何时区名称都可以使用。
它没有很好的文档,但来源是苹果开发者论坛上的开发者支持代表。
目前唯一可行的解决方案。XCode提供了为特定应用程序设置时区的选项。
在XCode中,单击应用程序,编辑方案->运行配置->参数选项卡->添加环境变量
创建一个变量,名称:TZ,值:CST(任何其他标准格式。XCode没有明确提到允许的值。但你也可以用America/Chicago)
推荐文章
- Xcode 4 -在新的Macintosh安装上的配置文件上“没有找到有效的签名标识”错误
- 新的自动引用计数机制是如何工作的?
- 如何测试对象在Objective-C中的类?
- Xcode +删除所有断点
- 是否有可能禁用浮动头在UITableView与UITableViewStylePlain?
- 从Cocoa应用程序执行一个终端命令
- Swift:理解// MARK
- Swift编译器错误:“框架模块内的非模块化头”
- 从父iOS访问容器视图控制器
- 如何在JavaScript中获得时区名称?
- 自定义dealloc和ARC (Objective-C)
- 调整UITableView的大小以适应内容
- 在代码中为UIButton设置一个图像
- UICollectionView中的单元格间距
- 我如何在我的iOS应用程序中每n分钟得到一个后台位置更新?