获取wifi信息兼容iOS 14以上设备
This commit is contained in:
@@ -76,25 +76,14 @@ Copy NSString *wifiName;//当前wifi名称
|
||||
//
|
||||
// self.navigationItem.leftBarButtonItem=backItem;
|
||||
|
||||
NSDictionary *wifiDic = [self fetchSSIDInfo];
|
||||
NSString *wifiName = [wifiDic objectForKey:@"SSID"];
|
||||
self.wifiNameTextField.text = wifiName;
|
||||
[CommonUtils getNetworkTypeComplete:^(NSString *netconnType, NSString *BSSID, NSString *SSID) {
|
||||
self.wifiNameTextField.text = SSID;
|
||||
}];
|
||||
|
||||
}
|
||||
//获取当前链接WiFi信息
|
||||
- (id)fetchSSIDInfo
|
||||
{
|
||||
NSArray *ifs = (id)CFBridgingRelease(CNCopySupportedInterfaces());
|
||||
NSLog(@"%s: Supported interfaces: %@", __func__, ifs);
|
||||
id info = nil;
|
||||
for (NSString *ifnam in ifs) {
|
||||
info = (id)CFBridgingRelease(CNCopyCurrentNetworkInfo((CFStringRef)ifnam));
|
||||
if (info && [info count]) {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
return info ;
|
||||
}
|
||||
|
||||
|
||||
-(void)clickBack{
|
||||
[self.navigationController popViewControllerAnimated:YES];
|
||||
}
|
||||
@@ -154,39 +143,30 @@ Copy NSString *wifiName;//当前wifi名称
|
||||
return YES;
|
||||
}
|
||||
- (IBAction)setLinkBtnClicked:(id)sender {
|
||||
if (self.nameLbl.text.length == 0) {
|
||||
[self.view makeToast:@"请输入正确的路由器名称"];
|
||||
return;
|
||||
}
|
||||
BOOL isRight =[dataContorl isIncludeSpecialCharact:self.pwdTf.text];
|
||||
|
||||
if (!isRight) {
|
||||
[self.view makeToast:@"您输入的密码带特殊符号,请更改路由器密码。"];
|
||||
return;
|
||||
}
|
||||
|
||||
if (![[self currentWifiSSID].lowercaseString hasPrefix:@"ifish"]) {
|
||||
[self.view makeToast:@"请先将手机连接到wifi:ifish-xxxx"];
|
||||
return;
|
||||
}
|
||||
|
||||
NSString *ssidName = self.nameLbl.text;
|
||||
NSString *ssidPwd = self.pwdTf.text;
|
||||
[self sendToDeviceWithSSIDName:ssidName andSSIDPWD:ssidPwd];
|
||||
[CommonUtils getNetworkTypeComplete:^(NSString *netconnType, NSString *BSSID, NSString *SSID) {
|
||||
if (self.nameLbl.text.length == 0) {
|
||||
[self.view makeToast:@"请输入正确的路由器名称"];
|
||||
return;
|
||||
}
|
||||
BOOL isRight =[dataContorl isIncludeSpecialCharact:self.pwdTf.text];
|
||||
|
||||
if (!isRight) {
|
||||
[self.view makeToast:@"您输入的密码带特殊符号,请更改路由器密码。"];
|
||||
return;
|
||||
}
|
||||
|
||||
if (![SSID.lowercaseString hasPrefix:@"ifish"]) {
|
||||
[self.view makeToast:@"请先将手机连接到wifi:ifish-xxxx"];
|
||||
return;
|
||||
}
|
||||
|
||||
NSString *ssidName = self.nameLbl.text;
|
||||
NSString *ssidPwd = self.pwdTf.text;
|
||||
[self sendToDeviceWithSSIDName:ssidName andSSIDPWD:ssidPwd];
|
||||
}];
|
||||
}
|
||||
|
||||
- (NSString *)currentWifiSSID {
|
||||
NSString *ssid = @"Not Found";
|
||||
CFArrayRef myArray = CNCopySupportedInterfaces();
|
||||
if (myArray != nil) {
|
||||
CFDictionaryRef myDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
|
||||
if (myDict != nil) {
|
||||
NSDictionary *dict = (NSDictionary *)CFBridgingRelease(myDict);
|
||||
ssid = [dict valueForKey:@"SSID"];
|
||||
}
|
||||
}
|
||||
return ssid;
|
||||
}
|
||||
|
||||
|
||||
#pragma mark - UDP related
|
||||
|
||||
@@ -213,31 +193,34 @@ Copy NSString *wifiName;//当前wifi名称
|
||||
}
|
||||
|
||||
- (void)startBinding {
|
||||
BOOL networkOk = NO;
|
||||
AFNetworkReachabilityStatus status = [AFNetworkReachabilityManager sharedManager].networkReachabilityStatus;
|
||||
if (status == AFNetworkReachabilityStatusReachableViaWiFi) {
|
||||
if (![[self currentWifiSSID].lowercaseString hasPrefix:@"ifish"]) {
|
||||
networkOk = YES;
|
||||
|
||||
[CommonUtils getNetworkTypeComplete:^(NSString *netconnType, NSString *BSSID, NSString *SSID) {
|
||||
if ([netconnType isEqualToString:@"no network"]||([netconnType isEqualToString:@"Wifi"]&&[SSID.lowercaseString hasPrefix:@"ifish"]))
|
||||
{
|
||||
|
||||
//无网络 或者连接的还是设备的热点
|
||||
}
|
||||
} else if (status == AFNetworkReachabilityStatusReachableViaWWAN) {
|
||||
networkOk = YES;
|
||||
}
|
||||
if (!networkOk) {
|
||||
return;
|
||||
}
|
||||
if (self.isBindingDevice) {
|
||||
return;
|
||||
}
|
||||
if (self.macAddress.length == 0) {
|
||||
return;
|
||||
}
|
||||
self.isBindingDevice = YES;
|
||||
if (self.bindTimer) {
|
||||
[self.bindTimer invalidate];
|
||||
self.bindTimer = nil;
|
||||
}
|
||||
self.retryTimes = 0;
|
||||
[self bindDeviceWithSsid:self.macAddress];
|
||||
else
|
||||
{
|
||||
if (self.isBindingDevice) {
|
||||
return;
|
||||
}
|
||||
if (self.macAddress.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
self.isBindingDevice = YES;
|
||||
if (self.bindTimer) {
|
||||
[self.bindTimer invalidate];
|
||||
self.bindTimer = nil;
|
||||
}
|
||||
self.retryTimes = 0;
|
||||
[self bindDeviceWithSsid:self.macAddress];
|
||||
|
||||
}
|
||||
}];
|
||||
|
||||
|
||||
}
|
||||
|
||||
- (void)udpHelperMessage:(NSString *)msg {
|
||||
|
||||
@@ -245,31 +245,33 @@ extern BOOL formLogIn;//连接页面是否来自登录界面
|
||||
}];
|
||||
}
|
||||
-(void)connectMyWIFI{
|
||||
|
||||
NSUserDefaults*userDefsult=[NSUserDefaults standardUserDefaults];
|
||||
NSString*pass=[userDefsult objectForKey:@"wifiPass"];
|
||||
NSDictionary *netInfo = [self fetchNetInfo];
|
||||
_ssid = [netInfo objectForKey:@"SSID"];
|
||||
_bssid = [netInfo objectForKey:@"BSSID"];
|
||||
NSString *msg = [NSString stringWithFormat:@"请输入%@密码",_ssid];
|
||||
UIAlertView *alerV = [[UIAlertView alloc]initWithTitle:msg
|
||||
message:nil
|
||||
delegate:self
|
||||
cancelButtonTitle:@"取消"
|
||||
otherButtonTitles:@"确定", nil];
|
||||
alerV.alertViewStyle = UIAlertViewStylePlainTextInput;
|
||||
_wifiFiled=[alerV textFieldAtIndex:0];
|
||||
_wifiFiled.secureTextEntry=NO;
|
||||
_wifiFiled.text=pass;
|
||||
_wifiFiled.clearButtonMode=UITextFieldViewModeAlways;
|
||||
alerV.tag=ALERTVIEW_TAG;
|
||||
|
||||
[alerV show];
|
||||
|
||||
// [self bindDeviceWithSsid:@"5CCF7F0065CA"];
|
||||
// 5CCF7F00669C
|
||||
// 5CCF7F006668
|
||||
// 5CCF7F0065CA
|
||||
[CommonUtils getNetworkTypeComplete:^(NSString *netconnType, NSString *BSSID, NSString *SSID) {
|
||||
NSUserDefaults*userDefsult=[NSUserDefaults standardUserDefaults];
|
||||
NSString*pass=[userDefsult objectForKey:@"wifiPass"];
|
||||
|
||||
_ssid = SSID;
|
||||
_bssid = BSSID;
|
||||
NSString *msg = [NSString stringWithFormat:@"请输入%@密码",_ssid];
|
||||
UIAlertView *alerV = [[UIAlertView alloc]initWithTitle:msg
|
||||
message:nil
|
||||
delegate:self
|
||||
cancelButtonTitle:@"取消"
|
||||
otherButtonTitles:@"确定", nil];
|
||||
alerV.alertViewStyle = UIAlertViewStylePlainTextInput;
|
||||
_wifiFiled=[alerV textFieldAtIndex:0];
|
||||
_wifiFiled.secureTextEntry=NO;
|
||||
_wifiFiled.text=pass;
|
||||
_wifiFiled.clearButtonMode=UITextFieldViewModeAlways;
|
||||
alerV.tag=ALERTVIEW_TAG;
|
||||
|
||||
[alerV show];
|
||||
|
||||
// [self bindDeviceWithSsid:@"5CCF7F0065CA"];
|
||||
// 5CCF7F00669C
|
||||
// 5CCF7F006668
|
||||
// 5CCF7F0065CA
|
||||
|
||||
}];
|
||||
|
||||
}
|
||||
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
|
||||
@@ -395,39 +397,12 @@ extern BOOL formLogIn;//连接页面是否来自登录界面
|
||||
[userdefult synchronize];
|
||||
}
|
||||
|
||||
- (NSString *)fetchSsid
|
||||
{
|
||||
NSDictionary *ssidInfo = [self fetchNetInfo];
|
||||
|
||||
return [ssidInfo objectForKey:@"SSID"];
|
||||
}
|
||||
|
||||
- (NSString *)fetchBssid
|
||||
{
|
||||
NSDictionary *bssidInfo = [self fetchNetInfo];
|
||||
|
||||
return [bssidInfo objectForKey:@"BSSID"];
|
||||
}
|
||||
|
||||
|
||||
// refer to http://stackoverflow.com/questions/5198716/iphone-get-ssid-without-private-library
|
||||
- (NSDictionary *)fetchNetInfo
|
||||
{
|
||||
NSArray *interfaceNames = CFBridgingRelease(CNCopySupportedInterfaces());
|
||||
// NSLog(@"%s: Supported interfaces: %@", __func__, interfaceNames);
|
||||
|
||||
NSDictionary *SSIDInfo;
|
||||
for (NSString *interfaceName in interfaceNames) {
|
||||
SSIDInfo = CFBridgingRelease(
|
||||
CNCopyCurrentNetworkInfo((__bridge CFStringRef)interfaceName));
|
||||
// NSLog(@"%s: %@ => %@", __func__, interfaceName, SSIDInfo);
|
||||
|
||||
BOOL isNotEmpty = (SSIDInfo.count > 0);
|
||||
if (isNotEmpty) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return SSIDInfo;
|
||||
}
|
||||
|
||||
|
||||
#pragma mark 绑定设备
|
||||
-(void)bindDeviceWithSsid:(NSString*)bssid{
|
||||
|
||||
|
||||
@@ -492,33 +492,35 @@ extern BOOL isfromCameraView;
|
||||
|
||||
}
|
||||
-(void)connectMyWIFI{
|
||||
|
||||
NSUserDefaults*userDefsult=[NSUserDefaults standardUserDefaults];
|
||||
NSString*pass=[userDefsult objectForKey:@"wifiPass"];
|
||||
NSDictionary *netInfo = [self fetchNetInfo];
|
||||
_ssid = [netInfo objectForKey:@"SSID"];
|
||||
_bssid = [netInfo objectForKey:@"BSSID"];
|
||||
|
||||
NSString *msg = [NSString stringWithFormat:@"请输入当前WiFi:%@的密码",_ssid];
|
||||
|
||||
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:msg];
|
||||
[CommonUtils getNetworkTypeComplete:^(NSString *netconnType, NSString *BSSID, NSString *SSID) {
|
||||
|
||||
NSUserDefaults*userDefsult=[NSUserDefaults standardUserDefaults];
|
||||
NSString*pass=[userDefsult objectForKey:@"wifiPass"];
|
||||
|
||||
_ssid = SSID;
|
||||
_bssid = BSSID;
|
||||
|
||||
NSString *msg = [NSString stringWithFormat:@"请输入当前WiFi:%@的密码",_ssid];
|
||||
|
||||
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:msg];
|
||||
|
||||
NSMutableParagraphStyle *paraghStyle =[[NSMutableParagraphStyle alloc] init];
|
||||
[paraghStyle setLineSpacing:4];
|
||||
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0 green:170.0/255.0 blue:218.0/255.0 alpha:1] range:NSMakeRange(msg.length - 3 -_ssid.length,_ssid.length)];
|
||||
[attributedString addAttribute:NSParagraphStyleAttributeName value:paraghStyle range:NSMakeRange(0, msg.length)];
|
||||
|
||||
[_wifiNamelabel setAttributedText:attributedString];
|
||||
_wifiTextFiled.secureTextEntry=NO;
|
||||
_wifiTextFiled.text=pass;
|
||||
_wifiTextFiled.clearButtonMode=UITextFieldViewModeAlways;
|
||||
|
||||
|
||||
//[self bindDeviceWithSsid:@"5ccf7f003a93"];
|
||||
// 5CCF7F00669C
|
||||
// 5CCF7F006668
|
||||
// 5CCF7F0065CA
|
||||
|
||||
NSMutableParagraphStyle *paraghStyle =[[NSMutableParagraphStyle alloc] init];
|
||||
[paraghStyle setLineSpacing:4];
|
||||
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0 green:170.0/255.0 blue:218.0/255.0 alpha:1] range:NSMakeRange(msg.length - 3 -_ssid.length,_ssid.length)];
|
||||
[attributedString addAttribute:NSParagraphStyleAttributeName value:paraghStyle range:NSMakeRange(0, msg.length)];
|
||||
|
||||
[_wifiNamelabel setAttributedText:attributedString];
|
||||
_wifiTextFiled.secureTextEntry=NO;
|
||||
_wifiTextFiled.text=pass;
|
||||
_wifiTextFiled.clearButtonMode=UITextFieldViewModeAlways;
|
||||
|
||||
|
||||
//[self bindDeviceWithSsid:@"5ccf7f003a93"];
|
||||
// 5CCF7F00669C
|
||||
// 5CCF7F006668
|
||||
// 5CCF7F0065CA
|
||||
|
||||
}];
|
||||
}
|
||||
-(void)checkLocation{
|
||||
NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
|
||||
@@ -530,24 +532,7 @@ extern BOOL isfromCameraView;
|
||||
}
|
||||
}
|
||||
// refer to http://stackoverflow.com/questions/5198716/iphone-get-ssid-without-private-library
|
||||
- (NSDictionary *)fetchNetInfo
|
||||
{
|
||||
NSArray *interfaceNames = CFBridgingRelease(CNCopySupportedInterfaces());
|
||||
// NSLog(@"%s: Supported interfaces: %@", __func__, interfaceNames);
|
||||
|
||||
NSDictionary *SSIDInfo;
|
||||
for (NSString *interfaceName in interfaceNames) {
|
||||
SSIDInfo = CFBridgingRelease(
|
||||
CNCopyCurrentNetworkInfo((__bridge CFStringRef)interfaceName));
|
||||
// NSLog(@"%s: %@ => %@", __func__, interfaceName, SSIDInfo);
|
||||
|
||||
BOOL isNotEmpty = (SSIDInfo.count > 0);
|
||||
if (isNotEmpty) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return SSIDInfo;
|
||||
}
|
||||
|
||||
|
||||
//保存wifi密码
|
||||
-(void)savePassw{
|
||||
|
||||
Reference in New Issue
Block a user