使用watchOS2中的WatchConnectivity在iOS和WatchOS之间发送消息 [英] Send messages between iOS and WatchOS with WatchConnectivity in watchOS2

查看:608
本文介绍了使用watchOS2中的WatchConnectivity在iOS和WatchOS之间发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我观看了WWDC2015并看到您现在可以在手表上开发本机应用程序。这开辟了很多功能,我想知道如何在我的iOS应用程序和AppleWatch应用程序之间发送数据。

I watched the WWDC2015 and saw that you can develop native apps on the watch now. This opened up a lot of capabilities and I am wondering how I could send data between my iOS app and my AppleWatch app.

我看到有一个名为WatchConnectivity的新框架。如何使用此功能以及来回发送数据时的选项是什么?

I saw that there is a new framework called WatchConnectivity. How can I use this and what are my options when sending data back and forth?

推荐答案

WatchConnectivity

首先,两个应该相互通信的类(iOS和watchOS)需要符合< WCSessionDelegate> #import WatchConnectivity 框架

First the two classes that are supposed to communicate with each other (iOS and watchOS) need to conform the <WCSessionDelegate> and #import the WatchConnectivity framework

在您发送数据之前,您需要检查您的设备是否能够发送数据

Before you can send data you need to check if your device is able to send data

if ([WCSession isSupported]) {
      WCSession *session = [WCSession defaultSession];
      session.delegate = self;
      [session activateSession];
      NSLog(@"WCSession is supported");
}

然后,如果您希望使用交互式消息传递(sendMessage API),您将需要先查看其他设备是否可以访问:

Then if you wish to use "interactive messaging" (sendMessage APIs) you will need to see if the other device is reachable first:

if ([[WCSession defaultSession] isReachable]) {
    //Here is where you will send you data
}

后台操作API在您调用WCSession API的时间点,不要求对方设备可以访问。

The "background operations" APIs do not require the counterpart device to be reachable at the point in time you call the WCSession API.

在应用程序之间传输数据时,您有多种选择, Apple文档它们的描述如下:

You have several options when it comes to transferring data between your apps, in the Apple Documentation they are described like this:


  • 使用 updateApplicationContext:错误:仅将最新状态信息传递给对方的方法。当对方醒来时,它可以使用此信息更新自己的状态并保持同步。使用此方法发送新字典会覆盖以前的字典。

  • Use the updateApplicationContext:error: method to communicate only the most recent state information to the counterpart. When the counterpart wakes, it can use this information to update its own state and remain in sync. Sending a new dictionary with this method overwrites the previous dictionary.

使用 sendMessage:replyHandler:errorHandler: sendMessageData:replyHandler:errorHandler:将数据立即传输到对方的方法。当您的iOS应用和WatchKit扩展均处于活动状态时,这些方法可用于即时通信。

Use the sendMessage:replyHandler:errorHandler: or sendMessageData:replyHandler:errorHandler: method to transfer data immediately to the counterpart. These methods are intended for immediate communication when your iOS app and WatchKit extension are both active.

使用 transferUserInfo:在后台传输数据字典的方法。您发送的词典排队等待交付给对方,并在当前应用暂停或终止时继续转移。

Use the transferUserInfo: method to transfer a dictionary of data in the background. The dictionaries you send are queued for delivery to the counterpart and transfers continue when the current app is suspended or terminated.

使用 transferFile :metadata:在后台传输文件的方法。如果要发送多个简单的值字典,请使用此方法。例如,使用此方法发送图像或基于文件的文档。

Use the transferFile:metadata: method to transfer files in the background. Use this method in cases where you want to send more than a simple dictionary of values. For example, use this method to send images or file-based documents.

我将举例说明如何使用应用程序上下文发送/接收数据

I will give you an example how to send/receive data with Application Context

发送数据:

WCSession *session = [WCSession defaultSession];
NSError *error;

[session updateApplicationContext:@{@"firstItem": @"item1", @"secondItem":[NSNumber numberWithInt:2]} error:&error];

接收数据:

- (void) session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext {

    NSLog(@"%@", applicationContext);


    NSString *item1 = [applicationContext objectForKey:@"firstItem"];
    int item2 = [[applicationContext objectForKey:@"secondItem"] intValue];
}

有关WatchConnectivity的更多信息,我真的建议您观看 WWDC2015会话视频并阅读 WatchConnectivity上的Apple文档

For more information about WatchConnectivity I really recommend watching the WWDC2015 session video and reading the Apple Documentation on WatchConnectivity

这篇关于使用watchOS2中的WatchConnectivity在iOS和WatchOS之间发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆