在iOS中模拟方向更改以进行测试

我想测试我的应用程序处理方向更改(纵向/横向)的能力.我目前正在使用 KIF,据我所知,它无法做到这一点.有没有办法以编程方式为iOS模拟器模拟旋转事件?

我不在乎它是否是一些未记录的私有API或黑客,因为这只会在测试期间运行,并且不会成为生产构建的一部分.

解决方法

这是实现这一目标的一步:
+ (KIFTestStep*) stepToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation {

    NSString* orientation = UIInterfaceOrientationIsLandscape(toInterfaceOrientation) ? @"Landscape" : @"Portrait";
        return [KIFTestStep stepWithDescription: [NSString stringWithFormat: @"Rotate to orientation %@",orientation]
                             executionBlock: ^KIFTestStepResult(KIFTestStep *step,NSError *__autoreleasing *error) {
                                 if( [UIApplication sharedApplication].statusBarOrientation != toInterfaceOrientation ) {
                                     UIDevice* device = [UIDevice currentDevice];
                                     SEL message = NSSelectorFromString(@"setOrientation:");

                                     if( [device respondsToSelector: message] ) {
                                         NSMethodSignature* signature = [UIDevice instanceMethodSignatureForSelector: message];
                                         NSInvocation* invocation = [NSInvocation invocationWithMethodSignature: signature];
                                         [invocation setTarget: device];
                                         [invocation setSelector: message];
                                         [invocation setArgument: &toInterfaceOrientation atIndex: 2];
                                         [invocation invoke];
                                     }
                                 }

                                 return KIFTestStepResultSuccess;
                             }];
}

注意:将设备平放在桌面上,或者加速度计更新会将视图旋转回来.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...