美文网首页SwiftUI
iOS 14-WidgetKit-桌面小组件-打开宿主APP指定

iOS 14-WidgetKit-桌面小组件-打开宿主APP指定

作者: CocoaJason | 来源:发表于2020-09-29 15:36 被阅读0次

    1. 示例代码

    Link(destination: URL(string: "you app`s scheme") ?? URL(string: "you app`s scheme")!){
                //View
        }
    
    widgetURL(<#T##url: URL?##URL?#>)
    

    Link 用于给某个view赋予跳转连接,widgetURL用于给整个小组件添加跳转连接


    2.在主工程AppDelegate.m中添加如下代码

    - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
        [self printUrl:url];
        return true;
    }
    - (void)printUrl:(NSURL *)url {
        NSLog(@"%@", url);
    }
    
    

    3 编写试验代码

    3.1. 先创建 CommonView.swift,可以用在不同组件中,其中放两个Text

    3.1.1 普通link代码,其中Text有不同的连接地址

    let homeLinkDestination = URL(string: "SwiftUIAndAppDemo://page=home")!
    let settingLinkDestination = URL(string: "SwiftUIAndAppDemo://page=setting")!
    
    struct CommonView: View {
        var body: some View {
            VStack {
                Link(destination: homeLinkDestination, label: {
                    Text("Home Page")
                })
                Spacer()
                Link(destination: settingLinkDestination, label: {
                    Text("Setting Page")
                })
            }
            .padding()
        }
    }
    
    

    3.2 我们为Extension_Two 创建三个不同的view,用于在不同尺寸下,展示不同的view

    @Environment(\.widgetFamily) var family
        var body: some View {
            switch family {
            case .systemSmall:
                Extension_Two_SmallView()
            case .systemMedium:
                Extension_Two_MediumView()
            default:
                Extension_Two_LargeView()
            }
        }
    

    3.3 分别编写三个不同view中的代码

    3.3.1 SmallView

    let smallWidgetUrl = URL(string: "SwiftUIAndAppDemo://widgetType=SmallView")!
    struct Extension_Two_SmallView: View {
        var body: some View {
            VStack {
                Text("Extension_Two_SmallView")
                Spacer()
                CommonView()
            }
            .widgetURL(smallWidgetUrl)
            
            .padding()
        }
    }
    

    3.3.2 MediumView

    let mediumWidgetUrl = URL(string: "SwiftUIAndAppDemo://widgetType=MediumView")!
    struct Extension_Two_MediumView: View {
        var body: some View {
            VStack {
                Text("Extension_Two_MediumView")
                Spacer()
                CommonView()
            }
            .widgetURL(mediumWidgetUrl)
            .padding()
        }
    }
    

    3.3.3 LargeView

    let largeWidgetUrl = URL(string: "SwiftUIAndAppDemo://widgetType=LargeView")!
    struct Extension_Two_LargeView: View {
        var body: some View {
            VStack {
                Text("Extension_Two_LargeView")
                Spacer()
                CommonView()
            }
            .widgetURL(largeWidgetUrl)
            .padding()
        }
    }
    
    Simulator Screen Shot - iPhone 12 - 2020-11-01 at 12.46.06.png Simulator Screen Shot - iPhone 12 - 2020-11-01 at 12.46.17.png

    3.3.4 分别打印上述三种view以及不同的点击位置如下

    卡片类型 点击Home 点击Setting 点击空白
    small SwiftUIAndAppDemo://widgetType=SmallView SwiftUIAndAppDemo://widgetType=SmallView SwiftUIAndAppDemo://widgetType=SmallView
    medium SwiftUIAndAppDemo://page=home SwiftUIAndAppDemo://page=setting SwiftUIAndAppDemo://widgetType=MediumView
    large SwiftUIAndAppDemo://page=home SwiftUIAndAppDemo://page=setting SwiftUIAndAppDemo://widgetType=LargeView

    3.4 在组件中某个view后面添加WidgetUrl,为Text 添加WidgetUrl

     Text("Setting Page")
                        .widgetURL(settingLinkDestination)
    

    卡片类型 点击Home 点击Setting 点击空白
    small、medium、large SwiftUIAndAppDemo://page=setting SwiftUIAndAppDemo://page=setting SwiftUIAndAppDemo://page=setting

    4.总结如下

    ·小尺寸的卡片,只能使用 widgetURL去处理跳转
    ·每个组件,只能处理一个Widget链接
    ·

    相关文章

      网友评论

        本文标题:iOS 14-WidgetKit-桌面小组件-打开宿主APP指定

        本文链接:https://www.haomeiwen.com/subject/qyyeuktx.html