【发布时间】:2020-12-27 23:18:39
【问题描述】:
H 我需要根据我的推送通知中显示的标题从我的 Appdelegate 更改 ViewController。正如您在下面的扩展中看到的那样,我在前台模式下按下它后会收到我的推送通知并进行测试,如果它包含以下文本:“Dein Freund ist am Joggen”。它包含应该切换视图控制器的文本。我已经尝试了以下文章中的方法:
Opening view controller from app delegate using swift
主要问题是我没有窗口和声明窗口:UIwindow?或类似的东西根本没有帮助。所以我的问题是如何强制在此处显示 Viewcontroller
我的故事板已经初始化,我的 requestPage 应该会显示出来。
let storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let requestPage = storyboard.instantiateViewController(withIdentifier: "Requester") as! RequestViewController
这是扩展的完整代码
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
// Change this to your preferred presentation option
completionHandler([[.alert, .sound]])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
let application = UIApplication.shared
if(application.applicationState == .active){
print("user tapped the notification bar when the app is in foreground")
if(userInfo.description.contains("Dein Freund ist am Joggen")) {
let storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let requestPage = storyboard.instantiateViewController(withIdentifier: "Requester") as! RequestViewController
}
}
if(application.applicationState == .inactive)
{
print("user tapped the notification bar when the app is in background")
}
completionHandler()
}
}
有什么想法吗?
【问题讨论】:
-
您没有窗口的事实表明您正在使用 UIWindowScene 架构。所以你想做这一切的地方就是 scene 委托;那是窗户所在的地方。看一个五年前的讨论对你没有帮助。场景架构比这要新得多。
-
如何使用 scenedelegate 做到这一点?
标签: ios swift uikit viewcontroller appdelegate