iOS Widget 03 - Dynamic Launcher Widgets

If you want to select a widget bundle based on some logic, example in App has 10 Widgets, but I just want to display two Widgets when man add the Widgets in the Desktop. You can do the following:

Demo-03 Download.zip

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@main // Use the customized WidgetBundle as the initialization entry of the widget
struct WidgetLauncher {
    
    static func main() {
        var isShowAll: Bool {
            // Put logic here
            false
        }
        
        // or can with other logic
        if isShowAll {
            CustomWidgetBundle.main()
        } else {
            CustomWidgetBundle1.main()
        }
    }
}

struct CustomWidgetBundle: WidgetBundle {
    @WidgetBundleBuilder
    var body: some Widget {
		Widget1()
		Widget2()
		Widget3()
    }
}

struct CustomWidgetBundle1: WidgetBundle {
    @WidgetBundleBuilder
    var body: some Widget {
		Widget4()
		Widget5()
    }
}

Targets

img

Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy