2013-03-04 72 views

回答

0

我就是這麼做的:

NSArray* interfaces = (NSArray*) CNCopySupportedInterfaces(); 

    for (NSString* interface in interfaces) 
    { 
     CFDictionaryRef networkDetails = CNCopyCurrentNetworkInfo((CFStringRef) interface); 
     if (networkDetails) 
     { 
      NSLog(@"all details: %@", (NSDictionary *)networkDetails); 
      NSLog(@"BSSID: %@", (NSString *)CFDictionaryGetValue (networkDetails, kCNNetworkInfoKeyBSSID)); 
      BSSID1 = (NSString *)CFDictionaryGetValue (networkDetails, kCNNetworkInfoKeyBSSID); 
      BSSID = [[BSSID1 stringByReplacingOccurrencesOfString:@":" 
                 withString:@""] uppercaseString]; 
      NSLog(@"%@",BSSID); 

      CFRelease(networkDetails); 
     } 
    } 
+0

不幸的是,這不再適用於iOS 9. – DanielG 2015-08-06 23:18:38

+0

看來蘋果已棄用CaptiveNetwork,但隨後遷移到NEHotspotHelper。檢查出來:http://stackoverflow.com/questions/31555640/how-to-get-wifi-ssid-in-ios9-after-captivenetwork-is-depracted-and-calls-for-wif – TimeString 2016-02-02 00:50:54

1

以下回答覆制自answer

在iOS 4.1或更高版本,你可以這樣做:

#import <SystemConfiguration/CaptiveNetwork.h> 

- (id)fetchSSIDInfo 
{ 
    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]; 
    } 
    [ifs release]; 
    return [info autorelease]; 
} 

輸出示例:

2011-03-04 15:32:00.669 ShowSSID[4857:307] -[ShowSSIDAppDelegate fetchSSIDInfo]: Supported interfaces: (
    en0 
) 
2011-03-04 15:32:00.693 ShowSSID[4857:307] -[ShowSSIDAppDelegate fetchSSIDInfo]: en0 => { 
    BSSID = "ca:fe:ca:fe:ca:fe"; 
    SSID = XXXX; 
    SSIDDATA = <>; 
} 

注意,沒有如果支持在模擬器上。在您的設備上測試。

1

1.增加SystemConfiguration.framework

2.import < SystemConfiguration/CaptiveNetwork.h>

3.使用下面的方法

 +(NSString *)currentWifiBSSID { 

      NSString *bssid = nil; 
      NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces(); 
      for (NSString *ifnam in ifs) { 
       NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam); 

       NSLog(@"info:%@",info); 

       if (info[@"BSSID"]) { 
        bssid = info[@"BSSID"]; 
       } 
      } 
      return bssid; 
     } 

希望這會有幫助