我正在开发一个Apple Watch应用程序,当父应用程序中发生某些更改时,我需要通知手表。我正在使用在GitHub上找到的MMWormhole库,但我在将消息从手机传递到手表时遇到了问题。这是我的代码,你知道为什么会发生这种情况吗?
我的主要viewController代码如下所示
ViewController.m
- (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
- (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];
}
}];
}谢谢!
发布于 2015-04-14 15:49:46
"com.mycompany.myapp"是你在应用程序中使用的真正价值吗?因为组标识符必须以group.开头。
如果使用了错误的组标识符,所有操作都会失败,因为MMWormhole内部的containerURLForSecurityApplicationGroupIdentifier调用返回nil。不幸的是,MMWormhole的开发人员没有做任何检查或断言来确保共享组标识符是正确的。
因此,我建议您暂时停止对MMWormhole的关注。相反,在代码中的早期添加此代码(例如applicationDidFinishLaunching),以验证容器标识符是否正确:
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");
}这会告诉你你的应用组设置是否不正确。
我不确定您在设置应用程序组方面走了多远,但您必须使用您在项目的应用程序组功能部分中使用的组标识符。

https://stackoverflow.com/questions/29609114
复制相似问题