你只需要在Appdelegate.swift
'sapplication(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?)
方法中判断。
但前提是你应该手动操作窗口:
删除你的这一行info.plist
:
![在此处输入图像描述](https://i.stack.imgur.com/8LPcW.jpg)
然后在你的AppDelegate.swift
你可以手动设置你的窗口:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow.init(frame: UIScreen.main.bounds)
let sb:UIStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
let isLogin:Bool = UserDefaults.standard.bool(forKey: "isLogin")
if isLogin {
let vc2 = sb.instantiateViewController(withIdentifier: "ViewController2")
self.window?.rootViewController = vc2
}else {
let vc1 = sb.instantiateViewController(withIdentifier: "ViewController")
self.window?.rootViewController = vc1
}
self.window?.makeKeyAndVisible()
return true
}
在您的ViewController.swift
(您可以将其视为 LoginVc)中:
override func viewDidLoad() {
super.viewDidLoad()
/* add userdefaults */
UserDefaults.standard.set(true, forKey: "isLogin")
UserDefaults.standard.synchronize()
}