2012-11-06 47 views
2

我使用下面的代碼來檢索iPod連接的WiFi網絡的SSID。使用CNCopyCurrentNetworkInfo的問題

NSArray *ifs = (id)CNCopySupportedInterfaces(); 
NSLog(@"%s: Supported interfaces: %@", __func__, ifs); 
id info = nil; 
for (NSString *ifnam in ifs) { 
    info = (id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam); 
    NSLog(@"%s: %@ => %@", __func__, ifnam, info); 
    if (info && [info count]) { 
     break; 
    } 
    [info release]; 
} 

有時這個代碼不返回網絡我的設備是爲什麼SSID沒有正確檢索connected.Any指針的正確SSID? CNCopyCurrentNetworkInfo包是否依賴於設備的iOS版本?

謝謝。

+0

我在iOS 5.1設備上運行的代碼。 – Cooldude

+0

這裏嘗試的解決方案: http://stackoverflow.com/questions/4712535/how-do-i-use-captivenetwork-to-get-the-current-wifi-hotspot-name –

+0

嘗試的解決方案,在這裏: http://stackoverflow.com/questions/4712535/how-do-i-use-captivenetwork-to-get-the-current-wifi-hotspot-name –

回答

4
  1. 添加SystemConfiguration.framework項目。

  2. import <SystemConfiguration/CaptiveNetwork.h>

  3. CFArrayRef myArray = CNCopySupportedInterfaces(); 
    CFStringRef interfaceName = CFArrayGetValueAtIndex(myArray, 0); 
    CFDictionaryRef captiveNtwrkDict = CNCopyCurrentNetworkInfo(interfaceName); 
    NSDictionary *dict = (NSDictionary*) captiveNtwrkDict; 
    NSString* ssid = [dict objectForKey:@"SSID"]; 
    NSLog(@"%s ssid : %@",__FUNCTION__, [ssid description]); 
    
+0

不確定誰投下了這個,投票了再次。 –