美文网首页
WatchConnectivity相互通信

WatchConnectivity相互通信

作者: Apple技术产品粉 | 来源:发表于2017-10-08 11:47 被阅读0次

在iOS 9.0+和watchOS 2.0+的时候,WatchConnectivity是可以进行手机和手表之间的双向通信的。而在之前,只能有手表主动发起连接,而手机端是无法向手表端发送回执的。

如何双向通信

看一个例子,在这个例子中,手表端点击发送按钮向手机端发送一条消息,消息通过本地通知的方式展现出来。然后,手机向手表发送一个回执。

Watch端 iPhone端

首先需要在手机端和手表端都初始化一个WCSession对象

if([WCSession isSupported])

{

      WCSession *session=[WCSession defaultSession];

      session.delegate=self;

      [session activateSession];

}

前提是先导入WatchConnectivity/WatchConnectivity.h文件并且引用WCSessionDelegate

然后分别在手表端和手机端分别实现代理方法sendMessage:ReplyHandler:方法和didReceiveMessage:ReplyHandler:,其中ReplyHandler方法就是接收完成后发送的回执。

手表端:

if([WCSession defaultSession].isReachable)   

 {        

         NSDictionary *msg=@{@"msg":@"你好iPhone,我是Apple Watch"};       

         [[WCSession defaultSession]sendMessage:msg replyHandler:^(NSDictionary* _Nonnull replyMessage) {

         NSString *reply=replyMessage[@"msg"];

         [self.ResultLabel setText:reply];

         } errorHandler:^(NSError * _Nonnull error) {

         }]; 

}

手机端:

-(void)session:(WCSession *)session didReceiveMessage:(NSDictionary*)message replyHandler:(nonnull void (^)(NSDictionary* _Nonnull))replyHandler

{

   NSString *msg=[NSString stringWithFormat:@"%@",message[@"msg"]];

   UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];

   //需创建一个包含待通知内容的 UNMutableNotificationContent 对象,注意不是    UNNotificationContent ,此对象为不可变对象。

   UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];

   content.title = [NSString localizedUserNotificationStringForKey:@"Hello!" arguments:nil];  

   content.body = [NSString localizedUserNotificationStringForKey:msg

   arguments:nil];

    content.sound = [UNNotificationSound defaultSound];

    // 在 alertTime 后推送本地推送

    UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger

triggerWithTimeInterval:1 repeats:NO];

    UNNotificationRequest* request = [UNNotificationRequest     requestWithIdentifier:@"FiveSecond"

content:content trigger:trigger];

//添加推送成功后的处理!

[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {

    NSDictionary *replyMSG=@{@"msg":[NSString stringWithFormat:@"你好watch,我是       iPhone,你的消息已收到 %@",[NSDate date].description]};

    replyHandler(replyMSG);

    }];

}

这样最简单的相互通信就完成了。

相关文章

  • WatchConnectivity相互通信

    在iOS 9.0+和watchOS 2.0+的时候,WatchConnectivity是可以进行手机和手表之间的双...

  • 让人闹心的WatchConnectivity

    最近开发一款手表项目,需要手机手表通信。选择WatchConnectivity来实现功能。以前也没研究过,于是先来...

  • iWatch issue集

    问题一、WatchConnectivity Error Domain=WCErrorDomain Code=701...

  • 进程间通信,线程间通信

    进程间通信 进程间通信又称IPC(Inter-Process Communication),指多个进程之间相互通信...

  • WatchConnectivity:学习 WCSession

    作者:Natasha The Robot,原文链接,原文日期:2015-09-21译者:小袋子;校对:numbbb...

  • Kubernetes进阶

    1. k8s的网络模型是什么? 容器与容器之间要能相互通信 pod和pod之间要能相互通信 任意pod和任意ser...

  • Flutter js 相互(调用)通信

    应用场景: 在Flutter 中显示一个网页(H5之类),然后在Flutter中可以调用网页js里的方法; js里...

  • vue中的组件间通信

    父子组件相互通信 非父子组件间通信 关于vuex请查阅: https://www.jianshu.com/writ...

  • Java线程通信

    线程通信 线程通信指的是多个线程在运行的期间,相互之间的数据交互协作。 1.通信方式 实现多个线程直接的协作,涉及...

  • 【Vue】组件通信(任意通信)

    本节所需的基础知识: 【Vue】组件通信(父传子props) 【Vue】组件通信(子传父$emit) 任意组件相互...

网友评论

      本文标题:WatchConnectivity相互通信

      本文链接:https://www.haomeiwen.com/subject/tgseyxtx.html