美文网首页
iWatch与iphone之间的数据交互

iWatch与iphone之间的数据交互

作者: Treey_Ahao | 来源:发表于2018-07-02 10:34 被阅读0次

在watch OS2中使用的是WCSession来进行数据的接收与发送。
iwatch端:
在InterfaceController头文件中导入WatchConnectivity/WatchConnectivity.h,并添加WCSessionDelegate代理:

#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>
#import <WatchConnectivity/WatchConnectivity.h>

@interface InterfaceController : WKInterfaceController <WCSessionDelegate>

在.m文件的- (void)willActivate中添加代码:

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

建立一个WCSession。

使用WCSession中的sendMessage发送数据:

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

通过定义一个NSDictionary作为数据对象,发送给iphone。
接收数据是通过WCSessionDelegate的代理实现:

#pragma mark WCSessionDelegate

- (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 *str = [message objectForKey:@"Message"];
    [_nameLabel setText:str];
}

iOS端:
同样,在iphone端也需要导入WatchConnectivity/WatchConnectivity.h,并添加WCSessionDelegate代理。
在ViewController中导入WatchConnectivity.h,添加WCSessionDelegate。

#import <WatchConnectivity/WatchConnectivity.h>

@interface ViewController ()<WCSessionDelegate>

在ViewController.m的viewDidLoad中添加代码:

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

同样调用sendMessage方法发送数据给iwatch

- (IBAction)tap:(UIButton *)sender {
    
    NSString *text = _textField.text;
    NSLog(@"++++%@++++", text);
    
    [self sendText:_textField.text];
}


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

和iwatch一样,需要实现相同的代理方法来接收数据,如果收到数据后需要更新UI,必须切换到主线程执行,否则会报错。

#pragma mark delegate
- (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:@"Message"]];
       
    });
    
    NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:_textField.text,@"Message", nil];
    
    replyHandler(dic);
    
    
}

相关文章

网友评论

      本文标题:iWatch与iphone之间的数据交互

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