You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CardinalMobile SDK OTP关闭后,如何恢复UINavigationController为根控制器?

解决CardinalMobile SDK关闭OTP界面后SVProgressHUD无法显示的问题

问题现象

使用CardinalMobile SDK展示OTP验证界面,关闭后SVProgressHUD无法正常显示加载状态,检查发现UIApplication.sharedApplication.keyWindow.rootViewController从原本的UINavigationController变成了UIInputWindowController。

解决方案

1. 提前保存主根控制器,强制指定SVProgressHUD的容器视图

在App启动时先保存好正常的根控制器,显示HUD时手动指定容器:

// AppDelegate.h中声明
@property (nonatomic, strong) UIViewController *mainRootVC;

// AppDelegate.m的didFinishLaunchingWithOptions方法中初始化
self.mainRootVC = self.window.rootViewController;

// 业务代码中显示HUD时
[SVProgressHUD setContainerView:self.mainRootVC.view];
[SVProgressHUD showWithStatus:@"加载中..."];

2. 在OTP界面关闭回调中恢复rootViewController

找到CardinalMobile SDK的OTP关闭回调方法,手动将rootViewController改回保存的主根控制器:

- (void)onOTPVerificationClosed {
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    if ([keyWindow.rootViewController isKindOfClass:NSClassFromString(@"UIInputWindowController")]) {
        keyWindow.rootViewController = ((AppDelegate *)[UIApplication sharedApplication].delegate).mainRootVC;
    }
}

3. 重写SVProgressHUD的容器获取逻辑(无需修改业务代码)

通过Category修改SVProgressHUD获取容器的逻辑,自动跳过UIInputWindowController:

// SVProgressHUD+FixRootVC.h
#import "SVProgressHUD.h"

@interface SVProgressHUD (FixRootVC)
@end

// SVProgressHUD+FixRootVC.m
#import "SVProgressHUD+FixRootVC.h"

@implementation SVProgressHUD (FixRootVC)

+ (UIView *)containerView {
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    UIViewController *rootVC = keyWindow.rootViewController;
    // 跳过异常的UIInputWindowController,找到真正的主根控制器
    while ([rootVC isKindOfClass:NSClassFromString(@"UIInputWindowController")]) {
        rootVC = ((AppDelegate *)[UIApplication sharedApplication].delegate).mainRootVC;
        if (rootVC) break;
    }
    // 处理存在presentedViewController的情况
    while (rootVC.presentedViewController) {
        rootVC = rootVC.presentedViewController;
    }
    return rootVC.view;
}

@end

注意事项

  • 务必提前保存主根控制器,避免后续找不到正确的视图容器
  • 使用Category修改SVProgressHUD时,注意和SDK版本的兼容性,后续更新SVProgressHUD时需验证逻辑是否有效
  • 优先选择在SDK回调中恢复rootViewController的方案,从根源上解决控制器被替换的问题

内容的提问来源于stack exchange,提问作者Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 07:04:55