【问题标题】:Change View from Appdelegate depending on push notification根据推送通知从 Appdelegate 更改视图
【发布时间】: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


【解决方案1】:

我已修复这似乎是一个常见问题,因此我很高兴分享我的解决方案。 仅当您的项目中有 Scenedelegate 时才有效!

guard var rootViewController = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window?.rootViewController else {
           return
       }
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "Requester") as! RequestViewController
rootViewController.present(controller, animated: true, completion: { () -> Void in

})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    相关资源
    最近更新 更多