iWatch 开发 4: 实现iWatch 与 iPhone 之间数据发送与接收

    xiaoxiao2021-03-25  576

    上一代的iwatch 与iPhone 数据交互使用的是 openParentApplication 函数,在Watch OS2 中摒弃了这种方法,引入了WCSesison 来进行iwatch 与iPhone的数据发送与接收。


    iWatch 端该如何像iphone 发送数据

    在InterfaceController 头文件中, 引入 WatchConnectivity/WatchConnectivity.h, 添加 WCSessionDelegate代理:

    #import <WatchKit/WatchKit.h> #import <Foundation/Foundation.h> #import <WatchConnectivity/WatchConnectivity.h> @interface InterfaceController : WKInterfaceController<WCSessionDelegate>

    在 willActivate 中加入如下代码:

    if([WCSession isSupported]){ WCSession *session = [WCSession defaultSession]; session.delegate = self; [session activateSession]; }

    这是建立一个WCSession的基本方法。

    使用WCSession中的sendMessage发送数据,代码如下:

    - (IBAction)countBtn { WCSession *session = [WCSession defaultSession]; NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"张三",@"Name", nil]; [session sendMessage:dic replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyMessage) { NSLog(@"replay: %@", replyMessage); } errorHandler:^(NSError * _Nonnull error) { NSLog(@"Error: %@", error); }]; }

    这边定义了一个 NSDictionary 的字典作为数据对象,来发送给iPhone。

    由于在头文件中添加了WCSessionDelegate 的代理,所以必须要实现其中的API 函数,其中的didReceiveMessage 就是用于接收iPhone发来的数据:

    - (void)session:(WCSession *)session activationDidCompleteWithState:(WCSessionActivationState)activationState error:(nullable NSError *)error __IOS_AVAILABLE(9.3) __WATCHOS_AVAILABLE(2.2){ } - (void)sessionDidBecomeInactive:(WCSession *)session __IOS_AVAILABLE(9.3) __WATCHOS_UNAVAILABLE{ } - (void)sessionDidDeactivate:(WCSession *)session __IOS_AVAILABLE(9.3) __WATCHOS_UNAVAILABLE{ } - (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *, id> *)message replyHandler:(void(^)(NSDictionary<NSString *, id> *replyMessage))replyHandler{ NSString *name = [message objectForKey:@"Name"]; [_nameLable setText:name]; }

    这样,我们就完成了WatchKit的拓展。


    iOS端处理接收与发送

    同样,在我们的iPhone 端也需要导入 WatchConnectivity/WatchConnectivity.h, 以及添WCSessionDelegate。

    1.在ViewController.h 中导入WatchConnectivity.h,添加WCSessionDelegate。 2.在ViewController.m 中的viewDidLoad 里加入:

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. if([WCSession isSupported]){ WCSession *session = [WCSession defaultSession]; session.delegate = self; [session activateSession]; } }

    3.调用sendMessage 来发送数据给iwatch.

    - (IBAction)sendData:(id)sender { NSString *text = _textField.text; NSLog(@"++++%@++++", text); WCSession *session = [WCSession defaultSession]; NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"李四",@"Name", nil]; [session sendMessage:dic replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyMessage) { NSLog(@"replay: %@", replyMessage); } errorHandler:^(NSError * _Nonnull error) { NSLog(@"Error: %@", error); }]; }

    4.同样跟iWatch一致,需要实现相同的代理方法来接收数据,如果你需要在收到数据后更新UI,请切换到主线程去执行,不然会报如下错误:

    正确的做法如下:

    - (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *, id> *)message replyHandler:(void(^)(NSDictionary<NSString *, id> *replyMessage))replyHandler{ dispatch_async(dispatch_get_main_queue(), ^{ //回调或者说是通知主线程刷新, [_textField setText:[message objectForKey:@"Name"]]; }); NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"李四1111",@"Name", nil]; replyHandler(dic); }

    总结

    通过WCSession, 我们就可以让 iWatch 与 iPhone 进行简单的数据通信,还不赶紧跟上 watch 开发的脚步,来尝试做出一些有意思的小玩意。


    好了。祝大家生活愉快。多多收获友谊和爱情。如果想获取更多的讯息,请扫描下方二维码关注我的微信公众号:

    转载请注明原文地址: https://ju.6miu.com/read-38780.html

    最新回复(0)