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

无法设置初始ViewController:存储AccessToken后仍跳转至登录页问题

Troubleshooting Auto-Redirect to HomeViewController on App Launch

Hey there, let's break down why your auto-redirect logic isn't working and fix it step by step. Based on your setup (iOS 13.4, Swift 5, Storyboards), here are the most likely issues and solutions:

1. iOS 13+ Uses SceneDelegate for Window Management (Most Common Cause)

Starting with iOS 13, Apple moved window initialization and scene management to SceneDelegate.swift, so your code in AppDelegate might be getting ignored. The SceneDelegate takes over setting up the root view controller when the app launches or a new scene is created.

Fix: Move Your Access Token Check to SceneDelegate

Replace the logic in your SceneDelegate's scene(_:willConnectTo:options:) method:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }
    
    let window = UIWindow(windowScene: windowScene)
    self.window = window
    
    // Check access token here
    let accessToken: String? = KeychainWrapper.standard.string(forKey: "accessToken")
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    
    let rootVC: UIViewController
    if let token = accessToken, !token.isEmpty {
        rootVC = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
    } else {
        // Ensure your LoginViewController has the correct storyboard ID
        rootVC = storyboard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
    }
    
    window.rootViewController = rootVC
    window.makeKeyAndVisible()
}

2. Verify Access Token is Actually Stored in Keychain

It's possible your KeychainWrapper isn't retrieving the token correctly. Add debug prints to confirm what's being fetched:

let accessToken: String? = KeychainWrapper.standard.string(forKey: "accessToken")
print("Retrieved Access Token: \(accessToken ?? "NO TOKEN FOUND")") // Check console output

Double-check:

  • The key name "accessToken" matches exactly what you used when storing the token
  • Keychain access permissions are set correctly (make sure you're not accidentally deleting the token on app restart)

3. Ensure window is Properly Initialized in AppDelegate (For iOS 12 and Below)

If you need to support iOS versions below 13, update your AppDelegate code to explicitly create and configure the window:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Create window explicitly for iOS <13
    window = UIWindow(frame: UIScreen.main.bounds)
    
    let accessToken: String? = KeychainWrapper.standard.string(forKey: "accessToken")
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    
    if let token = accessToken, !token.isEmpty {
        let homePage = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
        window?.rootViewController = homePage
    } else {
        let loginPage = storyboard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
        window?.rootViewController = loginPage
    }
    
    window?.makeKeyAndVisible()
    return true
}

4. Double-Check Storyboard IDs

Make sure:

  • HomeViewController has the exact storyboard ID "HomeViewController" set in the Identity Inspector
  • LoginViewController either has its own matching storyboard ID, or you're handling the fallback case properly

内容的提问来源于stack exchange,提问作者Vladimír

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 18:12:56