iPhone,iPad如何获取WIFI名称即SSID

2010年开始苹果清理了一批APP Store上的WIFI扫描软件, 缘由语焉不详.

这些WIFI扫描软件使用了苹果的私有函数apple80211.framework

尽管不能合法(指能通过App Store的审核)的获取WIFI列表, 不过我们还是可以获取到当前Wifi连接的信息,比如SSID.

SSID全称Service Set IDentifier, 即Wifi网络的公开名称.

苹果在IOS v4.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];
}

对于ARC版本, 代码可简化如下:

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

参考链接:

1. http://stackoverflow.com/questions/5198716/iphone-get-ssid-without-private-library

2. http://answers.yahoo.com/question/index?qid=20100529040141AAKd8dO


by iefreer


你可能感兴趣的:(iPhone,wifi,ipad)