如何获取当前的网络位置名称?

Rav*_*ash 2 macos networking cocoa system-configuration

在系统网络首选项中有一些位置名称.如何获取当前或活动的网络位置名称和所有网络位置的列表?
我猜SystemConfiguration.framework支持这个,但我没有准确地使用哪个API.

提前感谢您的回答.

关心
Devara Gudda

out*_*tis 6

您可以使用SCPreferencesCreate获取首选项,然后SCNetworkSetCopyAll获取网络位置.SCNetworkSetGetName将获得一个位置的名称.

SCPreferencesRef prefs = SCPreferencesCreate(NULL, @"SystemConfiguration", NULL);
NSArray *locations = (NSArray *)SCNetworkSetCopyAll(prefs);
for (id item in locations) {
    NSString *name = (NSString *)SCNetworkSetGetName((SCNetworkSetRef)item);
    ...
}
CFRelease(locations);
CFRelease(prefs);
Run Code Online (Sandbox Code Playgroud)

阅读" 系统配置编程指南 "了解更多信息.