2016-07-27 54 views
1

我的代碼我試圖使用SCNetworkSetGetServiceOrder獲取網絡服務訂單。像下面我們如何在OSX項目中以編程方式獲得網絡服務訂單

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方法的參數。我在哪裏可以瞭解更多有關這

回答

1

例如,像這樣(斯威夫特):

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) 
     } 
    } 
} 
+0

非常好......關於第一個重新排序的網絡設置,如無線網絡後以太網等多種網絡卡的任何想法?嘗試使用'SCNetworkSetSetServiceOrder(netset!,newSet)',但沒有運氣 –

相關問題