iOS 和 WatchOS 之间的 UserDefaults 不起作用 [英] UserDefaults between iOS and WatchOS not working

查看:60
本文介绍了iOS 和 WatchOS 之间的 UserDefaults 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码,但由于某种原因,当我从 Watch 读取时,我得到 nil.知道我错过了什么吗?

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

用 iOS 方法编写

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

从 WatchOS 读取

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 开发者网站:

推荐答案

watchKit 和应用之间使用 userDefaults 的通信已被移除.
可以使用 watchConnectivity 框架

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

这篇关于iOS 和 WatchOS 之间的 UserDefaults 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆