前面实现的
Widget
只是纯展示的样子,界面搭建也只是用展示的Text
实现,当点击桌面Widget
的时候,打开的是对应的APP
。如果Widget
布局的时候用的是按钮、图片、链接等方式,想实现点击对应的跳转APP
对应的页面、事件,那么就需要额外处理。
根据官方文档的描述,点击Widget
窗口唤起APP
进行交互指定跳转支持两种方式:
-
widgetURL
:点击区域是Widget
的所有区域,适合元素、逻辑简单的小部件 -
Link
:通过Link
修饰,允许让界面上不同元素产生点击响应
Widget
支持三种显示方式,分别是systemSmall
、 systemMedium
、systemLarge
,其中:
1、systemSmall
只能用widgetURL
修饰符实现URL传递接收。
@ViewBuilder
var body: some View {
VStack(content: {
Image("h_buyao")
.resizable()
.frame(width: 50, height: 50)
.clipShape(Circle())
.overlay(Circle().stroke(Color.white, lineWidth: 4))
.shadow(radius: 10)
Text("二狗子你变了")
})
.background(Color.init(red: 144 / 255.0, green: 252 / 255.0, blue: 231 / 255.0))
.widgetURL(URL(string: "https://www.jianshu.com/u/bc4a806f89c5"))
}
2、systemMedium
、systemLarge
可以用Link
或者 widgetUrl
处理
var body: some View {
Link(destination: URL(string: "https://www.jianshu.com/u/bc4a806f89c5")!) {
VStack {
Image("h_buyao")
.resizable()
.frame(width: 50, height: 50)
.clipShape(Circle())
.overlay(Circle().stroke(Color.white, lineWidth: 4))
.shadow(radius: 10)
Text("二狗子你变了")
}
}
}
这两种方式的本质都是URL Schemes
在查找资料的时候,看到网上有的地方说在AppDelegate
实现OpenUrl
进行跳转处理:
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool
然而试了之后发现根本没有响应,其实是需要在SceneDelegate
里面实现跳转处理,因为iOS13
后,APP
的UI
生命周期交由SceneDelegate
管理,这里拿到需要的URL
,就能处理产品需求实现了。
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
for context in URLContexts {
print(context.url) // https://www.jianshu.com/u/bc4a806f89c5
}
}
参考资料
creating-a-widget-extension
https://swiftrocks.com
iOS13 URL Schemes 跳转与传值问题