点击 WidgetKit 小部件会自动启动其父应用程序。如何检测我的应用程序是否是从其 WidgetKit 小部件扩展启动的?
我无法找到任何有关在应用程序中捕获此内容的文档AppDelegate
and/or SceneDelegate
.
要检测从 WidgetKit 小部件扩展中启动的应用程序,其中父应用程序支持您需要实现的场景场景(_:openURLContexts:) https://developer.apple.com/documentation/uikit/uiscenedelegate/3238059-scene,用于从后台状态启动,以及场景(_:willConnectTo:选项:) https://developer.apple.com/documentation/uikit/uiscenedelegate/3197914-scene,用于从冷状态启动,在您的父应用程序中SceneDelegate
。另外,添加小部件URL(_:) https://developer.apple.com/documentation/swiftui/view/widgeturl(_:)到您的小部件的视图。
小部件的View
:
struct WidgetEntryView: View {
var entry: SimpleEntry
private static let deeplinkURL: URL = URL(string: "widget-deeplink://")!
var body: some View {
Text(entry.date, style: .time)
.widgetURL(WidgetEntryView.deeplinkURL)
}
}
家长申请SceneDelegate
:
// App launched
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _: UIWindowScene = scene as? UIWindowScene else { return }
maybeOpenedFromWidget(urlContexts: connectionOptions.urlContexts)
}
// App opened from background
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
maybeOpenedFromWidget(urlContexts: URLContexts)
}
private func maybeOpenedFromWidget(urlContexts: Set<UIOpenURLContext>) {
guard let _: UIOpenURLContext = urlContexts.first(where: { $0.url.scheme == "widget-deeplink" }) else { return }
print("???? Launched from widget")
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)