


import WidgetKitimport SwiftUIimport Intents// 控制器,类似Controller,这里可以用来做小组件的刷新操作struct Provider: IntentTimelineProvider { func placeholder(in context: Context) -> SimpleEntry { SimpleEntry(date: Date(), configuration: ConfigurationIntent()) } func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) { let entry = SimpleEntry(date: Date(), configuration: configuration) completion(entry) } func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) { var entries: [SimpleEntry] = [] // Generate a timeline consisting of five entries an hour apart, starting from the current date. let currentDate = Date() for hourOffset in 0 ..< 5 { let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)! let entry = SimpleEntry(date: entryDate, configuration: configuration) entries.append(entry) } let timeline = Timeline(entries: entries, policy: .atEnd) completion(timeline) }}// 数据模型,数据显示在View上必须经过这里struct SimpleEntry: TimelineEntry { let date: Date let configuration: ConfigurationIntent}// View,小组件的界面struct WidgetExtensionEntryView : View { var entry: Provider.Entry var body: some View { Text(entry.date, style: .time) }}// 程序入口,初始化相关信息,如Provider,View等@mainstruct WidgetExtension: Widget { let kind: String = "WidgetExtension" var body: some WidgetConfiguration { IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in WidgetExtensionEntryView(entry: entry) } .configurationDisplayName("小组件") .description("This is an 测试一下 widget.") }}// 自定义样式struct WidgetExtension_Previews: PreviewProvider { static var previews: some View { // 设置小组件尺寸 systemSmall systemMedium systemLarge WidgetExtensionEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent())) .previewContext(WidgetPreviewContext(family: .systemSmall)) }}// 自定义样式struct WidgetExtension_Previews: PreviewProvider { static var previews: some View { // 设置小组件尺寸 systemSmall systemMedium systemLarge WidgetExtensionEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent())) .previewContext(WidgetPreviewContext(family: .systemSmall)) }}// View,小组件的界面struct WidgetExtensionEntryView : View { var entry: Provider.Entry var body: some View { // 深度布局,屏幕深度 ZStack(alignment: .center, content: { // 背景图 Image("2").resizable().aspectRatio(contentMode: .fit) // 水平 HStack(alignment: .center, spacing: 5, content: { // 左侧图 Image("1").frame(width: 80, height: 80, alignment: .center).aspectRatio(contentMode: .fit).cornerRadius(10.0) // 垂直 VStack(alignment: .center, spacing: 5, content: { // 右侧文字 Text("小组件1").foregroundColor(.blue) Text("小组件2").foregroundColor(.blue).lineLimit(2) }) }) }) }}// View,小组件的界面struct WidgetExtensionEntryView : View { var entry: Provider.Entry var body: some View { // 深度布局,屏幕深度 ZStack(alignment: .center, content: { // 背景图 Image("2").resizable().aspectRatio(contentMode: .fit) // 水平 HStack(alignment: .center, spacing: 5, content: { // 左侧图 Image("1").frame(width: 80, height: 80, alignment: .center).aspectRatio(contentMode: .fit).cornerRadius(10.0) // 垂直 VStack(alignment: .center, spacing: 5, content: { // 右侧文字 Text("小组件1").foregroundColor(.blue) Text("小组件2").foregroundColor(.blue).lineLimit(2) }) }) }).widgetURL(URL(string: "widgetExtensionDemo://test1")) }}接受数据
只能用SceneDelegate来接受数据,AppDelegate不行。
SceneDelegate
SceneDelegate中相应事件
- (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts{ NSLog(@"%s",__FUNCTION__); UIOpenURLContext * context = URLContexts.allObjects.firstObject; NSLog(@"%@", context.URL);}

// View,小组件的界面struct WidgetExtensionEntryView : View { @Environment(\.widgetFamily) var family:WidgetFamily var entry: Provider.Entry var body: some View { switch family { case .systemSmall: // 深度布局,屏幕深度 ZStack(alignment: .center, content: { // 背景图 Image("2").resizable().aspectRatio(contentMode: .fill) // 水平 HStack(alignment: .center, spacing: 5, content: { // 左侧图 Image("1").frame(width: 80, height: 80, alignment: .center).aspectRatio(contentMode: .fit).cornerRadius(10.0) // 垂直 VStack(alignment: .center, spacing: 5, content: { // 右侧文字 Text("小组件1").foregroundColor(.blue) Text("小组件2").foregroundColor(.blue).lineLimit(2) }) }) }).widgetURL(URL(string: "widgetExtensionDemo://test1")) case .systemMedium: // 深度布局,屏幕深度 ZStack(alignment: .center, content: { // 背景图 Image("2").resizable().aspectRatio(contentMode: .fill) // 水平 HStack(alignment: .top, spacing: 5, content: { // 左侧图 Image("1").resizable().aspectRatio(contentMode: .fit).frame(width: 200, height: 80, alignment: .leading).cornerRadius(10.0).foregroundColor(.blue) // 垂直 VStack(alignment: .trailing, spacing: 5, content: { // 右侧文字 Text("zh组件1").foregroundColor(.blue) Text("小组件2").foregroundColor(.blue).lineLimit(2) }).foregroundColor(.gray) }) }).widgetURL(URL(string: "widgetExtensionDemo://test2")) case .systemLarge: // 深度布局,屏幕深度 ZStack(alignment: .center, content: { // 背景图 Image("2").resizable().aspectRatio(contentMode: .fill) // 水平 HStack(alignment: .center, spacing: 5, content: { // 左侧图 Image("1").aspectRatio(contentMode: .fit).cornerRadius(10.0).frame(width: 200, height: 100, alignment: .leading) // 垂直 VStack(alignment: .center, spacing: 5, content: { // 右侧文字 Text("小组件1").foregroundColor(.blue) Text("小组件2").foregroundColor(.blue).lineLimit(2) }) }).foregroundColor(.blue) }).widgetURL(URL(string: "widgetExtensionDemo://test3")) default: // 深度布局,屏幕深度 ZStack(alignment: .center, content: { // 背景图 Image("2").resizable().aspectRatio(contentMode: .fit) // 水平 HStack(alignment: .center, spacing: 5, content: { // 左侧图 Image("1").frame(width: 80, height: 80, alignment: .center).aspectRatio(contentMode: .fit).cornerRadius(10.0) // 垂直 VStack(alignment: .center, spacing: 5, content: { // 右侧文字 Text("小组件1").foregroundColor(.blue) Text("小组件2").foregroundColor(.blue).lineLimit(2) }) }) }).widgetURL(URL(string: "widgetExtensionDemo://test1")) } }}// 更多小组件@mainstruct Widgets:WidgetBundle { init() { } @WidgetBundleBuilder var body: some Widget{ // 最多创建5次,也就是15个小组件 WidgetExtension() CustomWidget() CustomWidget() CustomWidget() CustomWidget() } }struct CustomWidget:Widget { var kind:String="自定义组件" var body: some WidgetConfiguration{ IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in CustomEntryView(entry:entry) } .configurationDisplayName("自定义更多组件") .description("ios14自定义更多小组件") } }// 自定义Uistruct CustomEntryView:View { @Environment(\.widgetFamily) var family:WidgetFamily var entry: Provider.Entry @ViewBuilder var body: some View { switch family { case .systemSmall: // 深度布局,屏幕深度 ZStack(alignment: .center, content: { // 背景图 Image("2").resizable().aspectRatio(contentMode: .fill) // 水平 HStack(alignment: .center, spacing: 5, content: { // 左侧图 Image("1").frame(width: 80, height: 80, alignment: .center).aspectRatio(contentMode: .fit).cornerRadius(10.0) // 垂直 VStack(alignment: .center, spacing: 5, content: { // 右侧文字 Text("小组件1").foregroundColor(.blue) Text("小组件2").foregroundColor(.blue).lineLimit(2) }) }) }).widgetURL(URL(string: "widgetExtensionDemo://test1")) case .systemMedium: // 深度布局,屏幕深度 ZStack(alignment: .center, content: { // 背景图 Image("2").resizable().aspectRatio(contentMode: .fill) // 水平 HStack(alignment: .top, spacing: 5, content: { // 左侧图 Image("1").resizable().aspectRatio(contentMode: .fit).frame(width: 200, height: 80, alignment: .leading).cornerRadius(10.0).foregroundColor(.blue) // 垂直 VStack(alignment: .trailing, spacing: 5, content: { // 右侧文字 Text("zh组件1").foregroundColor(.blue) Text("小组件2").foregroundColor(.blue).lineLimit(2) }).foregroundColor(.gray) }) }).widgetURL(URL(string: "widgetExtensionDemo://test2")) case .systemLarge: // 深度布局,屏幕深度 ZStack(alignment: .center, content: { // 背景图 Image("2").resizable().aspectRatio(contentMode: .fill) // 水平 HStack(alignment: .center, spacing: 5, content: { // 左侧图 Image("1").aspectRatio(contentMode: .fit).cornerRadius(10.0).frame(width: 200, height: 100, alignment: .leading) // 垂直 VStack(alignment: .center, spacing: 5, content: { // 右侧文字 Text("小组件1").foregroundColor(.blue) Text("小组件2").foregroundColor(.blue).lineLimit(2) }) }).foregroundColor(.blue) }).widgetURL(URL(string: "widgetExtensionDemo://test3")) default: // 深度布局,屏幕深度 ZStack(alignment: .center, content: { // 背景图 Image("2").resizable().aspectRatio(contentMode: .fit) // 水平 HStack(alignment: .center, spacing: 5, content: { // 左侧图 Image("1").frame(width: 80, height: 80, alignment: .center).aspectRatio(contentMode: .fit).cornerRadius(10.0) // 垂直 VStack(alignment: .center, spacing: 5, content: { // 右侧文字 Text("小组件1").foregroundColor(.blue) Text("小组件2").foregroundColor(.blue).lineLimit(2) }) }) }).widgetURL(URL(string: "widgetExtensionDemo://test1")) } }}参考1
参考2
本文来自博客园,作者:struggle_time,转载请注明原文链接:https://www.cnblogs.com/songliquan/p/15971298.html