iOS 获取WIFI名称以及连接WIFI

  • 获取WiFi名称

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    #import <SystemConfiguration/CaptiveNetwork.h>

    - (NSString *)currentIphoneConnectedWifiName{
    NSString *wifiName = nil;

    CFArrayRef wifiInterfaces = CNCopySupportedInterfaces();
    if (!wifiInterfaces)return nil;
    NSArray *interfaces = (__bridge NSArray *)wifiInterfaces;

    for (NSString *interfaceName in interfaces){

    CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interfaceName));

    if (dictRef){

    NSDictionary *networkInfoDic = (__bridge NSDictionary *)dictRef;

    wifiName = [networkInfoDic objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];
    CFRelease(dictRef);
    }
    }
    CFRelease(wifiInterfaces);
    return wifiName;
    }
  • 连接WiFi(需要开启Hostpot Configuration)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #import <NetworkExtension/NEHotspotConfigurationManager.h>

    - (void)hotpostAction {
    NEHotspotConfiguration* configuration = [[NEHotspotConfiguration alloc]initWithSSID:@"WIFI名称" passphrase:@"WIFI密码" isWEP:NO]; //加入有密码的wifi
    [[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:^(NSError * _Nullable error) {
    if([[self currentIphoneConnectedWifiName]isEqualToString:@"WIFI名称"]) {
    NSLog(@"加入网络成功");
    }
    NSLog(@"%@",error);
    }];
    }
本文总阅读量