userId 来 appdelegate 仍然不去 Mainviewcontroller 为什么?在 swift

userId coming to appdelegate still not going to Mainviewcontroller why? in swift

NavigationControllerisinitial viewcontroller in storyboard,NavigationController 嵌入到 LoginViewcontroller

在项目情节提要中导航将如下所示

NavigationController->LoginViewcontroller-> RegistrationViewcontroller -> MainViewcontroller

在 PhNUmber 成功注册后,我得到了 userId,我已将其存储在 KeychainWrapper

在 RegistrationViewcontroller 中: 我正在存储用户 ID,如下所示:

 let userID: String=jsonObj?["userId"] as? String ?? "" 
 KeychainWrapper.standard.set(userID, forKey: "USERID")

我正在检查 appdelegate,如下所示以进入 Mainviewcontroller:

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var savedUserId: String?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    savedUserId = KeychainWrapper.standard.string(forKey: "USERID")
    print("appdelegate userid \(savedUserId)")
    if savedUserId != nil{
        print("saveuserid \(savedUserId)")
        let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
        window?.rootViewController = mainStoryBoard.instantiateViewController(withIdentifier: "MainViewController")
    }
    return true
}
}

这里savedUserId来了那为什么我不去MainViewcontroller,一直出现LoginViewcontroller

您需要初始化 window。在上面的这一行中设置 rootViewController:

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var savedUserId: String?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        savedUserId = KeychainWrapper.standard.string(forKey: "USERID")
        print("appdelegate userid \(savedUserId)")
        if savedUserId != nil{
            print("saveuserid \(savedUserId)")
            let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
            window = UIWindow()
            window?.rootViewController = mainStoryBoard.instantiateViewController(withIdentifier: "MainViewController")
        }
        return true
    }
}

更新:如果您正在使用 SceneDelegate,您应该使用这个:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScene = scene as? UIWindowScene else { return }
        window = UIWindow(windowScene: windowScene)print("appdelegate userid \(savedUserId)")
        if savedUserId != nil{
            print("saveuserid \(savedUserId)")
            let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
            window = UIWindow()
            window?.rootViewController = mainStoryBoard.instantiateViewController(withIdentifier: "MainViewController")
        }
    }
}

尝试使用此扩展程序

  // add this function in your AppDelegate

  func makeRootVC(_ storyBoardName : String, _ vcName : String) {
        let vc = UIStoryboard(name: storyBoardName, bundle: Bundle.main).instantiateViewController(withIdentifier: vcName)
        let nav = UINavigationController(rootViewController: vc)
        nav.navigationBar.isHidden = true
        self.window?.rootViewController = nav
        let options: UIView.AnimationOptions = .transitionCrossDissolve
        let duration: TimeInterval = 0.6
        UIView.transition(with: self.window!, duration: duration, options: options, animations: {}, completion: nil)
    }

然后在您的 didFinishLaunchingWithOptions

中像这样使用它
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    savedUserId = KeychainWrapper.standard.string(forKey: "USERID")
    print("appdelegate userid \(savedUserId)")
    if savedUserId != nil{
        print("saveuserid \(savedUserId)")
        self.makeRootVC("Main","MainViewController")
    }
    return true
}

检查 window 是否为 nil 并在 AppDelegate 中设置 rootViewController 后添加 makeKeyAndVisible()

if(window == nil){
    self.window = UIWindow(frame:UIScreen.main.bounds)
}
if savedUserId != nil{
    print("saveuserid \(savedUserId)")
    let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
    window?.rootViewController = mainStoryBoard.instantiateViewController(withIdentifier: "MainViewController")
    self.window?.makeKeyAndVisible()
}