【问题标题】:UserDefaults between iOS and WatchOS not workingiOS 和 WatchOS 之间的 UserDefaults 不起作用
【发布时间】:2016-12-09 02:02:41
【问题描述】:

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

iOS 写法

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

从 WatchOS 读取

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

阅读时我也尝试了以下但没有...

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

苹果开发者网站:

WatchAppExtension:

WatchApp:

iOS 项目:

【问题讨论】:

    标签: swift3 nsuserdefaults watchkit


    【解决方案1】:

    watchKit 和应用程序之间使用 userDefaults 的通信已被删除。
    可以使用watchConnectivity framework

    【讨论】:

    • 哦,好吧,我认为手表连接框架适用于大量数据,并且 userdefaults 仍然处于活动状态。 Apple 是否有任何通知表明您已将其删除?谢谢
    • 是的,它已在 watchOS 2 中被删除,因为 watchOS 2 应用程序在手表而不是 iPhone 上运行。
    • 好的,我会调查的。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多