iOS Widget 小组件打开其他APP✨仿TopWidget快捷启动✨ 附常用URL schemes

开发一个小组件App,有一项需求是快捷启动指定功能,比如微信扫一扫,付款码等功能,如下图所示:

快捷启动分为两部分:
1.点击指定区域,进入App并传入需要打开的功能链接
2.从App中打开传入的链接

一.小组件打开App并传入链接

根据官方文档的描述,点击Widget窗口唤起APP进行交互指定跳转支持两种方式:

widgetURL:点击区域是Widget的所有区域,适合元素、逻辑简单的小部件。

Link:通过Link修饰,允许让界面上不同元素产生点击响应
 
Widget支持三种显示方式,分别是systemSmall、 systemMedium、systemLarge,其中:

 
1、systemSmall(小号组件)只能用widgetURL修饰符实现URL传递接收。

//MARK: -小组件
//快捷启动small
struct ShortcutWidgetViewS : View {
    
    
    var date: Date
    var data: wShortcutData
    var body: some View {
    
    
        let info = data.infos[0]
                ZStack{
    
    
                    Image(uiImage: data.bg).resizable()
                	
                }.widgetURL(URL(string:"weixin://scanqrcode"))//这里是跳转微信扫一扫链接
    }
}

widgetURL可以挂在任意view即可生效
同一组件多次使用widgetURL,只生效最后一个

 
 

2.systemMedium、systemLarge可以用Link或者 widgetUrl处理

//MARK: -大组件
//快捷启动Large
struct ShortcutWidgetViewL : View {
    
    
    
    var body: some View {
    
    
        
        ZStack{
    
    
                    Image(uiImage: data.bg).resizable()
                    VStack{
    
    
                        HStack{
    
    
                       		//微信扫一扫链接
                            SwiftUIShortcutCellL(urlPath:"weixin://scanqrcode")
   							//扫一扫链接
                            SwiftUIShortcutCellL(urlPath:"alipay://platformapi/startapp?saId=10000007")
                        }
                        HStack{
    
    
                        	//健康码链接
                            SwiftUIShortcutCellL(urlPath:"alipay://platformapi/startapp?appId=68687564")
                            //乘车码链接
                            SwiftUIShortcutCellL(urlPath:"alipay://platformapi/startapp?saId=200011235")
                        }
                        HStack{
    
    
                        	//付款码链接
                            SwiftUIShortcutCellL(urlPath:"alipay://platformapi/startapp?appId=20000056")
                            //QQ扫一扫链接
                            SwiftUIShortcutCellL(urlPath:"mqq://qrcode/scan_qrcode?version=1&src_type=app")
                        }
                    }
                       
                    
                }
    }
}


struct SwiftUIShortcutCellL:View{
    
    
    var urlPath:String!//跳转的链接
    var body:some View{
    
    
        Link(destination: URL(string: urlPath)!) {
    
    
                ZStack(content: {
    
    
					//点击区域的UI
					Image(uiImage: data.bg)
                })
                
            } 
    }
}

二.从App中打开传入的链接

通过步骤一,我们实现了点击小组件,打开App并传入数据

SceneDelegate中实现以下代码接收数据
App我用的是swift+UIKit ,用其他方式的大佬自行转换代码

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    
    
        print(URLContexts)
        print(URLContexts.first!.url)
    }

输出数据如下
在这里插入图片描述
至此我们拿到了点击区域对应的URL,然后跳转链接就可以了

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    
    
        print("URLContexts: \(URLContexts)")
        print("URL: \(URLContexts.first!.url)")
        UIApplication.shared.open(URLContexts.first!.url, options: [:], completionHandler: nil)
    }

三.URL schemes

跳转链接使用的方式是URL schemes
亲测有效
 
常用 URL schemes 大集合点这里

猜你喜欢

转载自blog.csdn.net/hzhnzmyz/article/details/119112823