我正在尝试实现一种通过iOS 14的小部件打开应用程序特定页面的方法,但是到目前为止,我只是简单地打开应用程序而没有获取完整的深层链接URL。
在主应用程序的SceneDelegate.swift
中:
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
if let item = URLContexts.first {
print(item.url)
}
}
在我的小部件的EntryView
中:
struct EntryWidgetEntryView : View {
var entry: Provider.Entry
var body: some View {
VStack(alignment: .leading, spacing: 4) {
Link(destination: URL(string: "entry://link1")!, label: {
Text("Link 1")
})
Link(destination: URL(string: "entry://link2")!, label: {
Text("Link 2")
})
}
}
}
当我通过小部件启动我的应用程序时,上述实现每次都会打印"entry://"
。无论我在窗口小部件上的任何位置还是在Text
标签上点击,都将打开我的应用并打印"entry://"
,而随后的信息(即link1,link2)将丢失。
我相信我已经在主应用程序中正确设置了info.plist
:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string></string>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>entry</string>
</array>
</dict>
</array>