Problem Storing Data (e.g. Preferences) in a Safari App Extension

Hi,


I have a simple legacy Safari Extension that stored some preference data using the Extension API.


Migrating to the App Extension model I face the problem of how to store the settings.


Unfortunately, I cannot find any information on that in the documentation.


I tried to use the UserDefaults API, but it seems, values are not made persistent.


Could somebody point me to some information on this topic or tell me, what the best way (or the intended way) is, to persist some settings in the App extension.


Thanks in advance


Rainer

Accepted Reply

Thanks for that hint.

I defined an App Group in both targets - using explicit group ID (registered in the Development portal) and also using the implicit Team-ID-Prefix. Neither worked for me.


Following the documentation I found, the correct way is to set up an App-ID with the App Service "App Groups" enabled.

But I am only able to do that for iOS App IDs - the OSX-App Id generation does not even mention App Groups.


Maybe I am only missing the point where I can create an OSX-App-ID with App-Groups enabled...

Replies

What do you mean by the fact that values are not made persistent?


Using NSUserDefaults in Safari App Extensions should work correctly. If you look at:


https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html


There is some information about how to share NSUserDefaults between your Safari App Extension and your containing app. If this doesn't help you, can you file a radar explaining when you see your NSUserDefaults wiped with a sample project?


Thanks!

Thanks for the response.


I was looking for a way similar to the former "settings" including the secure settings, including an editor.

My approach now wa to use the UserDefaults in a shared code (shared between containing App and Safari Extension)


While I now see that the information stored by the App Extension is persistent, it cannot be seen in the containing App.

To explain this - I want to use the Hosting App as a settings editor for the persisted values.


The Code for the User-Defaults-Acces looks like this:


class MySettings {
    private let myDefaults : UserDefaults = UserDefaults(suiteName: "com.myAppId")!
    var myVal: String {
        set {
            myDefaults.set(newValue, forKey: "mySettingId")
            myDefaults.synchronize()
        }
        get {
            return myDefaults.string(forKey: "mySettingId") ?? ""
        }
    }
    static let shared = MySettings()
}


I included the code in both Targets (hosting App and Extension)


The problem that I am now facing is that when I try to access the settings in the containing App, I do not receive the stored values...


Regards


Rainer

I haven't tested this myself and can be wrong, but I believe so that this works, bot the extension and its container application need to be in the same application group. Have you set them so in your project?

Thanks for that hint.

I defined an App Group in both targets - using explicit group ID (registered in the Development portal) and also using the implicit Team-ID-Prefix. Neither worked for me.


Following the documentation I found, the correct way is to set up an App-ID with the App Service "App Groups" enabled.

But I am only able to do that for iOS App IDs - the OSX-App Id generation does not even mention App Groups.


Maybe I am only missing the point where I can create an OSX-App-ID with App-Groups enabled...