我正在使用 Swift 和 XCTest 为我的应用程序编写 UI 测试,我必须摇动设备才能收到通知,您能告诉我如何在代码中生成摇动手势吗?
最佳答案
解决方案非常简单。您需要做的就是发送“com.apple.UIKit.SimulatorShake” Darwin 通知。请参阅下面的示例代码:
SimulatorShaker.h
#import <Foundation/Foundation.h>
@interface SimulatorShaker : NSObject
+ (void)performShake;
@end
SimulatorShaker.m
#import "SimulatorShaker.h"
#import "notify.h"
@implementation SimulatorShaker
+ (void)performShake {
notify_post("com.apple.UIKit.SimulatorShake");
}
@end
如果您使用 Swift 项目,则必须添加一个桥接 header 。通常 Xcode 建议在您将一些 Objective-C 文件添加到项目时自动创建桥接 header 。查看如何 set up the header manually如果您找不到桥接头或在构建项目时遇到任何问题
注意:Darwin Notification API 对第 3 方开发人员是私有(private)的,Apple 不允许将此类代码发布到 App Store。但是,如果您仅将 SimulatorShaker 用于 UI 测试目的,则不会遇到任何麻烦。
关于ios - 如何使用 Swift (XCTest) 生成摇动手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32743610/