iOS MDM payload managed app configuration, observer not working

China☆狼群 提交于 2019-12-13 02:59:57

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!