美文网首页
获取Wi-Fi的ssid和bssid

获取Wi-Fi的ssid和bssid

作者: 移动端_小刚哥 | 来源:发表于2017-12-05 18:43 被阅读1217次

首先介绍一下概念

  • ssid Wi-Fi名称
  • bssid Mac地址
    这两个概念我是网上搜来的,如果不对的话还请大佬指正

ssid的设置比较简单,设置Wi-Fi的时候就可以直接设置,修改起来也比较容易;
简单粗暴的代码:

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"];

因为公司的app是需要提交AppStore审核的,所以首先考虑到有没有涉及到私有api到问题
拒原文大佬记载:

刚开始写demo的时候,在网上查了很多资料,说是ios拿到WiFi名字的方法自从ios9出来以后就被苹果屏蔽,如果需要使用这个框架的方法需要向苹果申请资料!但是但是 其实苹果屏蔽的是属于NetworkExtension框架中的关于VPN开发的方法 如果仅仅手机使用简单的拿到WiFi的方法是可以不用申请的!

第一印象就是这么写应该是可以上架的,于是继续往下看;

CFArrayRef __nullable
CNCopySupportedInterfaces   (void)          __OSX_AVAILABLE_STARTING(__MAC_10_8,__IPHONE_4_1);

/*!
 @constant kCNNetworkInfoKeySSIDData
 @discussion NetworkInfo Dictionary key for SSID in CFData format
 */
extern const CFStringRef kCNNetworkInfoKeySSIDData  __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_1);

/*!
 @constant kCNNetworkInfoKeySSID
 @discussion NetworkInfo Dictionary key for SSID in CFString format
 */
extern const CFStringRef kCNNetworkInfoKeySSID      __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_1);

/*!
 @constant kCNNetworkInfoKeyBSSID
 @discussion NetworkInfo Dictionary key for BSSID in CFString format
 */
extern const CFStringRef kCNNetworkInfoKeyBSSID     __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_1);

如果要拿到WiFi 名字和WiFi 的 mac 地址我们只需要用到CNCopySupportedInterfaces CNCopyCurrentNetworkInfo 这两个大类 而我们可以看到这两个类没有常见的被画上红色横线 更没有红色字体提示该方法或该类目已经被替换不可使用的 而在xcode7中同样如此 不信大家可以试试 那么为什么网上几乎所有的帖子都说这个方法被屏蔽了呢 包括我之前在QQ群里问了很多人 都说需要向苹果申请权限 不然会被拒 我在想 之所以说这话的人应该是用了以下方法来获得WiFi名字

for (NEHotspotNetwork *hot in [NEHotspotHelper supportedNetworkInterfaces]) {
        double signalStrength = hot.signalStrength;
        NSLog(@"singnalStrength : %f",signalStrength);
    }



NS_CLASS_AVAILABLE(NA, 9_0)
@interface NEHotspotHelper : NSObject
/*!
 * @method registerWithOptions:queue:handler
 * @abstract
 *   Register the application as a HotspotHelper.
 * @discussion
 *   Once this API is invoked successfully, the application becomes
 *   eligible to be launched in the background and participate in
 *   various hotspot related functions.
 *
 *   This function should be called once when the application starts up.
 *   Invoking it again will have no effect and result in FALSE being returned.
 *
 *   The 'options' dictionary may be nil, or contain the single property
 *   kNEHotspotHelperOptionDisplayName.
 *
 * @param options If not nil, 'options' is an NSDictionary containing
 *   kNEHotspotHelperOption* keys (currently just
 *   kNEHotspotHelperOptionDisplayName).
 * @param queue The dispatch_queue_t to invoke the handle block on.
 * @param handler The NEHotspotHelperHandler block to execute to process
 *   helper commands.
 * @return
 *   YES if the registration was successful, NO otherwise.
 * @note Notes
 * @note 1
 *   The application's Info.plist MUST include a UIBackgroundModes array
 *   containing 'network-authentication'.
 * @note 2
 *   The application MUST set 'com.apple.developer.networking.HotspotHelper'
 *   as one of its entitlements. The value of the entitlement is a boolean
 *   value true.
 */

大家可以看到 在这个类目的使用下面有两个注意点 1、plist文件必须包括一个UIBackgroundModes数组,里面含有“网络身份验证”信息 2、应用程序必须设置“com.apple.developer.networking.HotspotHelper”*作为其权利之一。而这两点 就是提醒我们如果要使用这个类 必须向苹果申请开发权限 否则不可使用该类 。 其实,如果我们只是做简单的拿到手机所连接的WiFi名字和mac地址 是可以用另外的方法的,也就不需要想苹果申请权限 ,而苹果公司给我的被拒的邮件中也充分说明了这一点 ,我估计是因为网上很多人都以为只要是使用NetworExtension这个类的方法就一定要申请开发权限 ,其实不是的 ,苹果只是禁止了部分权限而已,大家还是以官方文档作为唯一参考吧!!!

参考地址:http://www.jianshu.com/p/629fe552eeaf

相关文章

网友评论

      本文标题:获取Wi-Fi的ssid和bssid

      本文链接:https://www.haomeiwen.com/subject/tjncixtx.html