if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = orientation;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
这段代码可以用来在用户已经锁定屏幕旋转的情况下, 进行强制旋转屏幕, 虽然这样用并不明智, 但是为了满足需求, 确实需要使用这样的方法, 在近期开发过程中发现了这样的问题, 在APP从后台切入前台后的这个方法中:
- (void)applicationDidBecomeActive:(UIApplication *)application
进行强制屏幕旋转会造成
- (void)applicationDidBecomeActive:(UIApplication *)application;
- (void)applicationWillResignActive:(UIApplication *)application;
- (void)applicationDidEnterBackground:(UIApplication *)application;
- (void)applicationWillEnterForeground:(UIApplication *)application;
会造成这几个方法再执行一遍;
最终的现象就是前后台通知回调走了两次, 从而造成各种莫名其妙的bug, 所以建议这种调用苹果私有API的情况尽量不要出现