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

查看:25
本文介绍了在 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)需要符合 #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:error: 方法仅将最近的状态信息传达给对方.当对方醒来时,它可以使用此信息来更新自己的状态并保持同步.使用此方法发送新字典会覆盖之前的字典.

  • 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 会议视频 并阅读 Apple 关于 WatchConnectivity 的文档

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天全站免登陆