zoukankan      html  css  js  c++  java
  • iPhone,iPad如何获取WIFI名称即SSID

    本文转载至 http://blog.csdn.net/wbw1985/article/details/20530281 
    2010年开始苹果清理了一批APP Store上的WIFI扫描软件, 缘由语焉不详.

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

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

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

    苹果在IOS v4.1+版本上提供了公开的方法来获取该信息.

    示范代码如下:

    [plain] view plaincopy
     
    1. #import <SystemConfiguration/CaptiveNetwork.h>  
    2.   
    3. - (id)fetchSSIDInfo  
    4. {  
    5.     NSArray *ifs = (id)CNCopySupportedInterfaces();  
    6.     NSLog(@"%s: Supported interfaces: %@", __func__, ifs);  
    7.     id info = nil;  
    8.     for (NSString *ifnam in ifs) {  
    9.         info = (id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam);  
    10.         NSLog(@"%s: %@ => %@", __func__, ifnam, info);  
    11.         if (info && [info count]) {  
    12.             break;  
    13.         }  
    14.         [info release];  
    15.     }  
    16.     [ifs release];  
    17.     return [info autorelease];  
    18. }  

    对于ARC版本, 代码可简化如下:
    [plain] view plaincopy
     
    1. - (id)fetchSSIDInfo {  
    2.      NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();  
    3.      NSLog(@"Supported interfaces: %@", ifs);  
    4.      id info = nil;  
    5.      for (NSString *ifnam in ifs) {  
    6.          info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);  
    7.          NSLog(@"%@ => %@", ifnam, info);  
    8.          if (info && [info count]) { break; }  
    9.      }  
    10.      return info;  
    11. }  

    参考链接:

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

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

    SSID全称Service Set IDentifier, 即Wifi网络的公开名称.在IOS 4.1以上版本提供了公开的方法来获取该信息.

    [html] view plaincopy
     
     
    1. #import <SystemConfiguration/CaptiveNetwork.h>  

    [html] view plaincopy
     
     
    1. -(id)fetchSSIDInfo  
    2. {  
    3.     NSArray *ifs = (id)CNCopySupportedInterfaces();  
    4.     NSLog(@"%s: Supported interfaces: %@", __func__, ifs);  
    5.     id info = nil;  
    6.     for (NSString *ifnam in ifs) {  
    7.         info = (id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam);  
    8.         if (info && [info count]) {  
    9.             break;  
    10.         }  
    11.         [info release];  
    12.     }  
    13.     [ifs release];  
    14.     return [info autorelease];  
    15. }  
    16.   
    17. - (NSString *)currentWifiSSID {  
    18.     // Does not work on the simulator.  
    19.     NSString *ssid = nil;  
    20.     NSArray *ifs = (  id)CNCopySupportedInterfaces();  
    21.     NSLog(@"ifs:%@",ifs);  
    22.     for (NSString *ifnam in ifs) {  
    23.         NSDictionary *info = (id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam);  
    24.         NSLog(@"dici:%@",[info  allKeys]);  
    25.         if (info[@"SSIDD"]) {  
    26.             ssid = info[@"SSID"];  
    27.               
    28.         }  
    29.     }  
    30.     return ssid;  
    31. }  
    32.   
    33. - (void)viewDidLoad  
    34. {  
    35.     [super viewDidLoad];  
    36.       
    37.     tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(50, 40, 200, 40)];  
    38.     tempLabel.textAlignment=NSTextAlignmentCenter;  
    39.     [self.view addSubview:tempLabel];  
    40.     NSDictionary *ifs = [self fetchSSIDInfo];  
    41.     NSString *ssid = [[ifs objectForKey:@"SSID"] lowercaseString];  
    42.     tempLabel.text=ssid;  
    43.    
    44. }  

    log 信息 :

    [html] view plaincopy
     
     
    1. 2013-06-05 21:39:14.357 wifiNameDemo[9877:707] dici:{  
    2.     BSSID = "f4:ec:38:40:cc:e8";  
    3.     SSID = "Nice_Apple";  
    4.     SSIDDATA = <4e696365 5f417070 6c65>;  
    5. }  
    6. 2013-06-05 21:39:14.360 wifiNameDemo[9877:707] Nice_Apple  

     ARC 版本:
    [html] view plaincopy
     
     
    1. - (id)fetchSSIDInfo {  
    2.     NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();  
    3.     NSLog(@"Supported interfaces: %@", ifs);  
    4.     id info = nil;  
    5.     for (NSString *ifnam in ifs) {  
    6.         info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);  
    7.         NSLog(@"%@ => %@", ifnam, info);  
    8.         if (info && [info count]) { break; }  
    9.     }  
    10.     return info;  
    11. }  

      效果如下:

  • 相关阅读:
    LeetCode: Longest Valid Parentheses 解题报告
    LeetCode: Generate Parentheses 解题报告
    Leetcode: LRU Cache 解题报告
    LeetCode: Maximal Rectangle 解题报告
    LeetCode: Min Stack 解题报告
    LeetCode: Restore IP Addresses 解题报告
    LeetCode: Longest Common Prefix 解题报告
    LeetCode: Regular Expression Matching 解题报告
    Python——rrdtool模块的安装
    python-xlsxwriter模块绘制表格
  • 原文地址:https://www.cnblogs.com/Camier-myNiuer/p/4111707.html
Copyright © 2011-2022 走看看