如何使用群组在 iPhone 和 Apple Watch 之间共享数据? [英] How to share data between iPhone and Apple Watch using groups?

查看:43
本文介绍了如何使用群组在 iPhone 和 Apple Watch 之间共享数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Watchkit 开发的新手,无法找到在 iPhone 和 iWatch 之间共享数据的解决方案,请帮助我我希望使用群组共享数据.

i am new to Watchkit development and unable to find solution to share data between iPhone and iWatch, please help me i am looking to share data using groups.

推荐答案

我们可以在 iPhone & 之间传递数据iWatch 使用组.

We can pass the data between iPhone & iWatch using groups.

基本上iWatch不能做任何处理,我们需要共享数据.我们可以使用 NSUserDefaults 共享数据.

Basically iWatch can not do any processing and we need to share the data. We can share data using the NSUserDefaults.

但为此,您需要在项目目标和 iwatch 应用目标的功能部分启用 Appp Groups,如下所示

But for that you need to enable Appp Groups from capabilities section in both your project target and your iwatch app target, as showed below

以下是实现这一目标的示例代码.

Below is the sample code to achieve that.

在您的 viewController 或 appDelegate 文件中添加以下代码

In your viewController or appDelegate file add following code

 NSUserDefaults *myDefaults = [[NSUserDefaults alloc]
                               initWithSuiteName:@"group.test.yourapp"];
[myDefaults setObject:@"aadil" forKey:@"name"];

基本上,您正在为 "name" 变量设置值 "aadil".

Basically you are setting the value "aadil" for "name" variable.

下一步是编写代码来检索,如下所示

Next step is to write code to retrieve that as below

 NSUserDefaults *myDefaults = [[NSUserDefaults alloc]
                               initWithSuiteName:@"group.test.yourapp"];
[myDefaults objectForKey:@"name"];

希望这有帮助:)

这篇关于如何使用群组在 iPhone 和 Apple Watch 之间共享数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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