You need to write a privileged tool helper and then implement some function like this
func setNetworkLocation(newLocation : String,reply: @escaping (String) -> Void){
var retVal : Bool = false
var response = ""
var prefs : SCPreferences
prefs = SCPreferencesCreate (nil, "Softech" as CFString, nil)!
var sets : CFArray
sets = SCNetworkSetCopyAll (prefs)!
let count = CFArrayGetCount(sets)
var newSet : SCNetworkSet? = nil
var bFound : Bool = false
for nIndex in 0..<count {
let mySet = CFArrayGetValueAtIndex (sets, nIndex)
let key = Unmanaged<SCNetworkSet>.fromOpaque(mySet!)
newSet = key.takeUnretainedValue()
let name : String = SCNetworkSetGetName(newSet!)! as String
if(name == newLocation){
bFound = true
break
}
print("Name: \(name) cerco \(newLocation)")
}
if(bFound){
print("Trovata netwok location da impostare Name: \(newLocation)")
retVal = SCNetworkSetSetCurrent (newSet!)
print ("SCNetworkSetSetCurrent = \(retVal) ");
retVal = SCPreferencesCommitChanges (prefs);
print ("SCPreferencesCommitChanges = \(retVal) ")
response = "setNetworkLocation: SCPreferencesCommitChanges = [\(retVal)] " + newLocation
}else{
response = "setNetworkLocation: Not found network location named [" + newLocation + "]"
}
reply(response)
}
Usage from application client
func setNetworkLocation(name: String){
let xpcService = prepareXPC()?.remoteObjectProxyWithErrorHandler() { error -> Void in
NSLog("XPC error: \(error)")
} as? RemoteProcessProtocol
xpcService?.setNetworkLocation(newLocation:name , reply: { [self]
responseInfo in
NSLog("\(name): FGLanSelectionHelper setNetworkLocation response => \(responseInfo)")
})
}
@IBAction func btnSetNetworkLocation(_ sender: Any) {
setNetworkLocation(name: "MyOffice")
}