グランスが最初に表示されたときに、一般的なウェルカム メッセージを でユーザーに提供できますinit
。次に、willActivate
(または、おそらくawakeWithContext:
)コントロールを設定し、タイマーを開始して、更新されたコンテンツを定期的に取得します。
Ivp が提案したように、更新されたデータは共有コンテナーに保存する必要があります。( NSUserDefaults:initWithSuiteName:を見てください)
グランスが起動する前に iPhone アプリが起動されることがわかっている場合は、一般的なウェルカム メッセージをスキップして、共有コンテナーにデータをシードするだけに頼ることができます。
上記で説明した内容のサンプルを次に示しますが、共有コンテナー内のデータのストレージは示していません。
- (instancetype)init {
self = [super init];
if (self) {
if (isFirstLaunch) {
self.displayInfo = [self createWelcomeInfo];
}
}
return self;
}
- (void)willActivate {
[super willActivate];
// refreshUI will check for new data, request more for next time,
// and then configure the display labels with the new data.
[self refreshUI];
// Setup a timer to refresh the glance based on new trends provided by the phone app.
// NOTE: This timer is never invalidated because didDeactivate seems to never be called.
// If, by chance, willActivate is called more than once, we may end up with multiple repeating timers.
self.refreshTimer = [NSTimer scheduledTimerWithTimeInterval:kTimeIntervalRefresh
target:self
selector:@selector(refreshUI)
userInfo:nil
repeats:YES];
}
- (void)refreshUI {
if (! isFirstLaunch ) {
if (self.nextDisplayInfo) {
self.displayInfo = self.nextDisplayInfo;
}
// requestMoreInfo will populate self.nextDisplayInfo asynchronously,
// so it's ready for the next time you are in refreshUI
[self requestMoreInfo];
}
[self configureLabels:self.displayInfo];
// Setup for when the user taps the glance
[self updateUserActivity:@"com.example.AppName" userInfo:@{@"data": self.displayInfo} webpageURL:nil];
}