共享的用户默认
我想通过共享的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是“现代”手表不可能的。
参考:
苹果开发人员论坛
您需要使用另一种传输机制。像 watchConnectivity 。
This is not possible with "modern" WatchOS.
Reference:
Apple developer forum
You would need to use another transfer mechanism. Like Watchconnectivity.