关于_runWithMainScene:transitionContext:completion错误的测试

来源:互联网 发布:软件产业基地5e 编辑:程序博客网 时间:2024/06/05 17:17

_runWithMainScene:transitionContext:completion错误


前几天升级xcode7,使用ios9,碰到一个诡异的问题。在一个手机中出现一启动app就crash。

后来重新安装app,然后中间做了一些“顺手”的操作,crash无法再次重现。


今天app正式上线,测试mm再次拿过来一只毕现crash的手机。

同事调试后,终于发现问题所在。


在app启动时,做了一些权限检测,当权限未开启时,会自定义一个提示框(uiwindow)。

而因为之前“顺手”的问题,把权限打开了,所以不会出现提示框(uiwindow),问题无法重现。


毕现过程也很简单,应该是ios9对于uiwindow的处理变化所致。代码如下:

片段1

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];        ViewController *testController = [[ViewController alloc] init];    _nav = [[UINavigationController alloc] initWithRootViewController:testController];    self.window.rootViewController = _nav;        [self.window makeKeyAndVisible];        return YES;}

片段2

- (void)applicationDidBecomeActive:(UIApplication *)application {    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.        //crash in ios9    _testWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];    _testWindow.hidden = NO;        //ok//    dispatch_async(dispatch_get_main_queue(), ^{//        _testWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];//        _testWindow.hidden = NO;//    });        }


问题重现说明:


为了直观的用代码反映问题,已经把新建工程中默认的storybroad去掉了,通过手写代码构造controller,然后添加到window中。

然后看问题代码片段2,在applicationDidBecomeActive方法中加入一个自定义的uiwindow,并显示它。


运行代码,然后程序就crash:

2015-10-22 19:31:43.277 RHHttpDemo[19783:1276197] *** Assertion failure in -[UIApplication _runWithMainScene:transitionContext:completion:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3505.16/UIApplication.m:3294(lldb) 


将片段2中的代码做调整,加入到下一个loop周期中,再次运行代码,程序正常。

对于相同的代码在ios8.0(iphone5s)中测试,一切正常。


总结:

应该是在ios9中,对应程序启动时的uiwindow处理有了调整。

有网友说,xcode7后,多个uiwindow时,都需要有rootViewController。

从上面的测试过程中,应该不全对,只是在启动过程中,未完全结束时,对多个uiwindow有要求。

从uiwindow.h文件描述中,是这样定义的,@property(nullable,nonatomic,strong)UIViewController *rootViewControllerNS_AVAILABLE_IOS(4_0); // default is nil,

是nullable,是允许未nil的,也可以算是一次侧面说明。


具体的启动过程不太清楚,先做记录,待有缘人回复,哈哈~~

联系方式如下:

qq:410289616

qq群:330585393


1 0