前面實(shí)現(xiàn)的
Widget
只是純展示的樣子,界面搭建也只是用展示的Text
實(shí)現(xiàn)腌巾,當(dāng)點(diǎn)擊桌面Widget
的時候,打開的是對應(yīng)的APP
铲觉。如果Widget
布局的時候用的是按鈕澈蝙、圖片、鏈接等方式撵幽,想實(shí)現(xiàn)點(diǎn)擊對應(yīng)的跳轉(zhuǎn)APP
對應(yīng)的頁面灯荧、事件,那么就需要額外處理盐杂。
根據(jù)官方文檔的描述逗载,點(diǎn)擊Widget
窗口喚起APP
進(jìn)行交互指定跳轉(zhuǎn)支持兩種方式:
-
widgetURL
:點(diǎn)擊區(qū)域是Widget
的所有區(qū)域,適合元素链烈、邏輯簡單的小部件 -
Link
:通過Link
修飾厉斟,允許讓界面上不同元素產(chǎn)生點(diǎn)擊響應(yīng)
Widget
支持三種顯示方式,分別是systemSmall
强衡、 systemMedium
擦秽、systemLarge
,其中:
1、systemSmall
只能用widgetURL
修飾符實(shí)現(xiàn)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: "http://www.reibang.com/u/bc4a806f89c5"))
}
2缩搅、systemMedium
、systemLarge
可以用Link
或者 widgetUrl
處理
var body: some View {
Link(destination: URL(string: "http://www.reibang.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("二狗子你變了")
}
}
}
這兩種方式的本質(zhì)都是URL Schemes
在查找資料的時候链快,看到網(wǎng)上有的地方說在AppDelegate
實(shí)現(xiàn)OpenUrl
進(jìn)行跳轉(zhuǎn)處理:
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool
然而試了之后發(fā)現(xiàn)根本沒有響應(yīng),其實(shí)是需要在SceneDelegate
里面實(shí)現(xiàn)跳轉(zhuǎn)處理眉尸,因?yàn)?code>iOS13后域蜗,APP
的UI
生命周期交由SceneDelegate
管理,這里拿到需要的URL
噪猾,就能處理產(chǎn)品需求實(shí)現(xiàn)了霉祸。
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
for context in URLContexts {
print(context.url) // http://www.reibang.com/u/bc4a806f89c5
}
}
參考資料
creating-a-widget-extension
https://swiftrocks.com
iOS13 URL Schemes 跳轉(zhuǎn)與傳值問題