我用一些 UIButtons 创建了一个今天的扩展,我想在用户点击一个按钮时打开一个特定的视图控制器。当主应用程序打开时,它现在可以工作,但是当它关闭时,它只会打开主应用程序并且不会导航到我的特定视图控制器。
我所做的:如果在TodayExtension View Controller中按下按钮,我已经设置了 URL Sheme 并编写了 OpenURL 代码。
@objc func buttonClick(sender: UIButton) {
let tag = sender.tag
if let url = URL(string: "OpenURL://\(tag)")
{
self.extensionContext?.open(url, completionHandler: nil)
}
}
在SceneDelegate我编写了导航到特定视图控制器的代码func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
if let url = URLContexts.first?.url {
if url.scheme == "OpenURL" {
guard let rootViewController = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window?.rootViewController else {
return
}
let storyboard = UIStoryboard(name: "Groups", bundle: nil)
if let rootVC = storyboard.instantiateViewController(withIdentifier: "Create") as? CreateSingleGroupViewController,
let tabBarController = rootViewController as? UITabBarController,
let navController = tabBarController.selectedViewController as? UINavigationController {
navController.pushViewController(rootVC, animated: true)
rootVC.buttonTag = Int(url.host!)
}
}
}
}
但是就像我说的那样,这仅在之前打开主应用程序时才有效。即使主应用程序关闭,我该怎么做才能工作?
在 Google 的帮助下,我发现我必须设置该func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
功能,但我不知道如何设置。我写的是
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
self.scene(scene, openURLContexts: connectionOptions.urlContexts)
}
但这不起作用。
希望可以有人帮帮我。