ios 極光推送獲取當前控制器進行介面跳轉

weixin_34391445發表於2017-03-29
#pragma mark - 通過推送返回的資料決定跳轉到那個頁面
- (void)JPushjudgeToWhichVC{
    if ([self.type isEqualToString:@"1"]) {
        //外鏈和APP內活動頁
        KindDetailViewController * KindVC = [[KindDetailViewController alloc] init];
        KindVC.url = self.redirect;
        [[self getCurrentVC] presentViewController:KindVC animated:YES completion:nil];
        
    }
}
#pragma mark - 獲取當前的顯示的控制器
- (UIViewController *)getCurrentVC{

    UIViewController *currVC = nil;
    UIViewController *rootvc = self.window.rootViewController;
    do {
        if ([Rootvc isKindOfClass:[UINavigationController class]]) {//控制器為UINavigationController
            UINavigationController *nav = (UINavigationController *)rootvc;
            UIViewController *vc = [nav.viewControllers lastObject];
            currVC = vc;
            rootvc = vc.presentedViewController;
            
        }else if ([Rootvc isKindOfClass:[UITabBarController class]]){//控制器為UITabBarController
            UITabBarController *tabVC = (UITabBarController *)rootvc;
            currVC = tabVC ;
            rootvc = [tabVC.viewControllers objectAtIndex:tabVC.selectedIndex];
            continue;
        }else {//控制器為UIViewController
            UIViewController *VC = (UIViewController *)rootvc;
            currVC = VC;
            Rootvc = VC.presentedViewController;
        }
    } while (Rootvc != nil);
    return currVC;
}

參考資料http://www.jianshu.com/p/a277790c9dc4 在其原有基礎上多新增了一個當前控制是UIViewController 的判斷和處理

相關文章