はじめに
今回はアプリ側のデータをWidgetKitにて表示を行うために、App Groupsを使用してデータの共有を行いたいと思います。
App Groupsとは簡単にいうと、同一デベロッパーが開発したアプリであれば、共有領域にデータを保存することによって、複数のアプリ間でデータの読み書きが行える機能になります。
App Groupsの準備
WidgetKitの準備はこちらの記事をご確認ください。今回はこの記事で作成したWidgetに追加する形でサンプルを記載しています。
App Groupsの許可
まずはホストのアプリのTARGETにある、Signing & CapabilitiesタブでApp Groupsを追加してください。そうすると以下の項目が追加されます。
+ボタンを押下し、グループ名を入力します。今回のサンプルでは「group.com.example.WidgetSample」とします。
そうしたならばWidgetKitのSigning & CapabilitiesタブからApp Groupsを追加します。そうすると先ほど追加したグループが表示されるのでチェックを入れて有効化してください。
App Groupsの使い方
App Groupsの使い方は簡単でUserDefaultsに先ほど指定したIDを使用することで、共有領域にデータを保存することができます。
1 2 | UserDefaults(suiteName:"group.com.example.WidgetSample")? .setValue(count,forKey:"CountRecordKey") |
WidgetKitにデータ共有
これで準備は完了したので、早速WidgetKitにデータを共有して表示を行いたいと思います。
まずはアプリ側でデータの保存とWidgetKitの更新を行います。
1 2 3 4 5 | funcreloadCount(count:Int){ UserDefaults(suiteName:"group.com.example.WidgetSample")? .setValue(count,forKey:"CountRecordKey") WidgetCenter.shared.reloadAllTimelines() } |
WidgetKit側にUtilitiesというstructを作成し、データ取得用のメソッドを追加します。
1 2 3 4 5 6 7 8 | structUtilities{ staticletgroup=UserDefaults(suiteName:"group.com.example.WidgetSample") staticfunccountOfRecord()->Int{ letcount=Utilities.group?.integer(forKey:"CountRecordKey")??0 returncount } } |
TimelineEntryに今回表示するようのcountを追加します。
1 2 3 4 5 6 | structSimpleEntry: TimelineEntry{ letdate:Date varname:String varcount:Int } |
entryを作成する際にデータ取得用のメソッドを呼びます。
1 2 3 | letentry=SimpleEntry(date:entryDate, name:configuration.Name??"", count:Utilities.countOfRecord()) |
追加したcountを表示するようにします。
1 2 3 4 5 6 7 8 9 10 11 12 13 | structWidgetTest1EntryView: View{ varentry:Provider.Entry varbody:someView{ Text(entry.name) .font(.headline) .padding(1) Text("\(entry.count)") .font(.headline) .padding(1) Text(entry.date,style:.time) } } |
今回追加したコードを含めたWidgetKitのコード全文になります。
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | importWidgetKit importSwiftUI importIntents structProvider: IntentTimelineProvider{ typealiasIntent=ConfigurationIntent funcplaceholder(incontext:Context)->SimpleEntry{ SimpleEntry(date:Date(),name:"",count:0) } funcgetSnapshot(forconfiguration:ConfigurationIntent,incontext:Context,completion:@escaping(SimpleEntry)->()){ letentry=SimpleEntry(date:Date(),name:"",count:0) completion(entry) } funcgetTimeline(forconfiguration:ConfigurationIntent,incontext:Context,completion:@escaping(Timeline<Entry>)->()){ varentries:[SimpleEntry]=[] // Generate a timeline consisting of five entries an hour apart, starting from the current date. letcurrentDate=Date() forhourOffset in0..<5{ letentryDate=Calendar.current.date(byAdding:.hour,value:hourOffset,to:currentDate)! letentry=SimpleEntry(date:entryDate,name:configuration.Name??"",count:Utilities.countOfRecord()) entries.append(entry) } lettimeline=Timeline(entries:entries,policy:.atEnd) completion(timeline) } } structSimpleEntry: TimelineEntry{ letdate:Date varname:String varcount:Int } structWidgetTest1EntryView: View{ varentry:Provider.Entry varbody:someView{ Text(entry.name) .font(.headline) .padding(1) Text("\(entry.count)") .font(.headline) .padding(1) Text(entry.date,style:.time) } } @main structWidgetTest1: Widget{ letkind:String="WidgetTest1" varbody:someWidgetConfiguration{ IntentConfiguration(kind:kind,intent:ConfigurationIntent.self,provider:Provider()){entry in WidgetTest1EntryView(entry:entry) } .configurationDisplayName("My Widget") .description("This is an example widget.") } } structWidgetTest1_Previews: PreviewProvider{ staticvarpreviews:someView{ WidgetTest1EntryView(entry:SimpleEntry(date:Date(),name:"鈴木",count:0)) .previewContext(WidgetPreviewContext(family:.systemSmall)) } } structUtilities{ staticletgroup=UserDefaults(suiteName:"group.com.example.WidgetSample") staticfunccountOfRecord()->Int{ letcount=Utilities.group?.integer(forKey:"CountRecordKey")??0 returncount } } |
さいごに
アプリのデータをWodgetに表示するのはマストだと思いますので、この記事が誰かの役に立てば幸いです。