gpt4 book ai didi

ios - 使用 MMWormhole 将消息传递给 Apple Watch

转载 作者:行者123 更新时间:2023-11-29 02:07:49 28 4
gpt4 key购买 nike

我正在开发一个 Apple Watch 应用程序,我需要在父应用程序发生某些更改时通知 watch 。我正在使用 GitHub 上的 MMWormhole 库,但我无法将消息从手机传递到 watch 。这是我的代码,您知道为什么会这样吗?

我的主要 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];
}
}];
}

谢谢!

最佳答案

"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");
}

如果您的应用组设置不正确,这会告诉您。

我不确定您设置应用组的进度,但您必须使用在项目的“应用组功能”部分中使用的组标识符。

enter image description here

关于ios - 使用 MMWormhole 将消息传递给 Apple Watch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29609114/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com