ios获取wifi信息和配置wifi

iOS App无需跳转系统设置自动连接Wi-Fi:https://juejin.im/post/5c85cf8ee51d453a637c12df

ios获取wifi列表:https://www.csdn.net/gather_2f/MtjaIg1sODcwNi1ibG9n.html

IOS支持的编码类型:https://blog.csdn.net/shang1219178163/article/details/48733791/


获取当前连接的wifi信息的函数(SSID和SSIDDATA)

+ (NSString*)getCurrentWifi

{

    NSString*wifiName =nil;

    NSData*SSIDDATA =nil; 

    CFArrayRef myArray = CNCopySupportedInterfaces();

    if(myArray !=nil)

    {

        CFDictionaryRef myDict =CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));

        if(myDict !=nil)

        {

            NSDictionary*dict = (NSDictionary*)CFBridgingRelease(myDict);

            wifiName = [dictvalueForKey:@"SSID"];

            SSIDDATA = [dictvalueForKey:@"SSIDDATA"];//SSIDDATA是ssid的二进制数据,有些路由器使用的GBK编码中文显示乱码,需要拿SSIDDATA转码

        }

    }

    if(wifiName !=nil&& SSIDDATA !=nil)

    {

        NSString *UTF8WifiName = [[NSString alloc] initWithData:SSIDDATA encoding:NSUTF8StringEncoding];

        if(UTF8WifiName == nil)//如果SSIDDATA不是UTF-8格式则把GBK转码为UTF-8

        {

            //用GBK解码

            NSStringEncoding gbkEncoding=CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);

            NSString*GBKWifiName = [[NSStringalloc]initWithData:SSIDDATAencoding:gbkEncoding];


            //转化为UTF-8

            NSCharacterSet*allowedCharacters = [[NSCharacterSetcharacterSetWithCharactersInString:GBKWifiName]invertedSet];

            NSString*encodedUrl = [GBKWifiNamestringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];

            wifiName = [encodedUrlstringByRemovingPercentEncoding];

        }

    }

    returnwifiName;

}

你可能感兴趣的:(ios获取wifi信息和配置wifi)