iOS获取WiFi的Mac地址

获取连接WiFi的MacAddress

SystemConfiguration.framework里面有CaptiveNetwork类,

/*!
 @function CNCopyCurrentNetworkInfo
 @discussion Returns the Network Info for the specified interface.
    For example, Network Info dictionary will contain the following
    keys, and values:
    <pre>
    @textblock
    Keys                      : Values
    =======================================
    kCNNetworkInfoKeySSIDData : CFDataRef
    kCNNetworkInfoKeySSID     : CFStringRef
    kCNNetworkInfoKeyBSSID    : CFStringRef
    @/textblock
    </pre>
 @param interfaceName Name of the interface you are interested in
 @result Network Info dictionary associated with the interface.
     Returns NULL if an error was encountered.
     You MUST release the returned value.
 */
CFDictionaryRef __nullable
CNCopyCurrentNetworkInfo    (CFStringRef interfaceName) __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_1);

通过如下方法获取wifi名称和wifi macAddress,ssid代表wifi名称,bssid表示wifi macAddress。

+(NSString *)MacAddress
{
    NSArray *ifs = CFBridgingRelease(CNCopySupportedInterfaces());
    id info = nil;
    for (NSString *ifnam in ifs) {
        info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam);
        if (info && [info count]) {
            break;
        }
    }
    NSDictionary *dic = (NSDictionary *)info;
    NSString *ssid = [[dic objectForKey:@"SSID"] lowercaseString];
    NSString *bssid = [dic objectForKey:@"BSSID"];
    NSLog(@"ssid:%@ \nssid:%@",ssid,bssid);
    return bssid;
}  
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容