SCDynamicStoreSetValue returns false

366 Views Asked by At

I tried updating the proxy settings of my mac. SCDynamicStoreSetValue: returned false, indicating an unsuccessful update. This is the code I use. What is the correct way?

let ds: SCDynamicStoreRef = SCDynamicStoreCreate(nil, "setProxy" as CFString, nil, nil)!

        let isUpdated = SCDynamicStoreSetValue(ds, "HTTPProxy" as CFStringRef, "111.111.111.1")

        if isUpdated{
            print("updated")
        }else{
            print("not updated")
        }

The question is about why SCDynamicStoreSetValue returns false and how to circumvent it.

2

There are 2 best solutions below

0
breakingobstacles On

After SCDynamicStoreSetValue fails, call SCError() to obtain the error code:

let errorCode = SCError()

Or obtain the error as a string with:

let errorString = String.fromCString(SCErrorString(SCError()))

In either case, review the Status and Error Codes for the System Configuration Framework. That should provide you with the reason that SCDynamicStoreSetValue is returning false.

(If your app is Sandboxed, the likely reason is kSCStatusAccessError, or "Permission Denied". Sandboxed apps can't set those values.)

0
Razvan Axinie On

I know this is an old topic, but the third argument of SCDynamicStoreSetValue should be a CFPropertyListRef (in our case a CFString, not a string), as in the docs

In my case this was causing the function call fail.