當用戶離線時,我正在嘗試設定一種干凈的處理方式,目前如果用戶離線,我正在將一個新螢屏推送到堆疊頂部,并帶有一條連接到互聯網的訊息。這對于設定它的一個螢屏非常有效,但我希望此代碼可以在用戶所在的任何螢屏上運行。我正在嘗試將其放入應用程式委托中,并在所有螢屏上共享代碼,但它不起作用。最終目標是如果用戶在我的應用程式中的任何螢屏上并且失去連接,以顯示離線視圖控制器 - 目前這僅在主螢屏上作業。
如果可能的話,我也不想使用 Alamo / Firebase 或任何其他第三方來處理這個問題。
這是主螢屏代碼,一切都按預期作業:
import UIKit
class ViewController: UIViewController, UIAlertViewDelegate {
let reachability = try! Reachability()
override func viewDidLoad() {
super.viewDidLoad()
// When network is unreachable, present offline view controller
reachability.whenUnreachable = { _ in
let vc = self.storyboard?.instantiateViewController(withIdentifier: "OfflineViewController")
vc?.modalPresentationStyle = .fullScreen
self.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 tapTryAgain(_ sender: Any) {
reachability.whenReachable = { reachability in
self.dismiss(animated: true, completion: nil)
}
}
}
現在,當我嘗試將所有代碼放在應用程式委托中時,它不起作用 - 這是我需要幫助的部分 - 請注意,當我嘗試此操作時,我正在從主螢屏注釋掉可達性代碼
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
let reachability = try! Reachability()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Note nothing happens when user goes offline with this code
reachability.whenUnreachable = { _ in
print("Not reachable")
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let offlineVC = storyboard.instantiateViewController(withIdentifier: "OfflineViewController")
offlineVC.modalPresentationStyle = .fullScreen
let appDelegate = UIApplication.shared.delegate
appDelegate?.window??.addSubview(offlineVC.view)
appDelegate?.window??.bringSubviewToFront(offlineVC.view)
}
do {
try reachability.startNotifier()
} catch {
print("Unable to start notifier")
}
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
}
uj5u.com熱心網友回復:
我想問題是你正在嘗試使用windowfrom AppDelegate,但它總是為零,因為你正在使用SceneDelegate. 只需將此代碼移至SceneDelegate,一切都會正常運行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/529806.html
標籤:IOS迅速代码斯威夫特5
下一篇:AdMob:幀大小無效
