Swift logout function
In xcode 11.4.1, in addition to AppDelegate.swift, there is a SceneDelegate.swift. I used UINavigationController() to realize the login -> main interface jump.
SceneDelegate.swift
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let _ = (scene as? UIWindowScene) else {
return }
let view = ViewController()
let nav = UINavigationController(rootViewController: view)
window = UIWindow(windowScene: scene as! UIWindowScene)
window?.rootViewController = nav
window?.backgroundColor = .white
}
}
When implementing the logout function, try: popToRootViewController has no effect:
// 没有效果
self.navigationController?.popToRootViewController(animated: false)
The correct approach should be:
func onLogout(_act: UIAlertAction) {
// self.navigationController?.popViewController(animated: false)
// self.navigationController?.popToRootViewController(animated: false)
// self.navigationController?.pushViewController(ViewController(), animated: false)
let view = ViewController() // 这是登录view
let nav = UINavigationController(rootViewController: view)
self.view.window?.rootViewController = nav
// 断开TCP连接
_ = IMManager.singleton.loginManager.logout(callback: nil)
}
Hereby record.
Reference: swift login interface jump
https://blog.csdn.net/wangxiaosu0501/article/details/80642141?utm_source=blogxgwz0