Removing configured SSIDs programmatically

Before Big Sur our application handled removing configured SSIDs by editing the com.apple.airport.preferences.plist file. Big Sur made this not possible any more so we moved to using terminal commands to accomplish this using the networksetup -removepreferredwirelessnetwork. This functioned without issues on intel machines but requires admin access on the new M1 machines. Is there a reason for this difference?

We also tried providing sudo access using the AuthorizationExecuteWithPrivileges function on the .sh file with the command. Strangely this also does not work and still pops up a "Command requires admin privileges" error. We are aware that AuthorizationExecuteWithPrivileges is a deprecated function, but we have decided to go with it since it's a one off and saves us the hassle of programming with the Authorisation Services on top of the fact that ours is a runtime application. Is this functionality broken on the m1 processor and is there a "correct" way to handle removing SSIDs that does not involve plist files or terminal commands?

Replies

I generally recommend against using a command-line tool as an API when there’s an actual API you can use instead. In this case you can use CoreWLAN to get the interface (CWInterface), then get the interface’s configuration (CWConfiguration), and then get the configuration’s list of profile (CWNetworkProfile). From there you can identify the profile to remove, build a mutable configuration without that profile (CWMutableConfiguration) and commit that new configuration to the interface (commitConfiguration(_:authorization:)).

Share and Enjoy

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

Sorry for opening this up again after all this time.

Remove SSID works very smoothly with your suggestion. I was wondering if there is a similar thing that could be done to add SSIDs as well? I see the CWMutableNetworkProfile exists that can handle a lot of the WLAN specific fields. I am basically looking to replace the current setup where I create a mobileconfig file that is pushed using terminal commands.

My question is - What is the best way to create a CWMutableNetworkProfile object and add it to a CWMutableConfiguration so that I can commitConfiguration? Attempted Using the same concept as remove SSID and added my MutableNetworkProfile to an NSArray along with the existing profiles - I then set the MutableNetworkProfile to this NSArray and try and commit that.

Unfortunately by the end of this most probably long-winded way of doing things, I do not see a network being added in my preferences.

I was wondering if there is a similar thing that could be done to add SSIDs as well?

You can definitely use Core WLAN to add preferred Wi-Fi networks.

to an NSArray along with the existing profiles

An array? The networkProfiles property of CWMutableConfiguration is meant to be an ordered set. The normal approach is to get this property, make a mutable copy, add your profile, and then set it back.

Share and Enjoy

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