macos - 我们如何在IOS项目中以编程方式获取网络服务订单

标签 macos system-configuration

我的代码是我尝试使用 SCNetworkSetGetServiceOrder 来获取网络服务订单。像下面这样

Ethernet
FireWire
Wi-Fi
Bluetooth PAN
Thunderbolt Bridge

我正在使用下面的代码

    -(void)getserviceorder
    {
           CFArrayRef          interfaces;
            CFIndex            interfaceCount;
            CFIndex            interfaceIndex;
            CFMutableArrayRef  result;
            CFArrayRef *portArray;
            portArray = NULL;
            result = NULL;
            interfaces = NULL;
                if (SCNetworkInterfaceCopyAll != NULL) {
                interfaces = SCNetworkInterfaceCopyAll();
                interfaceCount = CFArrayGetCount(interfaces);

            for (interfaceIndex = 0; interfaceIndex < interfaceCount;
                 interfaceIndex++) {
                SCNetworkInterfaceRef  thisInterface;
                SCNetworkSetRef set =         CFArrayGetValueAtIndex(interfaces,interfaceIndex);
                assert(thisInterface != NULL);

               NSLog(@"SCNetworkSetGetServiceOrder is %@",SCNetworkSetGetServiceOrder(set));

            }
                CFRelease(interfaces);
}

但它总是返回 null。这是正确的方法还是我如何使用 SCNetworkSetGetServiceOrder 方法来获取服务订单。 (SCNetworkSetRef set) 的值应该是多少,它是 SCNetworkSetGetServiceOrder 方法的参数。我在哪里可以了解更多相关信息

最佳答案

例如,像这样(在 Swift 中):

import SystemConfiguration

let scpref = SCPreferencesCreate(kCFAllocatorDefault, "iflist" as CFString, nil)

let netset = SCNetworkSetCopyCurrent(scpref!)
let netservs = SCNetworkSetGetServiceOrder(netset!)! as NSArray

for id in netservs {
    print("id \(id)")
    let serv = SCNetworkServiceCopy(scpref!, id as! CFString)!
    if SCNetworkServiceGetEnabled(serv) {
        if let interface = SCNetworkServiceGetInterface(serv) {
            let serviceName = SCNetworkServiceGetName(serv)
            print(serviceName as! String)
        }
    }
}

关于macos - 我们如何在IOS项目中以编程方式获取网络服务订单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38609889/

相关文章:

macos - 鼠标空闲时鼠标悬停

xcode - CFURLCopyResourcePropertyForKey 失败,因为它传递了这个没有方案 : 的 URL

java - 无法创建 AVD - Eclipse、MAC OSX、Android 4.4.2

cocoa - 如何将 NSImage 保存为新文件

python - 如何使用 pip 在 mac OS 上安装 mecab-python3

magento - 如何在Magento系统配置中添加所见即所得编辑器?

python - 在没有 yum 的情况下卸载 python 2.6