gpt4 book ai didi

ios - WCSession 发送消息在实际设备上不起作用,但在模拟器上起作用

转载 作者:行者123 更新时间:2023-11-29 01:02:15 25 4
gpt4 key购买 nike

我正在从 iPhone 向 Watch (WatchOS2) 发送一条消息,其中 iPhone 上运行着一个内部计时器。每隔 30 秒,我就会从 iPhone 发送消息以供观看。不幸的是,发送消息仅在第一次工作,我的 watch 已成功接收。但是,从下一次开始 watch 就收不到任何消息了。整个场景在模拟器中完美运行,但在实际 watch 上却不行。任何帮助,将不胜感激。我试图从主线程发送消息但没有用。关于我哪里做错的任何想法。

提前致谢。

这是我使用的代码

在 watch 侧

//InterfaceController1.m
- (void)willActivate {
// This method is called when watch view controller is about to be visible to user
[super willActivate];
if ([WCSession isSupported]) {
self.session = [WCSession defaultSession];
self.session.delegate = self;
[self.session activateSession];
[self.session sendMessage:@{@"OpeniOS":@"YES"} replyHandler:nil errorHandler:nil];
}
}
- (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *, id> *)message replyHandler:(void(^)(NSDictionary<NSString *, id> *replyMessage))replyHandler{
//able to receive message - dictionary with IssuerNames Array from iPhone
[self setupTable:message]//I'm setting up my tableview and on click of a row from tableview I'm pushing the user to InterfaceController2.m
}

- (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex{
[self.session sendMessage:@{@"AccSel":@"YES"} replyHandler:nil errorHandler:nil];
[self pushControllerWithName:@"InterfaceController2" context:[NSNumber numberWithInteger:rowIndex]];
}

//InterfaceController2.m
- (void)willActivate {
[super willActivate];
if ([WCSession isSupported]) {
_session = [WCSession defaultSession];
_session.delegate = self;
[_session activateSession];
}
}

-(void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *,id> *)message replyHandler:(void(^)(NSDictionary<NSString *, id> *replyMessage))replyHandler{
NSLog(@"%@",message);
}

在iPhone侧

//ViewController1.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
if ([WCSession isSupported]) {
_session=[WCSession defaultSession];
_session.delegate=self;
[_session activateSession];
[_session sendMessage:@{@"testKey":@"testVal"} replyHandler:nil errorHandler:nil];
}
}
-(void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *,id> *)message{
if ([[message objectForKey:@"OpeniOS"] isEqualToString:@"YES"]) {
NSMutableArray *tempIssuerArray=[[NSMutableArray alloc] init];
for (OTPToken *token in self.tokenManager.tokens) {
[tempIssuerArray addObject:token.issuer];
}
if ([_session isReachable]) {
NSDictionary *temp=@{@"IssuerNames":tempIssuerArray};
[_session sendMessage:temp replyHandler:nil errorHandler:nil];
}
}
if ([[message objectForKey:@"AccSel"] isEqualToString:@"YES"]) {
OTPToken *token = [self.tokenManager.tokens objectAtIndex:[[message objectForKey:@"selIndex"] intValue]];
DisplayTokenViewController *dtvc=[self.storyboard instantiateViewControllerWithIdentifier:@"DisplayToken"];
dtvc.token=token;
dtvc.tkm=self.tokenManager;
[self.navigationController pushViewController:dtvc animated:YES];
}
}

//ViewController2.m
-(void)viewDidLoad {
[super viewDidLoad];
mySession=[WCSession defaultSession];
mySession.delegate=self;
[mySession activateSession];
[self refresh]; //this refresh method is called every 30 seconds based on a property change value
}

- (void)refresh{
NSDictionary* dict=@{@"code":@"123",@"name":@"abc"};
[mySession sendMessage:dict replyHandler:nil errorHandler:nil];
}

实际上,在 watch 端,InterfaceController1.m 首先显示给用户,点击 InterfaceController1.m 的按钮,用户重定向到 InterfaceController2.m。同时,在 iPhone 端,我在收到来自 watch 的消息时从 ViewController1.m 推送 ViewController2.m。

这里,refresh 方法只调用一次,每 30 秒调用一次,理想情况下应该调用 refresh 方法,而不是在实际设备中调用。但一切都在模拟器中完美运行

最佳答案

当您将 WCSession sendMessage API 与 nil replyHandler 一起使用时,如下所示:

[_session sendMessage:@{@"testKey":@"testVal"} replyHandler:nil errorHandler:nil];

您需要在接收端实现以下委托(delegate)方法:

- (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *, id> *)message {
//able to receive message - dictionary with IssuerNames Array from iPhone
[self setupTable:message]//I'm setting up my tableview and on click of a row from tableview I'm pushing the user to InterfaceController2.m
}

而不是 session :didReceiveMessage:replyHandler:。因此,在上面的示例中, watch 端的 InterfaceController1.m 似乎无法像您所说的那样接收消息。我猜你可能只是在为 SO 清理代码时犯了复制粘贴错误。

关于ios - WCSession 发送消息在实际设备上不起作用,但在模拟器上起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36872761/

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