共享的用户默认

发布于 2025-01-22 23:19:49 字数 977 浏览 0 评论 0原文

我想通过共享的UserDefaults(与AppGroup)共享iPhone和Apple Watch之间的数据,但是这两个之间的通信不起作用。当我查看共享容器中的实际存储文件时,我发现它们不使用相同的容器。有人可以帮我吗?

手表上的代码:

let suiteName = "group.suitname"
if let userDefaults = UserDefaults(suiteName: suiteName) {
   userDefaults.set(inputText, forKey: "watchTransfer")
   let groupPath = FileManager().containerURL(forSecurityApplicationGroupIdentifier: suiteName)!.appendingPathComponent("Library/Preferences")
   print(groupPath)
   print(userDefaults.string(forKey: "watchTransfer") ?? "Nothing stored")
}

iPhone上的代码:

let suiteName = "group.suitname"
if let userDefaults = UserDefaults(suiteName: suiteName) {
   let string = userDefaults.string(forKey: "watchTransfer") ?? "Nothing stored"
   print("Fetched: \(string)")
   let groupPath = FileManager().containerURL(forSecurityApplicationGroupIdentifier: suiteName)!.appendingPathComponent("Library/Preferences")
   print(groupPath)
   return string
}

I want to share data between iPhone and Apple Watch with shared UserDefaults (with AppGroup), but the communication between these two doesn't work. When I look at actual stored files in the shared Container, I found out that they do not use the same container. Can someone help me with this?

Code on watch:

let suiteName = "group.suitname"
if let userDefaults = UserDefaults(suiteName: suiteName) {
   userDefaults.set(inputText, forKey: "watchTransfer")
   let groupPath = FileManager().containerURL(forSecurityApplicationGroupIdentifier: suiteName)!.appendingPathComponent("Library/Preferences")
   print(groupPath)
   print(userDefaults.string(forKey: "watchTransfer") ?? "Nothing stored")
}

Code on iPhone:

let suiteName = "group.suitname"
if let userDefaults = UserDefaults(suiteName: suiteName) {
   let string = userDefaults.string(forKey: "watchTransfer") ?? "Nothing stored"
   print("Fetched: \(string)")
   let groupPath = FileManager().containerURL(forSecurityApplicationGroupIdentifier: suiteName)!.appendingPathComponent("Library/Preferences")
   print(groupPath)
   return string
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ヅ她的身影、若隐若现 2025-01-29 23:19:49

这是“现代”手表不可能的。

参考:

苹果开发人员论坛

您需要使用另一种传输机制。像 watchConnectivity

This is not possible with "modern" WatchOS.

Reference:

Apple developer forum

You would need to use another transfer mechanism. Like Watchconnectivity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文