如何从SceneDelegate中取消视图控制器



我正在开发一款ios应用程序(Swift 5(,如果该应用程序离线,我会尝试显示一个屏幕,然后在用户重新连接时关闭。

我希望OfflineViewController在用户脱机时显示,如果用户已连接,则会显示用户所在的最后一个屏幕。

发生的情况是,当我断开与网络的连接时,OfflineViewController会出现,但当我连接回网络时,它不会消失。我试着添加一个按钮来关闭,但这也不起作用。

我在下面附上了我的代码,你知道我做错了什么吗?

场景电报

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
let reachability = try! Reachability()

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

guard let _ = (scene as? UIWindowScene) else { return }
// Send to homepage if logged in, otherwise login screen.
let accessToken: String? = KeychainWrapper.standard.string(forKey: "accessToken")

// If access token exists, skip login page
if accessToken != nil {            
if let windowScene = scene as? UIWindowScene {
self.window = UIWindow(windowScene: windowScene)
let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = mainStoryboard.instantiateViewController(withIdentifier: "homeTabController") as! TabBarController
self.window!.rootViewController = vc
}
}
reachability.whenUnreachable = { [self] _ in
print("Not reachable (Scene delegate)")
let storyboard = UIStoryboard(name: "Main", bundle: nil)

let vc = storyboard.instantiateViewController(withIdentifier: "OfflineViewController") as! OfflineViewController
vc.modalPresentationStyle = .fullScreen
guard let firstScene = UIApplication.shared.connectedScenes.first as? UIWindowScene else {
return
}
guard let firstWindow = firstScene.windows.first else {
return
}

let rootVC = firstWindow.rootViewController
rootVC?.dismiss(animated: true, completion: nil)
rootVC!.present(vc, animated: true, completion: nil)
}
do {
try reachability.startNotifier()
} catch {
print("Unable to start notifier")
}
}
}

OfflineViewController

import UIKit
class OfflineViewController: UIViewController {

let reachability = try! Reachability()
override func viewDidLoad() {
super.viewDidLoad()
do {
try reachability.startNotifier()
} catch {
print("Unable to start notifier")
}
}

@IBAction func hitRefresh(_ sender: Any) {
reachability.whenReachable = { reachability in
self.dismiss(animated: true, completion: nil)
}
}
}

我将开始从OfflineViewController中删除所有可达性代码。否定的逻辑属于呈现的逻辑。

然后用解除当前OfflineViewControllerwhenReachable块更新场景代理中的可达性代码。

还应避免在场景代理代码中使用UIApplication.shared.connectedScenes。你已经知道这个场景了。无需去寻找。

更新后的whenUnreachable:

reachability.whenUnreachable = { [self] _ in
print("Not reachable (Scene delegate)")
let storyboard = UIStoryboard(name: "Main", bundle: nil)

let vc = storyboard.instantiateViewController(withIdentifier: "OfflineViewController") as! OfflineViewController
vc.modalPresentationStyle = .fullScreen
guard let winScene = (scene as? UIWindowScene) else { return }
guard let firstWindow = winScene.windows.first else {
return
}

let rootVC = firstWindow.rootViewController
rootVC?.dismiss(animated: true, completion: nil)
rootVC?.present(vc, animated: true, completion: nil)
}

添加的whenReachable:

reachability.whenReachable = { [self] _ in
print("Reachable (Scene delegate)")
guard let winScene = (scene as? UIWindowScene) else { return }
guard let firstWindow = winScene.windows.first else {
return
}

let rootVC = firstWindow.rootViewController
rootVC?.dismiss(animated: true, completion: nil)
}

最新更新