我尝试使用 WCSession
方法从 iwatchExtension(在 ButtonClick 上)向 iPhone 应用发送消息
[[WCSession defaultSession] sendMessage:applicationData
replyHandler:^(NSDictionary *reply)
但是委托(delegate)方法
- (void)session:(WCSession *)session didReceiveMessage:
未被调用。
当我尝试将数据从 iPhone 传递到 iWatch 时,它工作正常。在“Extension Delegate”类中正确调用所有委托(delegate)方法。
最佳答案
如果你检查 WCSession
的定义委托(delegate)方法,您会发现它们的接收性质有细微差别。检查粗体引号。
/** Called on the delegate of the receiver. Will be called on startup if the incoming message caused the receiver to launch. */
- (void)session:(WCSession *)session didReceiveMessage:(NSDictionary *)message;/** Called on the delegate of the receiver when the sender sends a message that expects a reply. Will be called on startup if the incoming message caused the receiver to launch. */
- (void)session:(WCSession *)session didReceiveMessage:(NSDictionary *)message replyHandler:(void(^)(NSDictionary *replyMessage))replyHandler;
因为您为 replyHandler:
提供了一个非空参数,您应该在其他代表中收到消息,即 -(void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *, id> *)message
.
您正在使用 [WCSession defaultSession] sendMessage: ...] 进行通信,缺少委托(delegate)分配而且您还没有激活 session 。
在您的扩展类中,为 WCSession
创建一个 ivar并编写这段代码
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
// Configure interface objects here.
if([WCSession isSupported])
{
session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
}
然后发送类似[session sendMessage:applicationData replyHandler:^(NSDictionary *reply)
的消息.
在你的AppDelegate.m
文件只是创建 WCSession
的实例并激活它并使用正确的 delegate
方法来捕获消息。
if([WCSession isSupported]) {
appSession = [WCSession defaultSession];
appSession.delegate = self;
[appSession activateSession];
}
我相信这应该可以解决您的问题。
关于ios - 使用 watchOS 2 从 Apple Watch 向 iPhone 应用程序发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34268965/