【发布时间】:2017-03-28 03:12:10
【问题描述】:
UserDefaults 在我的应用程序中不起作用。请在AppDelegate文件下找到以下代码。
let sharingData = UserDefaults.init(suiteName: "group.macuser79.xxx");
sharingData?.set("vincenzo", forKey:"username");
sharingData?.synchronize();
在要 Watch 的应用程序的 InterfaceController 中,为了能够检索值所以我这样做了:
override func awake(withContext context: Any?) {
let sharingData = UserDefaults.init(suiteName: "group.macuser79.xxx");
let username = sharingData?.object(forKey: "username");
print("Value username \(username)");
}
请告诉我,我做错了什么!
【问题讨论】:
-
替代解决方案:使用 GenericKeychain。 developer.apple.com/library/content/samplecode/GenericKeychain/…
-
首先要看的是
group.macuser79.xxx组在两个目标的项目功能选项卡中定义。 -
@Otávio。您好,我检查了该组是否在目标中定义并已定义。已在 iPhone 的目标中定义,既在目标
Watch 中定义,又在 Watch Extensions 中定义,看起来一切正常。我还更新了配置文件,但遗憾的是什么都没有。 -
嗯,这段代码是我用 Objective-C 编写的代码的直接移植,所以我看不出任何错误。
sharingData对象在这两种情况下都不为零吗?
标签: ios iphone swift3 watchkit userdefaults