在真实设备上的 XCUI 测试中检测暗模式

问题描述

我正在尝试找到一种方法来在 XCUI 测试中检测手机是否处于黑暗模式。到目前为止,我已经有了这个:

@Override
public String getMessage() {
if( this instanceOf childException)
return super.getMessage();
else
return String.Format(myFormat,A,super.getMessage());
}

这对模拟器非常有效,但不适用于真实设备。如何在真实设备上实现这一点?

注意:我知道有一种方法可以在 XCUI 测试中设置暗/亮模式。我不要这个,我只是想检测一下。

解决方法

只需在您的 viewController 中使用 traitCollectionDidChange 方法,如下所示:

//Call this method when you change UserInterface style
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    print(previousTraitCollection," preview UI mode")
}