UserDefaults between iOS and WatchOS not working

天涯浪子 提交于 2019-12-11 04:47:49

问题


Here is the code, but for some reason I get nil when I read from Watch. Any idea what am I missing?

Writing in iOS Method

@IBAction func writeDefaults(_ sender: Any) {
    UserDefaults(suiteName: "group.testingGroups")!.set(true, forKey: "myKey")
}

Reading from WatchOS

@IBAction func readDefaults() {
    let readKey = UserDefaults(suiteName: "group.testingGroups")!.bool(forKey: "myKey")
    print("Key Value:  \(readKey)")
}

I also tried the following when reading but nothing...

 let readKey = UserDefaults.init(suiteName: "group.testingGroups")!.bool(forKey: "myKey")

Apple Developer Site:

WatchAppExtension:

WatchApp:

iOS Project:


回答1:


Communication between watchKit and app using userDefaults has been removed.
It can be done using watchConnectivity framework



来源:https://stackoverflow.com/questions/41051997/userdefaults-between-ios-and-watchos-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!