當用戶按下推送通知時,我想導航到某個螢屏。但是,self.window?.rootViewController不斷給我錯誤,即Thread 1: Swift runtime failure: force unwrapped a nil value.
現在我已嘗試使用此解決方案。它確實有效;但是,它要求我將SceneDelegate.swift檔案與其他檔案一起洗掉,而我不想這樣做。
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
print("userNotificationCenter didReceive")
defer {
completionHandler()
}
guard response.actionIdentifier == UNNotificationDefaultActionIdentifier else {
return
}
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationVC = storyboard.instantiateViewController(withIdentifier: "MyInfo") as! MyInfoViewController
let navigationController = self.window?.rootViewController as! UINavigationController
navigationController.pushViewController(destinationVC, animated: false)
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
}
當用戶按下推送通知時,任何人都可以向我展示一種導航到某個特定位置的替代方法嗎?先感謝您。
uj5u.com熱心網友回復:
你可以試試這個;
UIApplication.shared.keyWindow?.rootViewController
使用 keyWindow 代替 window
uj5u.com熱心網友回復:
您應該改用 keyWindow。試試這個擴展:
extension UIApplication
{
static func getKeyWindow() -> UIWindow? {
return shared
.connectedScenes
.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }
.first { $0.isKeyWindow }
}
}
進而:
let navigationController = UIApplication.getKeyWindow()?.rootViewController as! UINavigationController
navigationController.pushViewController(destinationVC, animated: false)
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/346660.html
標籤:ios 迅速 推送通知 导航控制器 uiscenedelegate
下一篇:無法將'()->()'型別的值轉換為預期的引數型別'(LongPressGesture.Value)->Void'(又名'(Bool)-&
