1. 基本介紹
Widget Extension 創(chuàng)建看這里 -> iOS 14 小組件(1):WidgetExtension 創(chuàng)建及報(bào)錯(cuò)詳解
這一篇介紹一下如何處理長按的彈出框以及網(wǎng)絡(luò)數(shù)據(jù)交互更新。
2. 效果展示

Edit Widget Extension.gif
3. 網(wǎng)絡(luò)數(shù)據(jù)處理
其實(shí)小組件刷新原理很簡單,流程就是 Provider -> Entry -> EntryView,這么個(gè)邏輯。我們?cè)?Provider 中更新 entry 的屬性,然后在 EntryView 中可以拿到這個(gè) entry,于是就可以完成數(shù)據(jù)的更新展示,當(dāng)然 entry 的屬性參數(shù)都是可以自定義的。
這里我就只寫一下網(wǎng)絡(luò)數(shù)據(jù)的處理,至于 Request 請(qǐng)求,我就不找虛擬接口來處理了。
import WidgetKit
import SwiftUI
import Intents
struct Provider: IntentTimelineProvider {
let response = ResponseObject(test1: "default_test1", test2: "default_test2")
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), configuration: ConfigurationIntent(), response: response)
}
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date(), configuration: configuration, response: response)
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)!
// 在這里做網(wǎng)絡(luò)請(qǐng)求,拿到請(qǐng)求結(jié)果 netResponse 用來更新頁面
let netResponse = ResponseObject(test1: "net_test1", test2: "net_test2")
let entry = SimpleEntry(date: entryDate, configuration: configuration, response: netResponse)
entries.append(entry)
}
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
}
struct ResponseObject {
let test1 : String
let test2 : String
}
struct SimpleEntry: TimelineEntry {
let date: Date
let configuration: ConfigurationIntent
let response : ResponseObject
}
struct WidgetExEntryView : View {
var entry: Provider.Entry
var body: some View {
ZStack(alignment: .leading, content: {
Image("widgetImage")
.frame(width: 100, height: 100, alignment: .center)
HStack(alignment: .center, spacing: 15, content: {
Image("xcode")
VStack(alignment: .leading, spacing: 15, content: {
Text(entry.response.test1)
.foregroundColor(.white)
Text(entry.response.test2)
.foregroundColor(.white)
.lineLimit(2)
})
})
}).widgetURL(URL(string: "widget://test/widgetImage"))
}
}
@main
struct WidgetEx: Widget {
let kind: String = "WidgetEx"
var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
WidgetExEntryView(entry: entry)
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
}
}
struct WidgetEx_Previews: PreviewProvider {
static var previews: some View {
let response = ResponseObject(test1: "default_test1", test2: "default_test2")
WidgetExEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent(), response: response))
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}
添加前效果圖(注意圖中文字變化)

default small.png

default large.png
添加后效果圖(注意圖中文字變化)

net small medium.png
4. 長按交互
其實(shí)長按交互事件和代碼沒啥關(guān)系,主要是 .intentdefinition 文件的配置,我這里直接上圖了。

Enum.png

String.png

Enum detail.png

Type detail.png
效果圖如下。

Edit Widget Image.png

Edit Widget Extension.gif
5. 技術(shù)小結(jié)
- Timeline 雖然可以刷新小組件的樣式,但是系統(tǒng)有一個(gè)刷新次數(shù)和間隔設(shè)定,可能并不會(huì)和你預(yù)期一樣實(shí)時(shí)動(dòng)態(tài)刷新。
- 長按的交互窗口,屬性其實(shí)還是很多的,需要自行嘗試。