尝试使用应用程序组和用户默认值在Apple Watch和iOS应用程序之间同步数据
我想创建一个简单的iOS应用程序,与手表应用程序同步(并共享一些数据)。我尝试使用AppGroup和UserDefault来实现这一点。iOS应用程序保存数据,但Apple Watch应用程序无法读取数据。有人知道原因吗?是因为我的免费发展账户吗 iOS代码:尝试使用应用程序组和用户默认值在Apple Watch和iOS应用程序之间同步数据,ios,synchronization,watch,ios-app-group,Ios,Synchronization,Watch,Ios App Group,我想创建一个简单的iOS应用程序,与手表应用程序同步(并共享一些数据)。我尝试使用AppGroup和UserDefault来实现这一点。iOS应用程序保存数据,但Apple Watch应用程序无法读取数据。有人知道原因吗?是因为我的免费发展账户吗 iOS代码: @IBAction func buttonPressed(_ sender: Any) { print(FileManager.default.containerURL(forSecurityApplicationGrou
@IBAction func buttonPressed(_ sender: Any) {
print(FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.my.app")!
.appendingPathComponent("switchState.plist"))
var sharedDefault = UserDefaults(suiteName: "group.com.my.app")
sharedDefault?.set(textFieldOne.text, forKey: "shared1")
print(UserDefaults(suiteName: "group.com.my.app")!.string(forKey: "shared1")!)
sharedDefault?.synchronize()
}
iWatch代码:
@IBAction func buttonPressed() {
let sharedDefault = UserDefaults(suiteName: "group.com.my.app")
print(FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.my.app")!
.appendingPathComponent("switchState.plist"))
print(UserDefaults(suiteName: "group.com.my.app")?.object(forKey: "shared1"))
let sharedText = sharedDefault!.string(forKey: "shared1")
textField.setText(sharedText)
print(sharedText)
}
当我运行iOS应用程序时,它会写入switchState.p,并在路径中列出一个具有值的键:
/用户/_用户名/库/开发者/核心模拟器/设备/F5A0485A-CBB7-4903-8339-738DBE3491B6/data/Containers/Shared/AppGroup/5C371E82-27A7-4BB3-982D-B0E5CDE5AC52/Library/Preferences/switchState.plist
但当我尝试在iWatch应用程序中读取数据时-
/用户/用户名/库/开发者/核心模拟器/设备/6F195AF7-73CA-4AF9-8CFE-5B47286F3DE5/data/Containers/Shared/AppGroup/533FEFB3-4A77-4A2B-8A53-99CF916D5481/
没有plist文件(当然返回nil)
但是!当我在第二个路径中手动添加Library/Preferences/switchState.plist时,它工作得非常好
我已经在真实设备上尝试过了-iWatch应用程序说:
file:///private/var/mobile/Containers/Shared/AppGroup/D297CDB8-F623-4BDF-83D8-15505451EC92/switchState.plist
2020-03-04 10:59:25.736335+0200 IOSWACHOSTEST扩展[288:94115][用户默认值]无法读取CFPrefsPlistSource(域:group.com.sk.todoey-ios13.todoey,用户:kCFPreferencesAnyUser,按主机:是,容器:(null),内容需要刷新:是):仅允许将kcfprefspreferencesanyuser与容器一起使用,从CFSD分离
你能帮我吗?你不能使用应用程序组在iOS应用程序和watch应用程序之间共享数据,因为它们不共享文件系统。你需要使用感谢你的回答,并指出正确的方向!我会试着找出这个话题。此外,我还发现了这篇文章——它看起来不像apple docs那么可怕:)你不能使用应用程序组在你的iOS应用程序和你的watch应用程序之间共享数据,因为它们不共享文件系统。你需要使用感谢你的回答,并指出正确的方向!我会试着找出这个话题。此外,我还发现了这篇文章——它看起来没有苹果文档那么可怕:)