ios – Tips on how to dismiss view controller from SceneDelegate?
[ad_1]
I’m engaged on an ios App (Swift 5) and I’m attempting to indicate a display if the app is offline after which dismisses when the person re-connects.
I’m anticipating the OfflineViewController to look when the person is offline, and the final display the person on to look if they’re related.
What is going on is the OfflineViewController is showing once I disconnect from the community, nevertheless it doesn’t go away once I join again to the community. I attempted including a button to dismiss and this doesn’t work both.
I’ve hooked up my code beneath, any concept what am I doing mistaken?
SceneDelegate
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
let reachability = attempt! Reachability()
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, choices connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
// Ship to homepage if logged in, in any other case login display.
let accessToken: String? = KeychainWrapper.commonplace.string(forKey: "accessToken")
// If entry token exists, skip login web page
if accessToken != nil {
if let windowScene = scene as? UIWindowScene {
self.window = UIWindow(windowScene: windowScene)
let mainStoryboard:UIStoryboard = UIStoryboard(title: "Important", 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(title: "Important", 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.home windows.first else {
return
}
let rootVC = firstWindow.rootViewController
rootVC?.dismiss(animated: true, completion: nil)
rootVC!.current(vc, animated: true, completion: nil)
}
do {
attempt reachability.startNotifier()
} catch {
print("Unable to start out notifier")
}
}
}
OfflineViewController
import UIKit
class OfflineViewController: UIViewController {
let reachability = attempt! Reachability()
override func viewDidLoad() {
tremendous.viewDidLoad()
do {
attempt reachability.startNotifier()
} catch {
print("Unable to start out notifier")
}
}
@IBAction func hitRefresh(_ sender: Any) {
reachability.whenReachable = { reachability in
self.dismiss(animated: true, completion: nil)
}
}
}
[ad_2]