问题
I can read from MDM payload managed app configuration below is code to read
func getManagedAppServerUrl() -> String? {
if let managedConf = UserDefaults.standard.object(forKey: "com.apple.configuration.managed") as? [String:Any] {
if let serverURL = managedConf["server_url"] as? String{
return serverURL
}
}
return nil
}
Above code is working as expected.
I wish to get notified whenever changes are made by MDM server to "MDM payload managed app configuration"
i tried to add Userdefault observer on key "com.apple.configuration.managed" code is below to add observer
UserDefaults.standard.addObserver(self, forKeyPath: "com.apple.configuration.managed", options: NSKeyValueObservingOptions.new, context: nil)
Callback method which will get a call once changes are made
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {//key value of mdm configraion change detection
if let keyPathChangeDetect = keyPath {
if keyPathChangeDetect == "com.apple.configuration.managed" {
print("configuration change detected")
}
}
}
But Callback method i.e override func observeValue.... never gets a call when the server makes changes to Managed app configuration.
even if changes are made by the app using timer and changing "com.apple.configuration.managed" Userdefault value doesn't revoke observeValue methods.
回答1:
You should use NSUserDefaultsDidChangeNotification not KVO
来源:https://stackoverflow.com/questions/49809637/ios-mdm-payload-managed-app-configuration-observer-not-working