我有一个带有链接的小工具.我要在SceneDelegate
中实现小部件URL.问题是,当我点击小工具时,我会得到一个链接,它起作用了.但是,当我在SceneDelegate
中调用View控制器函数时,它什么也不做.嗯,它可以打印一些东西,但它不会改变任何东西.SceneDelegate
:
// App launched
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: windowScene)
window.rootViewController = ViewController()
self.window = window
window.makeKeyAndVisible()
getURL(urlContext: connectionOptions.urlContexts)
}
// App opened from background
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
getURL(urlContext: URLContexts)
}
// Get URL
private func getURL(urlContext: Set<UIOpenURLContext>) {
guard let urlContexts = urlContext.first(where: { $0.url.scheme == "app-deeplink" }) else { return }
print(urlContexts.url)
print("success")
let viewController = ViewController()
viewController.cameFromWidget()
}
更改标签文本的视图控制器中的函数:
func cameFromWidget() {
label.text = "Hello"
print(label.text)
}
小窗口扩展名:
var body: some View {
Text(entry.date, style: .time)
.widgetURL(URL(string: "app-deeplink://app"))
}
因此,当我从SceneDelegate
开始调用时,视图控制器函数只打印文本,但不会更改它.
我的小部件链接只在WidgetExtension和SceneDelegate中,我没有将它添加到infoPlist中.
我的问题是:为什么它起作用了,却毫无作用?也许我应该把它加到某个文件里? 非常感谢!