WWDC2020 - Meet WidgetKit 을 보고 작성한 글입니다. 2024년 기준으로 많은 변화가 있었으므로 전체적인 흐름만 참고 부탁드립니다
위젯은 켰을때 로딩되면 안되고, 바로 보여야 하기 때문에 타임라인 이라는 것을 활용하여 미리 데이터를 생성한다.
-> WidgetKit extension 은 background extension 이다.
Small (2x2)
medium (2x4)
large (4x4)
extra large (4x8)
모든 사이즈를 지원할 필요는 없다.
일반적인 디자인이 있다.



폰트 : SF Font Family 사용을 추천한다. 커스텀 폰트를 추천하지 않는다.
글자를 최소화 해라. 그래픽으로 해결할 수 있다면 글자를 줄여서 직관적으로 바꿔라.
@main
public struct SampleWidget: Widget {
private let kind: String = "SampleWidget"
public var body: some WidgetConfiguration {
StaticConfiguration(
kind: kind,
provider: Provider(),
placeholder: PlaceholderView()) { entry in
SampleWidgetEntryView(entry: entry)
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
}
}
Intent 위젯의 코드는 게시글 하단에 있습니다.위젯에는 오직 탭 상호작용 밖에 없다. (영상, 스크롤 X)
위젯 전체를 앱을 열기위한 버튼으로 하거나,
일부분을 앱의 특정 widget URL API 로 앱의 특정 화면으로 연결할 수 있다.

-> Default Content. 아무 데이터도 받지 않았을 때 보이는 화면 (ex 로딩 되기 전의 위젯 화면)
-> 더미 데이터가 아닌 지금 위젯이 어떻게 보이는지 나타낸다. 가장 최신 버전의 위젯의 뷰를 빠르게 보여주기 위해 시스템이 single entry를 표시하는 경우.
-> 시간에 알맞게 표시되는 여러 뷰의 집합

TimelineProvider 프로토콜에 들어가는 것
associatedtype Entry: TimelineEntry : 날짜typealias Context = TimelineProviderContext : 타임라인을 요청할 때 현재의 환경 정보func getSnapshot() : 시스템이 single entry를 요청할 때funt getTimeline() : 시스템이 series of entrise를 요청할 때public struct Provider: TimelineProvider {
// single entry를 만들어서 반환한다.
public func snapshot(with context: Context,
completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date())
completion(entry)
}
// entry 배열을 만들어 반환하고 데이터 업데이트 정책을 정한다.
public func timeline(with context: Context,
completion: @escaping (Timeline<Entry>) -> ()) {
let entry = SimpleEntry(date: Date())
let timeline = Timeline(entries: [entry, entry], policy: .atEnd)
completion(timeline)
}
}
atEnd: 타임라인이 끝날 때 reload 를 요청after(date: Date): 특정 날짜 이후 reload 요청never: reload 안한다.기본적인 타임라인 리로드 외에도 리로드가 필요한 경우가 있다.
WidgetCenter 을 사용하면 앱 프로세스 또는 익스텐션에서 타임라인 업데이트 가능
reloadTimelines(ofKind:) : 타임라인중 골라서 리로드reloadAllTimelines : 전체 타임라인 리로드getCurrentConfigurations(completion:) : 현재 구성을 리스트로 받아올 수 있음더 많은 정보를 얻기 위해 서버에 쿼리해야 하는 경우
onBackgroundURLSessionEvents 로 서버에서 익스텐션으로 정보 받아올 수 있다.위에서 설명한 대로 사용자가 설정할 수 있는 위젯이다.
Intents는 유저에게 질문할 수 있는 Parameters 집합을 포함한다. (ex 날씨 위젯에서 위치, 주식 위젯에서 회사)
WWDC2020 What's New in SiriKit and Shortcuts 영상 참고.위에서 작성한 staticConfiguration 위젯의 코드를 IntentConfiguration 코드로 바꿔보자.
@main
public struct SampleWidget: Widget {
private let kind: String = "SampleWidget"
public var body: some WidgetConfiguration {
// 이 아래 부분이 다르다.
IntentConfiguration(kind: kind,
intent: ConfigurationIntent.self
provider: Provider(),
placeholder: PlaceholderView()) { entry in
SampleWidgetEntryView(entry: entry)
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
}
}
// 프로토콜 다르다
public struct Provider: IntentTimelineProvider {
// 파라미터가 다르다.
public func timeline(for configuration: ConfigurationIntent, with context: Context,
completion: @escaping (Timeline<Entry>) -> ()) {
let entry = SimpleEntry(date: Date(), configuration: configuration)
// 여기서 Intent 에 알맞은 타임라인을 생성하면 된다.
completion(timeline)
}
}
위젯이 스택에 있을때 다른 위젯들과 비교하여 적절한 시간에 등장하도록 하는 법
WWDC 2020 Enable Widget Peersonalization and Intelligence 참고