Apple Watch - 更新背景

时间:2015-01-15 14:42:22

标签: ios background apple-watch

我正在开发AppleWatch应用程序,我想知道是否可以在后台更新一瞥。当用户打开一瞥时,我想立即更新一瞥,而不是等到一瞥从服务中收到更新的信息并提供它。

目前我正在使用此功能更新一瞥,但这将显示更新的信息,而不是立即更新。

- (void)awakeWithContext:(id)context // This function can't spend too much time processing the data otherwise we will se a black background. Can't call the service here.
{
    //Configure the interface using the cached info. 
    [super awakeWithContext:context];
}


- (void)willActivate 
{
    //Configure the interface using the service
    [super willActivate];
}

有什么想法吗?

提前致谢。

2 个答案:

答案 0 :(得分:1)

您可以在iOS应用中安排后台获取,并在获取后,将数据保存到共享组容器。 然后在您的glance控制器的awakeWithContext方法中,您可以从该共享组容器中获取数据。它应该足够快。

答案 1 :(得分:-1)

您第一次看到它时,可以在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];    
}