今天在接入一个业务的时候,发现自定义的window不显示了,主要是因为ios13以后苹果增加了SceneDelegate进行管理窗口。
以前的时候如果想要显示一个window,使用如下代码就行了
window = [[NewMonkeyWindow alloc] initWithFrame:CGRectMake(0, 0, windowWidth, windowWidth)];
window.hidden = false
但是现在因为使用SceneDelegate管理window,必须将window注册到SceneDelegate中,如下所示
window = [[NewMonkeyWindow alloc] initWithFrame:CGRectMake(0, 0, windowWidth, windowWidth)];
window.hidden = false
if (@available(iOS 13.0, *)) {
for (UIWindowScene *windowScene in [UIApplication sharedApplication].connectedScenes) {
if (windowScene.activationState == UISceneActivationStateForegroundActive) {
window.windowScene = windowScene ;
break;
}
}
}
这样就完试了吗? too young,too simple,因为我的SDK是通过hook AppDelegate的didFinishLaunchingWithOptions方法,在执行该方法的时候,[UIApplication sharedApplication].connectedScenes
还为空,正常情况下应该hook的是SceneDelegate中的- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions
这个方法,为了兼容以前苹果以前的系统,只能通过延时策略进行注册,如下所示:
window = [[NewMonkeyWindow alloc] initWithFrame:CGRectMake(0, 0, windowWidth, windowWidth)];
window.hidden = false
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (@available(iOS 13.0, *)) {
for (UIWindowScene *windowScene in [UIApplication sharedApplication].connectedScenes) {
if (windowScene.activationState == UISceneActivationStateForegroundActive) {
window.windowScene = windowScene ;
break;
}
}
}
});
大功告成,顺利解决。
参考文章
1、https://blog.csdn.net/holdsky/article/details/102602213
2、https://www.jianshu.com/p/9cefeab409e8
3、https://www.codenong.com/js781fc3e433f8/
4、https://www.coder.work/article/6234891
5、https://blog.csdn.net/u011248221/article/details/107508263
6、https://blog.csdn.net/mapboo/article/details/105476167