/*
?獲取頂層控制器的三種方案(兼容iOS 13)
?第1種和第2種方案有可能返回的是UITextEffectsWindow下的UIInputWindowController
?所以推薦使用第3種方案
?*/
- (UIViewController *)getTopViewController1{
? ? __blockUIViewController*topVC =nil;
? ? if(@available(iOS13, *)) {
? ? ? ? NSSet *windowScenes = [UIApplication sharedApplication].connectedScenes;
? ? ? ? [windowScenesenumerateObjectsUsingBlock:^(UIWindowScene*windowScene,BOOL*_Nonnullstop) {
? ? ? ? ? ? if (windowScene.activationState == UISceneActivationStateForegroundActive)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? topVC = windowScene.windows.lastObject.rootViewController;
? ? ? ? ? ? ? ? *stop =YES;
? ? ? ? ? ? }
? ? ? ? }];
? ? }else{
? ? ? ? topVC = [UIApplication sharedApplication].keyWindow.rootViewController;
? ? }
? ? while (topVC.presentedViewController) {
? ? ? ? topVC = topVC.presentedViewController;
? ? }
? ? returntopVC;
}
- (UIViewController *)getTopViewController2{
? ? UIViewController *appRootVC = [UIApplication sharedApplication].windows.lastObject.rootViewController;
? ? UIViewController*topVC = appRootVC;
? ? while (topVC.presentedViewController) {
? ? ? ? topVC = topVC.presentedViewController;
? ? }
? ? returntopVC;
}
- (UIViewController *)getTopViewController3{
? ? NSArray *windows = [UIApplication sharedApplication].windows;
? ? __blockUIViewController*topVC =nil;
? ? [windowsenumerateObjectsUsingBlock:^(UIWindow*window,NSUIntegeridx,BOOL*_Nonnullstop) {
? ? ? ? if([windowisKeyWindow]) {
? ? ? ? ? ? topVC = window.rootViewController;
? ? ? ? ? ? *stop =YES;
? ? ? ? }
? ? }];
? ? while (topVC.presentedViewController) {
? ? ? ? topVC = topVC.presentedViewController;
? ? }
? ? returntopVC;
}