简体   繁体   English

为什么NSUserDefaults不能在我的应用和共享扩展程序之间工作?

[英]Why doesn't NSUserDefaults work between my app & share extension?

I have an iOS app with a share extension. 我有一个分享扩展的iOS应用程序。 I am trying to share data between them using NSUserDefaults and App Groups but, while I can write into the NSUD object, read it, and synchronize() without error, reading in the extension always results in nil . 我正在尝试使用NSUserDefaults和App Groups在它们之间共享数据,但是,虽然我可以写入NSUD对象,读取它,并且synchronize()没有错误,读取扩展名总是导致nil

I have an app group, the literal string "group.net.foo.bar" for which both the app & extension have configured under Capabilities -> App Groups. 我有一个应用程序组,文字字符串“group.net.foo.bar”,应用程序和扩展程序都在功能 - >应用程序组下配置。 This string is in a constants struct in my app: 这个字符串在我的app中的constants结构中:

struct Forum {
    static let APP_GROUP = "group.net.foo.bar"
    static let AUTH_KEY = "AUTH_KEY"
}

In the main app I create a UserDefaults object and write to it: 在主应用程序中,我创建一个UserDefaults对象并写入它:

fileprivate lazy var userDefaults: UserDefaults = {
    let defaults = UserDefaults()
    defaults.addSuite(named: Forum.APP_GROUP)
    return defaults
}()

// later
userDefaults.set(apiKey, forKey: Forum.AUTH_KEY)
userDefaults.synchronize()

Creating a new NSUD object after that synchronize() and retrieving the AUTH_KEY works. synchronize()和检索AUTH_KEY之后创建新的NSUD对象。 In the extension, I create an NSUD and try to retrieve the value, to no avail: 在扩展中,我创建了一个NSUD并尝试检索该值,但无济于事:

private lazy var userDefaults: UserDefaults = {
    let defaults = UserDefaults()
    defaults.addSuite(named: Forum.APP_GROUP)
    return defaults
}()

// later
private func getApiKey() -> String? {
    return userDefaults.string(forKey: Forum.AUTH_KEY)
}
// returns nil

In all of my reading of the Apple docs and depressingly-similar questions here on Stack Overflow I can't divine what I've done incorrectly. 在我所有关于Apple文档和Stack Overflow中令人沮丧的类似问题的阅读中,我无法理解我做错了什么。

Xcode Version 8.0 (8A218a), also tested with Xcode 8.1 Beta 2. Same behavior on simulator andy my iPhone 6s running iOS 10. Xcode版本8.0(8A218a),也使用Xcode 8.1 Beta 2进行了测试。模拟器和运行iOS 10的iPhone 6s上的行为相同。

Not sure if defaults.addSuite(named: ...) does the same as UserDefaults(suiteName: ...) . 不确定defaults.addSuite(named: ...)是否与UserDefaults(suiteName: ...) In my app I use appGroups this way and it works as expected: 在我的应用程序中,我以这种方式使用appGroups,它按预期工作:

// write
if let userDefaults = UserDefaults(suiteName: appGroupName) {
    userDefaults.set("---" as AnyObject, forKey: "distance")
    userDefaults.set("---" as AnyObject, forKey: "altitude")
    ...
    userDefaults.synchronize()
}

// read
if let userDefaults = UserDefaults(suiteName: appGroupName) {
    self.distanceLabel.text = userDefaults.string(forKey: "distance")
    self.altitudeLabel.text = userDefaults.string(forKey: "altitude")
}

if you suffer this problem when you try to save data to extension APP by using userDefault,maybe you had written this code : [[NSUserDefaults standardUserDefaults ] initWithSuiteName:@"group.xxx.com"];,this code reset default userDefault. 如果您在尝试使用userDefault将数据保存到扩展APP时遇到此问题,可能您已编写此代码:[[NSUserDefaults standardUserDefaults ] initWithSuiteName:@“group.xxx.com”] ;,此代码重置默认userDefault。 Actually,the correct code is : [[NSUserDefaults alloc ] initWithSuiteName:@"group.xxx.com"]; 实际上,正确的代码是:[[NSUserDefaults alloc ] initWithSuiteName:@“group.xxx.com”]; enter link description here 在此输入链接描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM