无法将数据保存到应用组的共享首选项/用户默认值

时间:2017-06-07 21:08:55

标签: swift macos sharedpreferences nsuserdefaults userdefaults

我需要保存一些应该在两个应用之间共享的设置。为实现此目的,我在两个应用的功能中激活了App Groups,并将应用组设置为TeamIdentifierPrefix.name.appname

将数据保存在用户默认值中的相关代码:

let kListArray = "list"
let defaults = UserDefaults(suiteName: "TeamIdentifierPrefix.name.appname")
let data: [String:Any] = ["str1" : textInput.stringValue,
                          "str2" : button.state,
                          "str3" : ""]
let array = defaults?.array(forKey: kListArray)
var mutableArray = array
mutableArray?.append(data)
defaults?.set(mutableArray, forKey: kListArray)

虽然当我通过Finder查看~/Library/Group Containers/TeamIdentifierPrefix.name.appname/Library/Preferences/TeamIdentifierPrefix.name.appname.plist中的plist时,它仍然是空的。

在Xcode中,在控制台中收到以下警告:

2017-06-07 20:27:10.207748+0200 appname[3106:394220] [User Defaults] Failed to read 
values in CFPrefsPlistSource<0x6080000e2b00> (Domain: 
TeamIdentifierPrefix.name.appname, User: kCFPreferencesAnyUser, ByHost: Yes, 
Container: (null)): Using kCFPreferencesAnyUser with a container is only 
allowed for System Containers, detaching from cfprefsd

0 个答案:

没有答案