Is there a way to programmatically set the "Location" value in the Network preference pane without using private APIs?

I need to do this in a way that will be accepted by the Mac App Store.

有帮助吗?

解决方案

AFAIK there's no library function that will reliably achieve this, but you could execute the networksetup tool:

/usr/sbin/networksetup -switchtolocation $LOCATION

Alas, I’m not sure if this is allowed for MAS apps. The only way to know for sure is as always just trying.

其他提示

Actually, there are public & available API's that can do what you're looking for.

Take a look at the System Configuration framework. The actual call that I use in my own code that sets a new location (with my users being fully aware that it's happening) is SCNetworkSetSetCurrent.

That said, there's two reasons why you can't do this in your own code:

number 1) as @gcbrueckmann said (and +1 for him for saying so), doing this in a sandboxed app for sale in the Mac App Store is sure to be a no no.

number 2) if you're going to be messing with System Configuration settings that modify system level settings, you need to do it from a privileged (i.e. running as admin or root) tool or app.

scselect will work and does not require admin privileges.

scselect $LOCATION

But you may not be able to use that from a sandbox.

You can also change it via AppleScript, but again, from a sandbox may be problematic.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top