我已经用一些UIButtons创建了一个今天的扩展,我想在用户点击按钮时打开一个特定的视图控制器。它现在工作时,主应用程序是打开的,但当它关闭时,它将只打开主应用程序,而不是导航到我的特定视图控制器。
我所做的:我已经设置了URL,并编写了OpenURL代码,如果按钮是按下TodayExtension视图控制器。
@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!)
}
}
}
}但就像我说过的,这只在主应用程序之前被打开时才起作用。如果主ap关闭了,我要怎么做才能做到这一点?
在谷歌的帮助下,我发现我必须设置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)
}但这不管用。
希望有人能帮我。
发布于 2020-10-23 11:48:44
最后我解决了我的问题。
我不得不在场景willConnectTo中加上一行..。职能:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
if let url = connectionOptions.urlContexts.first?.url {
UIApplication.shared.open(url)
self.scene(scene, openURLContexts: connectionOptions.urlContexts)
}
}https://stackoverflow.com/questions/63641970
复制相似问题