如何使用群组在 iPhone 和 Apple Watch 之间共享数据?
How to share data between iPhone and Apple Watch using groups?
我是 Watchkit 开发的新手,无法找到在 iPhone 和 iWatch 之间共享数据的解决方案,请帮助我
我希望使用群组共享数据。
我们可以使用组在 iPhone 和 iWatch 之间传递数据。
基本上iWatch不能做任何处理,我们需要共享数据。我们可以使用 NSUserDefaults
.
共享数据
但是为此,您需要在您的项目目标和 iwatch 应用程序目标中的功能部分启用 Appp Groups
,如下所示
下面是实现该功能的示例代码。
在您的 viewController or appDelegate
文件中添加以下代码
NSUserDefaults *myDefaults = [[NSUserDefaults alloc]
initWithSuiteName:@"group.test.yourapp"];
[myDefaults setObject:@"aadil" forKey:@"name"];
基本上,您正在为 "name"
变量设置值 "aadil"
。
下一步是编写代码来检索它,如下所示
NSUserDefaults *myDefaults = [[NSUserDefaults alloc]
initWithSuiteName:@"group.test.yourapp"];
[myDefaults objectForKey:@"name"];
希望这对您有所帮助 :)
警告! 它不再适用于 WatchOS 2,请使用 WatchConnectivity Framework 中的方法。最好的是 updateApplicationContext: 它总是保持最新的数据。来自 Apple 文档:
Watch apps that shared data with their iOS apps using a shared group
container must be redesigned to handle data differently. In watchOS 2,
each process must manage its own copy of any shared data in the local
container directory. For data that is actually shared and updated by
both apps, this requires using the Watch Connectivity framework to
move that data between them.
我是 Watchkit 开发的新手,无法找到在 iPhone 和 iWatch 之间共享数据的解决方案,请帮助我 我希望使用群组共享数据。
我们可以使用组在 iPhone 和 iWatch 之间传递数据。
基本上iWatch不能做任何处理,我们需要共享数据。我们可以使用 NSUserDefaults
.
但是为此,您需要在您的项目目标和 iwatch 应用程序目标中的功能部分启用 Appp Groups
,如下所示
下面是实现该功能的示例代码。
在您的 viewController or appDelegate
文件中添加以下代码
NSUserDefaults *myDefaults = [[NSUserDefaults alloc]
initWithSuiteName:@"group.test.yourapp"];
[myDefaults setObject:@"aadil" forKey:@"name"];
基本上,您正在为 "name"
变量设置值 "aadil"
。
下一步是编写代码来检索它,如下所示
NSUserDefaults *myDefaults = [[NSUserDefaults alloc]
initWithSuiteName:@"group.test.yourapp"];
[myDefaults objectForKey:@"name"];
希望这对您有所帮助 :)
警告! 它不再适用于 WatchOS 2,请使用 WatchConnectivity Framework 中的方法。最好的是 updateApplicationContext: 它总是保持最新的数据。来自 Apple 文档:
Watch apps that shared data with their iOS apps using a shared group container must be redesigned to handle data differently. In watchOS 2, each process must manage its own copy of any shared data in the local container directory. For data that is actually shared and updated by both apps, this requires using the Watch Connectivity framework to move that data between them.