iOS 程式碼 獲取wifi資訊 及主動連線WiFi

weixin_34308389發表於2018-08-30

1、獲取WiFi資訊

>

info.ssid = dic[@"SSID"]; 

 info.bssid = dic[@"BSSID"];

 info.ssidData = dic[@"SSIDDATA"]; 

 NSString *str = [[NSString alloc] initWithData:dic[@"SSIDDATA"] encoding:NSUTF8StringEncoding]; 

 NSLog(@"%@", str);

>

-(NSDictionary *)getWifiInfo{

    NSArray *ifs = (__bridge_transfer id)(CNCopySupportedInterfaces());

    //NSLog(@"interface %@", ifs);

    NSDictionary *info = nil;

    for (NSString *ifname in ifs) {

        info = (__bridge_transfer NSDictionary *)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifname);

        //NSLog(@"%@ => %@",  ifname, info);

    }

    return info;

}

>





2、主動連線
//https://blog.csdn.net/github_28024665/article/details/78222471

#import < NetworkExtension/NEHotspotConfigurationManager.h  >

- (IBAction)connectWifi:(id)sender {

    //建立將要連線的WIFI配置例項

    NEHotspotConfiguration * hotspotConfig = [[NEHotspotConfiguration alloc] initWithSSID:@"Xiaomi_9A36" passphrase:@"12121212" isWEP:NO];

    // 開始連線 (呼叫此方法後系統會自動彈窗確認)

    [[NEHotspotConfigurationManager sharedManager] applyConfiguration:hotspotConfig completionHandler:^(NSError * _Nullable error) {

        NSLog(@"%@", error);

        if (!error) {

            self.msgLabel.text = @"切換成功";

        }else{

            self.msgLabel.text = error.localizedDescription;

        }

    }];

}

相關文章