Settings Bundle observing in NE targets

Hi, I have been trying to add Settings Bundle to my app that utilizes DNS Proxy and Content Filter, however, I noticed some weird behavior. I worked with Settings Bundle before, but the initial implementation didn't work for some reason.

So, I simplified it to just one toggle switch and tried again.

Initially, I had an observer adding in the init of my SettingsBundleService class and I was using a shared instance in DNS Proxy target, as a result, SettingsBundleService was observing changes but would always return false. But when I tried to use a shared instance from the main target, it worked just fine.

public final class SettingsBundleService {
    static let shared = SettingsBundleService()

    private (set) var test: Bool = false

    init() {
        registerSettingsBundle()
        NotificationCenter.default.addObserver(self, selector: #selector(defaultsChanged), name: UserDefaults.didChangeNotification, object: nil)
        defaultsChanged()
        
        Logger.statistics.log("[SettingsBundleService] – added Settings Bundle observing")
    }
    
    private func registerSettingsBundle(){
        let appDefaults = [String: AnyObject]()
        UserDefaults.standard.register(defaults: appDefaults)
    }
    
    @objc 
    private func defaultsChanged(){
        self.test = UserDefaults.standard.bool(forKey: "enable_feature_preference")
        Logger.statistics.log("[SettingsBundleService] – changed: \(test)")
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
        Logger.statistics.log("[SettingsBundleService] – removed Settings Bundle observing")
    }

Could somebody advise me on what I am doing wrong here?

Answered by DTS Engineer in 788127022

I have been trying to add Settings Bundle to my app that utilizes DNS Proxy and Content Filter

This is not going to work. Again, it’s tied to the fact that your app and your NE proxy are different executables. Each of these executables has its own container. When your Settings bundle changes things, it writes to the the settings in the app’s container. The app will see these writes but your NE providers won’t.

It is possible to share settings between your app and its extensions via an app group [1]. However, there’s no way to configure a Settings bundle to write to the app group settings rather than the app settings.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] By constructing your user defaults with the init(suiteName:) initialiser.

Accepted Answer

I have been trying to add Settings Bundle to my app that utilizes DNS Proxy and Content Filter

This is not going to work. Again, it’s tied to the fact that your app and your NE proxy are different executables. Each of these executables has its own container. When your Settings bundle changes things, it writes to the the settings in the app’s container. The app will see these writes but your NE providers won’t.

It is possible to share settings between your app and its extensions via an app group [1]. However, there’s no way to configure a Settings bundle to write to the app group settings rather than the app settings.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] By constructing your user defaults with the init(suiteName:) initialiser.

I see, thank you a lot. I tried to use user defaults with the init(suiteName:) a couple days ago as I realised it was the same problem as I filed earlier about rules storage access from different targets.

Thank you again!

Settings Bundle observing in NE targets
 
 
Q