Sharing UserDefaults between extensions

前端 未结 5 1772
借酒劲吻你
借酒劲吻你 2020-12-02 18:25

Creating a Today widget and I am using UserDefaults(suiteName:) to persist some data. In the main application I am using UserDefaults.standard(). T

相关标签:
5条回答
  • 2020-12-02 18:42

    You cannot use UserDefaults.standard to share data between a host app and its app extension. You instead have to create a shared container with UserDefaults(suiteName:) to share data.

    Even though an app extension bundle is nested within its containing app’s bundle, the running app extension and containing app have no direct access to each other’s containers.

    To enable data sharing, use Xcode or the Developer portal to enable app groups for the containing app and its contained app extensions. Next, register the app group in the portal and specify the app group to use in the containing app.

    After you enable app groups, an app extension and its containing app can both use the NSUserDefaults API to share access to user preferences. To enable this sharing, use the initWithSuiteName: method to instantiate a new NSUserDefaults object, passing in the identifier of the shared group.

    For more, refer to: https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW1

    How to use App Groups: https://github.com/pgpt10/Today-Widget

    Standard or SuiteName?

    Use standard one for data that is only for Host App. Use suiteName for data that you want to share between Extension and Host App. Just don't persist the same data in both of them. Avoid data redundancy. Use both of them according to the context.

    0 讨论(0)
  • 2020-12-02 18:52

    Also make sure you add App Groups to the correct Configuration (Debug, Release). If you add App Groups in Debug for your application target and try to use it in Release config for your extension, then it wont work.

    If you add in Debug config (for app target), then use it in debug config (for extension target)

    0 讨论(0)
  • 2020-12-02 18:53

    PGDev: augmentation:

    you can't avoid redundancy when dragging Settings (preferences) https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/UserDefaults/Preferences/Preferences.html to an extension: they are contained in the app private bundle and have to be replicated into suitenamed bundle to become available to an app extension

    0 讨论(0)
  • 2020-12-02 18:54

    A simple example where I create a shared bundle:

    if let userDefaults = UserDefaults(suiteName: "group.your.bundle.here") {
        userDefaults.set("test 1" as AnyObject, forKey: "key1")
        userDefaults.set("test 2" as AnyObject, forKey: "key2")
        userDefaults.synchronize()
    }
    

    This is how you can read it later:

    if let userDefaults = UserDefaults(suiteName: "group.your.bundle.here") {
        let value1 = userDefaults.string(forKey: "key1")
        let value2 = userDefaults.string(forKey: "key2")
        ...
    }
    
    0 讨论(0)
  • 2020-12-02 19:02

    Make sure App Groups is enabled for ALL of your targets (your app and extensions targets) in Capabilities tab

    And then use the group's identifier above as suite name when create UserDefaults:

    let userDefaults = UserDefaults(suiteName: "group.com.YourCompany.YourApp")
    
    0 讨论(0)
提交回复
热议问题