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

时间:2016-04-26 18:16:29

标签: ios iphone watchkit apple-watch watch-os-2

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

提前致谢。

这是我使用的代码

在观看方面

//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];
 }

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

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

1 个答案:

答案 0 :(得分:0)

当您使用带有nil replyHandler的WCSession sendMessage API时,如下所示:

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

您需要在接收方实施以下委托方法:

- (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:。因此,在上面的示例中,手表端的InterfaceController1.m似乎无法像您所说的那样接收消息。我猜你可能只是因为你正在清理SO的代码而犯了一个复制粘贴错误。