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

前端之家收集整理的这篇文章主要介绍了在iOS中模拟方向更改以进行测试前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想测试我的应用程序处理方向更改(纵向/横向)的能力.我目前正在使用 KIF,据我所知,它无法做到这一点.有没有办法以编程方式为iOS模拟器模拟旋转事件?

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

解决方法

这是实现这一目标的一步:
  1. + (KIFTestStep*) stepToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation {
  2.  
  3. NSString* orientation = UIInterfaceOrientationIsLandscape(toInterfaceOrientation) ? @"Landscape" : @"Portrait";
  4. return [KIFTestStep stepWithDescription: [NSString stringWithFormat: @"Rotate to orientation %@",orientation]
  5. executionBlock: ^KIFTestStepResult(KIFTestStep *step,NSError *__autoreleasing *error) {
  6. if( [UIApplication sharedApplication].statusBarOrientation != toInterfaceOrientation ) {
  7. UIDevice* device = [UIDevice currentDevice];
  8. SEL message = NSSelectorFromString(@"setOrientation:");
  9.  
  10. if( [device respondsToSelector: message] ) {
  11. NSMethodSignature* signature = [UIDevice instanceMethodSignatureForSelector: message];
  12. NSInvocation* invocation = [NSInvocation invocationWithMethodSignature: signature];
  13. [invocation setTarget: device];
  14. [invocation setSelector: message];
  15. [invocation setArgument: &toInterfaceOrientation atIndex: 2];
  16. [invocation invoke];
  17. }
  18. }
  19.  
  20. return KIFTestStepResultSuccess;
  21. }];
  22. }

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

猜你在找的iOS相关文章