xcode 无法使用应用程序组访问 NSUserDefaults
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33834002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot access NSUserDefaults using app groups one to another
提问by do it better
I'm working on an app and a widget that the widget needs to get data from app. I've used the following codes to read and write on NSUserDefaults. And also I used $(PRODUCT_BUNDLE_IDENTIFIER).widget
for widget and $(PRODUCT_BUNDLE_IDENTIFIER)
referring to thispost. But widget cannot get the data from app or NSUserDefaults. How can I make it work?
我正在开发一个应用程序和一个小部件,小部件需要从应用程序获取数据。我使用以下代码在 NSUserDefaults 上读写。我也用于$(PRODUCT_BUNDLE_IDENTIFIER).widget
小部件并$(PRODUCT_BUNDLE_IDENTIFIER)
参考这篇文章。但是小部件无法从应用程序或 NSUserDefaults 获取数据。我怎样才能让它工作?
func addTask(name: String?) {
let key = "keyString"
tasks.append(name!)
let defaults = NSUserDefaults(suiteName: "group.Mins")
defaults?.setObject(tasks, forKey: key)
defaults?.synchronize()
}
///////
///////
let defaults = NSUserDefaults(suiteName: "group.Mins")
let key = "keyString"
if let testArray : AnyObject = defaults?.objectForKey(key) {
let readArray : [String] = testArray as! [String]
timeTable = readArray
timeTable = timeTable.sort(<)
print("GOT IT")
print("timetable: \(timeTable)")
}
回答by Rashwan L
To read and save from the same set of NSUserDefaults you need to the the following:
要从同一组 NSUserDefaults 读取和保存,您需要执行以下操作:
- In your main app, select your project in the project navigator.
- Select your main app target and choose the capabilities tab.
- Switch on App Groups (this will communicate with the developer portal, as it is generating a set of entitlements, and relevant App Id and so forth).
- Create a new container. According to the help, it must start with “group.”, so give it a name like “group.myapp.test”.
- Select your Today Extension target and repeat this process of switching on app groups. Don't create a new one, rather select this newly created group to signify that the Today Extension is a member of the group.
- 在您的主应用程序中,在项目导航器中选择您的项目。
- 选择您的主要应用程序目标并选择功能选项卡。
- 打开应用程序组(这将与开发人员门户通信,因为它正在生成一组权利和相关的应用程序 ID 等)。
- 创建一个新容器。根据帮助,它必须以“group.”开头,因此将其命名为“group.myapp.test”。
- 选择您的 Today Extension 目标并重复这个打开应用程序组的过程。不要创建一个新的,而是选择这个新创建的组来表示 Today Extension 是该组的成员。
Write to your NSUserDefaults:
写入您的 NSUserDefaults:
// In this example I′m setting FirstLaunch value to true
NSUserDefaults(suiteName: "group.myapp.test")!.setBool(true, forKey: "FirstLaunch")
Read from NSUserDefaults:
从 NSUserDefaults 读取:
// Getting the value from FirstLaunch
let firstLaunch = NSUserDefaults(suiteName: "group.myapp.test")!.boolForKey("FirstLaunch")
if !firstLaunch {
...
}
Swift 4.x:
斯威夫特 4.x:
Write:
写:
UserDefaults(suiteName: "group.myapp.test")!.set(true, forKey: "FirstLaunch")
Read:
读:
UserDefaults(suiteName: "group.myapp.test")!.bool(forKey: "FirstLaunch")