首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用MMWormhole将消息传递给Apple Watch

使用MMWormhole将消息传递给Apple Watch
EN

Stack Overflow用户
提问于 2015-04-13 23:23:23
回答 1查看 1.4K关注 0票数 0

我正在开发一个Apple Watch应用程序,当父应用程序中发生某些更改时,我需要通知手表。我正在使用在GitHub上找到的MMWormhole库,但我在将消息从手机传递到手表时遇到了问题。这是我的代码,你知道为什么会发生这种情况吗?

我的主要viewController代码如下所示

ViewController.m

代码语言:javascript
复制
- (void)viewDidLoad {
    [super viewDidLoad];

    self.wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:@"com.mycompany.myapp"
                                                         optionalDirectory:@"wormhole"];

    NSString *myString = [[NSString alloc] initWithFormat:@"Test String"];


    [self.wormhole passMessageObject:@{@"string" : myString}
                      identifier:@"messageIdentifier"];

我的WatchkitExtension中的InterfaceController如下所示:

InterfaceController.m

代码语言:javascript
复制
- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];

    // Initialize the wormhole
    self.wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:@"com.mycompany.myapp"
                                                         optionalDirectory:@"wormhole"];

    // Obtain an initial value for the selection message from the wormhole
    id messageObject = [self.wormhole messageWithIdentifier:@"messageIdentifier"];

    NSString *string = [messageObject valueForKey:@"string"];

    if (string != nil) {
        NSLog(string);
        [myLabel setText:string];
    }

    // Listen for changes to the selection message. The selection message contains a string value
    // identified by the selectionString key. Note that the type of the key is included in the
    // name of the key.
    [self.wormhole listenForMessageWithIdentifier:@"messageIdentifier" listener:^(id messageObject) {

        NSString *string = [messageObject valueForKey:@"string"];

        if (string != nil) {
            [self.myLabel setText:string];
        }
    }];                                    
}

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2015-04-14 15:49:46

"com.mycompany.myapp"是你在应用程序中使用的真正价值吗?因为组标识符必须以group.开头。

如果使用了错误的组标识符,所有操作都会失败,因为MMWormhole内部的containerURLForSecurityApplicationGroupIdentifier调用返回nil。不幸的是,MMWormhole的开发人员没有做任何检查或断言来确保共享组标识符是正确的。

因此,我建议您暂时停止对MMWormhole的关注。相反,在代码中的早期添加此代码(例如applicationDidFinishLaunching),以验证容器标识符是否正确:

代码语言:javascript
复制
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSURL *appGroupContainer = [fileManager containerURLForSecurityApplicationGroupIdentifier:@"group.com.mycompany.myapp"];
if (!appGroupContainer) {
    NSLog(@"group identifier incorrect, or app groups not setup correctly");
}

这会告诉你你的应用组设置是否不正确。

我不确定您在设置应用程序组方面走了多远,但您必须使用您在项目的应用程序组功能部分中使用的组标识符。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29609114

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档