我正在開發一個 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")
}
}
}
離線視圖控制器
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)
}
}
}
uj5u.com熱心網友回復:
我將開始從OfflineViewController. 解散的邏輯屬于呈現的邏輯。
然后使用whenReachable關閉當前OfflineViewController.
您還應該避免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)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/529803.html
標籤:IOS迅速代码斯威夫特5
