在不运行时保持我的WatchKit并发症是最新的 [英] Keeping my WatchKit complications up to date when not running

查看:154
本文介绍了在不运行时保持我的WatchKit并发症是最新的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发我的应用程序的WatchKit扩展程序,并且遇到了一些并发症问题。

I'm working on the WatchKit Extension of my app, and have some issues with complications.

我有一个显示给定总量的并发症,这取决于用户在iOS应用程序上做了什么。当WatchKit扩展程序运行时,iOS应用程序使用 - [WCSession updateApplicationContext:] 方法更新监视应用程序上下文。它工作正常,然后在我的Watch应用程序的ExtensionDelegate中,我用新数据手动更新复杂性。

I have a complication that displays a given total amount, which depends of what the user is doing on the iOS app. When the WatchKit Extension is running, the iOS app updates the watch app context using the -[WCSession updateApplicationContext:] method. It works fine, and then in the ExtensionDelegate of my Watch app, I manually update the complication with the new data.

但只有在扩展运行时才可以(如果不是,它将无法获得应用程序上下文直到下一次启动。)

But this is OK only when the extension is running (if it's not, it won't get the application context until the next launch).

所以我编辑了我的代码,以便在用户更改时将复杂数据直接发送到Watch iOS应用程序中的一些东西,使用 - [WCSession transferCurrentComplicationUserInfo:] 方法(在文档中写入应该唤醒ExtensionDelegate以在后台接收用户信息)。

So I edited my code to send the complication data directly to the Watch when user changed something in the iOS app, using the -[WCSession transferCurrentComplicationUserInfo:] method (it's written in the documentation that the ExtensionDelegate should be woken up to receive the user info in background).

我已经实现了ExtensionDelegate的 -session:didReceiveUserInfo:方法,以便在收到数据时更新并发症来自iOS应用程序,但是当扩展程序未运行时它不起作用...(我不知道它是否曾收到用户信息,因为我无法记录它)

I've implemented the -session:didReceiveUserInfo: method of the ExtensionDelegate to update the complication when it received data from the iOS app, but it doesn't work when the extension is not running... (and I don't know if it ever receives the user info as I can't log it)

即使在什么时候,我该怎样做才能使我的并发症保持最新状态扩展程序没有运行??

How should I do to keep my complications up to date even when the extension is not running??

谢谢

PS:我正在使用Watch Simulator,并且关闭扩展我只是重新启动Watch(从硬件菜单)

PS: I'm using the Watch Simulator, and to "close" the extension I just Reboot the Watch (from the Hardware menu)

编辑:我设法在应用程序出现时注销语句没有运行(通过打开Watch Simulator系统日志),当iOS将新的复杂用户数据发送到监视扩展时,我得到这些行:

Edit: I managed to log out statements when the app is not running (by opening the Watch Simulator system log), and I get these lines when the iOS send a new complication user data to the watch extension:


Oct 18 18:08:11 pc16 WatchApp Extension [26615]:扩展收到了
请求唤醒并发症支持。

Oct 18 18:08:11 pc16 WatchApp Extension[26615]: Extension received request to wake up for complication support.

10月18日18: 08:11 pc16断言[26585]:断言失败:15A284 13S343:断言+ 15398 [B48FCADB-A071-3A46-878B-538DC0AFF60B]:0x1

Oct 18 18:08:11 pc16 assertiond[26585]: assertion failed: 15A284 13S343: assertiond + 15398 [B48FCADB-A071-3A46-878B-538DC0AFF60B]: 0x1

因此手表很好地接收了用户信息词典,但似乎无法唤醒扩展程序......

So the watch receives well the user info dictionary, but seems to fail waking up the extension...

编辑2 :这是ExtensionDelegate中应该接收comp的代码部分用户信息(但在应用程序未运行时未调用):

Edit 2: here is the part of code in the ExtensionDelegate that should receive the complication user info (but which is not called when the app is not running):

- (void) session: (WCSession *)session didReceiveUserInfo: (NSDictionary *)userInfo
{
    NSLog(@"session:didReceiveUserInfo: %@", userInfo);

    NSString *userInfoType = userInfo[KHWASessionTransferUserInfoType];
    NSDictionary *userInfoContents = userInfo[KHWASessionTransferUserInfoContents];

    // Complication Data
    if ([userInfoType isEqualToString:KHWASessionTransferUserInfoTypeComplicationData]) {

        // Store the complication data into user defaults
        [[NSUserDefaults standardUserDefaults] setValue:userInfoContents[KHWAComplicationTotalBalance] forKey:KHWAComplicationTotalBalance];
        [[NSUserDefaults standardUserDefaults] synchronize];

        // And refresh the complications
        CLKComplicationServer *complicationServer = [CLKComplicationServer sharedInstance];
        for (CLKComplication *complication in complicationServer.activeComplications) {
            [complicationServer reloadTimelineForComplication:complication];
        }
    }
}

编辑3 :WCSession在扩展委托中设置 applicationDidFinishLaunching 方法:

Edit 3: the WCSession is set in the extension delegate applicationDidFinishLaunching method:

- (void) applicationDidFinishLaunching
{
    // Setup the WatchConnectivity session
    WCSession *session = [WCSession defaultSession];
    session.delegate = self;
    [session activateSession];

    [...]
}


推荐答案

哇,我终于解决了这个问题!

Wow, I finally resolved the issue!

看来,即使我没有在日志文件中看到它(请参阅我的上一篇文章)评论),WCExtensionDelegate的 init 方法在唤醒应用程序时被很好地调用。

It seems that, even if I didn't see it in the log files (see my last comment), the init method of WCExtensionDelegate is well called when waking up the app.

所以我只需要将WCSession设置块移动到 init 方法:

So I just had to move the WCSession setting bloc into the init method :

- (id) init
{
    if (self = [super init]) {

        // Setup the WatchConnectivity session
        WCSession *session = [WCSession defaultSession];
        session.delegate = self;
        [session activateSession];
    }

    return self;
}

暂时工作正常......

And for the while it works fine...

这篇关于在不运行时保持我的WatchKit并发症是最新的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆