获取wifi信息兼容iOS 14以上设备

This commit is contained in:
kai60
2022-04-29 18:07:47 +08:00
parent 64d5967585
commit 27b8ee3653
16 changed files with 474 additions and 485 deletions
@@ -125,21 +125,7 @@ typedef NS_ENUM(NSInteger,lodingViewdissMissStyle) {
}
//获取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)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
@@ -164,123 +150,125 @@ typedef NS_ENUM(NSInteger,lodingViewdissMissStyle) {
}
-(void)initComponent{
//967/1018/ 0.95
CGFloat percentHbiW = 0.117 ;//5s 35/300
CGFloat y = 20;
self.nodImg= [[UIImageView alloc] initWithFrame:CGRectMake(5, y,12, 12)];
self.nodImg.image = [UIImage imageNamed:@"linkingdevice_point"];
[self.view addSubview:self.nodImg];
self.wifilabel=[[UILabel alloc] init];
NSDictionary *ifs = [self fetchSSIDInfo];
if (!self.wifiName.length)
{
self.wifiName = [ifs objectForKey:@"SSID"];
}
self.wifilabel.text = [NSString stringWithFormat:@"请输入当前WiFi:%@密码",self.wifiName];
self.wifilabel.textAlignment = NSTextAlignmentLeft;
self.wifilabel.textColor = [UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1];
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:self.wifilabel.text];
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:8];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.wifilabel.text length])];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0 green:170.0/255.0 blue:218.0/255.0 alpha:1] range:NSMakeRange(10,self.wifiName.length)];
[self.wifilabel setAttributedText:attributedString];
[self.wifilabel sizeToFit];
CGFloat labelSizeW = [self widthForString:self.wifilabel.text fontSize:18 andHeight:10];
CGFloat labelH= [self heightForString:self.wifilabel.text fontSize:18 andWidth:labelSizeW];
self.wifilabel.frame = CGRectMake(CGRectGetMaxX(self.nodImg.frame), y, labelSizeW, labelH);
[self.view addSubview:self.wifilabel];
//wifi 输入框
CGFloat wifiFileW=kScreenSize.width - 2*10;
CGFloat wifiFileH=wifiFileW*percentHbiW;
self.wifiSecFiled=[[UITextField alloc] initWithFrame:CGRectMake(10,CGRectGetMaxY(self.wifilabel.frame) + 10, wifiFileW, wifiFileH)];
self.wifiSecFiled.placeholder = @"请输入WiFi密码";
self.wifiSecFiled.delegate=self;
self.wifiSecFiled.keyboardType=UIKeyboardTypeAlphabet;
self.wifiSecFiled.layer.masksToBounds=YES;
self.wifiSecFiled.layer.cornerRadius=5;
if (@available(iOS 13.0, *)){//iOS13之后,设置颜色的方便变更
[CommonUtils getNetworkTypeComplete:^(NSString *netconnType, NSString *BSSID, NSString *SSID) {
CGFloat percentHbiW = 0.117 ;//5s 35/300
CGFloat y = 20;
self.nodImg= [[UIImageView alloc] initWithFrame:CGRectMake(5, y,12, 12)];
self.nodImg.image = [UIImage imageNamed:@"linkingdevice_point"];
[self.view addSubview:self.nodImg];
}else{
[self.wifiSecFiled setValue:[UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1] forKeyPath:@"_placeholderLabel.textColor"];
}
self.wifiSecFiled.borderStyle = UITextBorderStyleNone;
self.wifiSecFiled.backgroundColor = [UIColor colorWithRed:244.0/255.0 green:244.0/255.0 blue:244.0/255.0 alpha:1];
NSUserDefaults*userDefsult=[NSUserDefaults standardUserDefaults];
NSString*pass=[userDefsult objectForKey:@"wifiPass"];
if (pass) {
self.wifiSecFiled.text = pass;
}
self.wifiSecFiled.textAlignment = NSTextAlignmentLeft;
[self.view addSubview:self.wifiSecFiled];
//占位view 让输入框文字 偏移 也有别的方法 contentoffset
UIView*phoneView=[[UIView alloc]initWithFrame:CGRectMake(0,0, 9, 10)];
self.wifiSecFiled.leftView=phoneView;
self.wifiSecFiled.leftViewMode=UITextFieldViewModeAlways;
CGFloat btnW=100;
//输入框内白色2像素view
UIView *whiteView=[[UIView alloc] initWithFrame:CGRectMake(wifiFileW - btnW -2, 0, 2, wifiFileH)];
whiteView.backgroundColor = [UIColor whiteColor];
[self.wifiSecFiled addSubview:whiteView];
self.sBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[self.sBtn setTitle:@"确定" forState:UIControlStateNormal];
self.wifilabel=[[UILabel alloc] init];
if (!self.wifiName.length)
{
self.wifiName = SSID;
}
self.wifilabel.text = [NSString stringWithFormat:@"请输入当前WiFi:%@密码",self.wifiName];
self.wifilabel.textAlignment = NSTextAlignmentLeft;
self.wifilabel.textColor = [UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1];
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:self.wifilabel.text];
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:8];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.wifilabel.text length])];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0 green:170.0/255.0 blue:218.0/255.0 alpha:1] range:NSMakeRange(10,self.wifiName.length)];
[self.wifilabel setAttributedText:attributedString];
[self.wifilabel sizeToFit];
CGFloat labelSizeW = [self widthForString:self.wifilabel.text fontSize:18 andHeight:10];
CGFloat labelH= [self heightForString:self.wifilabel.text fontSize:18 andWidth:labelSizeW];
self.wifilabel.frame = CGRectMake(CGRectGetMaxX(self.nodImg.frame), y, labelSizeW, labelH);
[self.view addSubview:self.wifilabel];
//wifi 输入框
CGFloat wifiFileW=kScreenSize.width - 2*10;
CGFloat wifiFileH=wifiFileW*percentHbiW;
self.wifiSecFiled=[[UITextField alloc] initWithFrame:CGRectMake(10,CGRectGetMaxY(self.wifilabel.frame) + 10, wifiFileW, wifiFileH)];
self.wifiSecFiled.placeholder = @"请输入WiFi密码";
self.wifiSecFiled.delegate=self;
self.wifiSecFiled.keyboardType=UIKeyboardTypeAlphabet;
self.wifiSecFiled.layer.masksToBounds=YES;
self.wifiSecFiled.layer.cornerRadius=5;
if (@available(iOS 13.0, *)){//iOS13之后,设置颜色的方便变更
}else{
[self.wifiSecFiled setValue:[UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1] forKeyPath:@"_placeholderLabel.textColor"];
}
self.wifiSecFiled.borderStyle = UITextBorderStyleNone;
self.wifiSecFiled.backgroundColor = [UIColor colorWithRed:244.0/255.0 green:244.0/255.0 blue:244.0/255.0 alpha:1];
NSUserDefaults*userDefsult=[NSUserDefaults standardUserDefaults];
NSString*pass=[userDefsult objectForKey:@"wifiPass"];
if (pass) {
self.wifiSecFiled.text = pass;
}
self.wifiSecFiled.textAlignment = NSTextAlignmentLeft;
[self.view addSubview:self.wifiSecFiled];
//占位view 让输入框文字 偏移 也有别的方法 contentoffset
UIView*phoneView=[[UIView alloc]initWithFrame:CGRectMake(0,0, 9, 10)];
self.wifiSecFiled.leftView=phoneView;
self.wifiSecFiled.leftViewMode=UITextFieldViewModeAlways;
CGFloat btnW=100;
//输入框内白色2像素view
UIView *whiteView=[[UIView alloc] initWithFrame:CGRectMake(wifiFileW - btnW -2, 0, 2, wifiFileH)];
whiteView.backgroundColor = [UIColor whiteColor];
[self.wifiSecFiled addSubview:whiteView];
self.sBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[self.sBtn setTitle:@"确定" forState:UIControlStateNormal];
self.sBtn.frame = CGRectMake(wifiFileW - btnW,0, btnW,wifiFileH);
[self.sBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.sBtn addTarget:self action:@selector(sBtn:) forControlEvents:UIControlEventTouchUpInside];
//self.sBtn.backgroundColor = COLOR_LABEL_TITLE;
[self.sBtn setBackgroundImage:[UIImage imageNamed:@"linkingdevice_botton"] forState:UIControlStateNormal];
[self.wifiSecFiled addSubview:self.sBtn];
//底部背景
UIImageView *backImg=[[UIImageView alloc] initWithFrame:CGRectMake(0,kScreenSize.height - kScreenSize.width * 1.1,kScreenSize.width,kScreenSize.width * 1.1)];
backImg.image = [UIImage imageNamed:@"linkingdevice_setting"];
backImg.userInteractionEnabled = YES;
[self.view addSubview:backImg];
self.sBtn.frame = CGRectMake(wifiFileW - btnW,0, btnW,wifiFileH);
[self.sBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.sBtn addTarget:self action:@selector(sBtn:) forControlEvents:UIControlEventTouchUpInside];
//self.sBtn.backgroundColor = COLOR_LABEL_TITLE;
[self.sBtn setBackgroundImage:[UIImage imageNamed:@"linkingdevice_botton"] forState:UIControlStateNormal];
[self.wifiSecFiled addSubview:self.sBtn];
//底部背景
UIImageView *backImg=[[UIImageView alloc] initWithFrame:CGRectMake(0,kScreenSize.height - kScreenSize.width * 1.1,kScreenSize.width,kScreenSize.width * 1.1)];
backImg.image = [UIImage imageNamed:@"linkingdevice_setting"];
backImg.userInteractionEnabled = YES;
[self.view addSubview:backImg];
//波纹
self.waveProgressView = [[LXWaveProgressView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.wifiSecFiled.frame) +10, kScreenSize.width,CGRectGetMinY(backImg.frame) - CGRectGetMaxY(self.wifiSecFiled.frame))];
self.attentionLabel=[[UILabel alloc]initWithFrame:CGRectMake(30, CGRectGetMidY(self.waveProgressView.frame), kScreenWidth-60, 130)];
self.attentionLabel.font=[UIFont systemFontOfSize:13];
self.attentionLabel.textColor=[UIColor blackColor];
self.attentionLabel.numberOfLines=0;
self.attentionLabel.text=@"1、请长按摄像头背后复位孔,复位摄像头。\n2、若Wifi名称有中文,请改成英文字母加数字。\n3、Wifi密码不能包含特殊符号(如‘“等)。\n4、若多次智能连接未成功,请尝试热点模式连接。";
[self.view addSubview:self.waveProgressView];
[self.view bringSubviewToFront:self.waveProgressView];
[self.view addSubview:self.attentionLabel];
//说明书
UIButton *shuimingshuBtn=[UIButton buttonWithType:UIButtonTypeCustom];
shuimingshuBtn.frame = CGRectMake(0,kScreenSize.height - 44 - 200, kScreenSize.width*1/3, 200);
[shuimingshuBtn addTarget:self action:@selector(ChaKanShuimingShu) forControlEvents:UIControlEventTouchUpInside];
//[shuimingshuBtn setBackgroundColor:[UIColor redColor]];
[self.view addSubview:shuimingshuBtn];
//设置输入状态UI
[self connectNormalView];
//波纹
self.waveProgressView = [[LXWaveProgressView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.wifiSecFiled.frame) +10, kScreenSize.width,CGRectGetMinY(backImg.frame) - CGRectGetMaxY(self.wifiSecFiled.frame))];
self.attentionLabel=[[UILabel alloc]initWithFrame:CGRectMake(30, CGRectGetMidY(self.waveProgressView.frame), kScreenWidth-60, 130)];
self.attentionLabel.font=[UIFont systemFontOfSize:13];
self.attentionLabel.textColor=[UIColor blackColor];
self.attentionLabel.numberOfLines=0;
self.attentionLabel.text=@"1、请长按摄像头背后复位孔,复位摄像头。\n2、若Wifi名称有中文,请改成英文字母加数字。\n3、Wifi密码不能包含特殊符号(如‘“等)。\n4、若多次智能连接未成功,请尝试热点模式连接。";
[self.view addSubview:self.waveProgressView];
[self.view bringSubviewToFront:self.waveProgressView];
[self.view addSubview:self.attentionLabel];
//说明书
UIButton *shuimingshuBtn=[UIButton buttonWithType:UIButtonTypeCustom];
shuimingshuBtn.frame = CGRectMake(0,kScreenSize.height - 44 - 200, kScreenSize.width*1/3, 200);
[shuimingshuBtn addTarget:self action:@selector(ChaKanShuimingShu) forControlEvents:UIControlEventTouchUpInside];
//[shuimingshuBtn setBackgroundColor:[UIColor redColor]];
[self.view addSubview:shuimingshuBtn];
//设置输入状态UI
[self connectNormalView];
}];
}
@@ -315,14 +303,16 @@ self.attentionLabel.text=@"1、请长按摄像头背后复位孔,复位摄像
#pragma mark- 点击确定
-(void)goAPmode
{
ConAquarNowWiFiPwdVC *vc = InitObject(ConAquarNowWiFiPwdVC);
vc.deviceType=DEVICECAMERA;
NSDictionary *netInfo = [CommonUtils getSSIDInfo];
vc.ssid = [netInfo objectForKey:@"SSID"];
vc.bssid = [netInfo objectForKey:@"BSSID"];
[self.navigationController pushViewController:vc animated:YES];
[CommonUtils getNetworkTypeComplete:^(NSString *netconnType, NSString *BSSID, NSString *SSID) {
ConAquarNowWiFiPwdVC *vc = InitObject(ConAquarNowWiFiPwdVC);
vc.deviceType=DEVICECAMERA;
vc.ssid = SSID;
vc.bssid = BSSID;
[self.navigationController pushViewController:vc animated:YES];
}];
}
-(void)sBtn:(UIButton*)btn{
NSString *ssidName =@"";
@@ -519,21 +509,23 @@ self.attentionLabel.text=@"1、请长按摄像头背后复位孔,复位摄像
{
//
if (self.deviceID.length&&![self.linkDict[self.deviceID] boolValue]) {//90秒之后还未连接上,连接超时
[[GWP2PDeviceLinker shareInstance] p2pStopSmartLink];
[[GWP2PDeviceLinker shareInstance] p2pStopAPLink];
if ([[[CommonUtils getSSIDInfo] objectForKey:@"SSID"] isEqualToString:self.wifiName])
{
[self.view makeToast:@"请确保Wifi密码正确,重置设备后再试!"];
}
else
{
[self.view makeToast:[NSString stringWithFormat:@"WiFi连接失败,请确保手机连接到%@",self.wifiName]];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[CommonUtils getNetworkTypeComplete:^(NSString *netconnType, NSString *BSSID, NSString *SSID) {
[[GWP2PDeviceLinker shareInstance] p2pStopSmartLink];
[[GWP2PDeviceLinker shareInstance] p2pStopAPLink];
[self.navigationController popViewControllerAnimated:YES];
});
if ([SSID isEqualToString:self.wifiName])
{
[self.view makeToast:@"请确保Wifi密码正确,重置设备后再试!"];
}
else
{
[self.view makeToast:[NSString stringWithFormat:@"WiFi连接失败,请确保手机连接到%@",self.wifiName]];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.navigationController popViewControllerAnimated:YES];
});
}];
}
else if(!self.deviceID.length)
@@ -13,7 +13,7 @@
#import "AppDelegate.h"
#import "Utils.h"
#import "UDManager.h"
#import "Reachability.h"
#include <sys/types.h>
#include <stdint.h>
#include <unistd.h>
@@ -161,8 +161,7 @@ withFilterContext:(id)filterContext
while (1)
{
//没有连接wifi,肯定不是ap模式
NSString* ssid = [Utils currentWifiSSID];
if (ssid == nil) {
if ([Reachability reachabilityForInternetConnection].currentReachabilityStatus!=ReachableViaWiFi) {
NSLog(@"get ap mode info failed, no wifi");
break;
}
@@ -117,7 +117,7 @@
while (1)
{
//没有wifi时不搜索
NSString* ssid = [Utils currentWifiSSID];
BOOL iswifi = ([Reachability reachabilityForInternetConnection].currentReachabilityStatus==ReachableViaWiFi);
//ap模式不搜索
int dwApContactID = [[AppDelegate sharedDefault] dwApContactID];
@@ -125,7 +125,7 @@
//后台运行不搜索
UIApplicationState iState = [UIApplication sharedApplication].applicationState;
_isConditionOK = (ssid != nil &&
_isConditionOK = (iswifi &&
dwApContactID == 0 &&
iState != UIApplicationStateBackground);