第一次出现您的一瞥时,您可以在init
. 然后,在willActivate
(或者,我想,awakeWithContext:
)填充您的控件并启动一个计时器以定期获取更新的内容以供查看。
正如 Ivp 所建议的,您应该将更新的数据存储在共享容器中。(看看NSUserDefaults:initWithSuiteName:)
如果您知道您的 iPhone 应用程序将在您的 Glance 启动之前启动,您可以跳过一般的欢迎消息,而只依靠它来为共享容器中的数据播种。
这是我上面描述的示例,尽管它没有显示数据在共享容器中的存储。
- (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];
}