Swift点击通知跳转指定ViewController代码问题求助
Hey there! I see you're having trouble getting your app to navigate to the right ViewController when a notification is tapped—let's sort this out step by step, since you're new to Swift 😊
First, let's break down the issues in your current code:
- Directly replacing the
rootViewControllerisn't ideal for most app structures (like if you're using aUINavigationControllerorUITabBarController), because it breaks your existing navigation stack and loses user context. - On iOS 13+,
self.windowmight benilsince apps use window scenes now, so you need a reliable way to get the active window. - You aren't handling navigation when the app is in the foreground (you just print a message, but you probably want to jump to the target VC there too).
Here's the corrected code that works for most common app setups, with clear explanations:
Full Updated Code
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { print("Notification tapped:", response) // Step 1: Get the active key window (works for iOS 13+) guard let window = UIApplication.shared.connectedScenes .filter({ $0.activationState == .foregroundActive }) .compactMap({ $0 as? UIWindowScene }) .first?.windows .first(where: { $0.isKeyWindow }) else { completionHandler() return } // Step 2: Load your target ViewController let storyboard = UIStoryboard(name: "Main", bundle: nil) guard let targetVC = storyboard.instantiateViewController(withIdentifier: "notificationcenter") as? NotificationsListViewController else { print("Couldn't load NotificationsListViewController—check storyboard ID and class name!") completionHandler() return } // Step 3: Navigate based on your app's root controller type switch window.rootViewController { case let navController as UINavigationController: // If root is a Navigation Controller: Push the VC onto the existing stack navController.pushViewController(targetVC, animated: true) case let tabBarController as UITabBarController: // If root is a Tab Bar Controller: Switch to a desired tab first, then push tabBarController.selectedIndex = 0 // Update this to your target tab index if let selectedNav = tabBarController.selectedViewController as? UINavigationController { selectedNav.pushViewController(targetVC, animated: true) } else { // If the selected tab doesn't have a nav controller, present the VC modally tabBarController.selectedViewController?.present(targetVC, animated: true) } default: // If root is a plain ViewController: Present the VC modally targetVC.modalPresentationStyle = .fullScreen window.rootViewController?.present(targetVC, animated: true) } completionHandler() }
Critical Checks to Ensure It Works:
- Storyboard ID: Make sure your
NotificationsListViewControllerin the Main storyboard has its Storyboard ID set exactly to"notificationcenter"(case-sensitive!). You can set this in the Identity Inspector in Xcode. - Class Name: Double-check that the ViewController's class is assigned to
NotificationsListViewController(not the defaultUIViewController). - Navigation Style: Adjust the navigation logic (push/present/tab switch) to match your app's structure. For example, if you want to present the VC modally even in a navigation stack, just replace the push code with the present logic.
- Foreground/Background Behavior: This code works seamlessly whether the app is in the foreground or background—no separate checks needed unless you want different behavior for each state.
That should get your notification tap navigating to the right place! Let me know if you run into any other hiccups.
内容的提问来源于stack exchange,提问作者user12180762
相关产品推荐
相关产品推荐

