第一次出现您的目光时,您可以在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];
}