ifish/Ifish/controllers/ConnectAauariumVC/ConAquarChooseWiFiVC.m

177 lines
6.2 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// ConAquarChooseWiFiVC.m
// Ifish
//
// Created by wbzhan on 2019/9/20.
// Copyright © 2019 lianlian. All rights reserved.
//
#import "ConAquarChooseWiFiVC.h"
#import "ConnectingAquarVC.h"
#import <SystemConfiguration/CaptiveNetwork.h>
@interface ConAquarChooseWiFiVC ()
Strong UIImageView *topImage;
Strong UIButton *connectWiFiBtn;
Assign BOOL isGoChangeWiFi;//更换为设备的WiFi
@end
@implementation ConAquarChooseWiFiVC
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:Noti_WillEnterForeground object:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self addTitleViewWithTitle:@"选择设备WiFi"];
[self.view addSubview:self.topImage];
[self.view addSubview:self.connectWiFiBtn];
[self addContentLabel];
[self.topImage mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.mas_equalTo(self.view);
make.height.mas_equalTo(kSizeFrom750(424));
}];
[self.connectWiFiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.view);
make.bottom.mas_equalTo(-30);
make.width.mas_equalTo(200);
make.height.mas_equalTo(30);
}];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewWillEnterForeground:) name:Noti_WillEnterForeground object:nil];
// Do any additional setup after loading the view.
}
- (UIImageView *)topImage
{
if (!_topImage) {
_topImage = InitObject(UIImageView);
[_topImage setImage:IMAGEBYENAME(@"icons_con_topbg")];
}
return _topImage;
}
- (UIButton *)connectWiFiBtn{
if (!_connectWiFiBtn) {
_connectWiFiBtn = InitObject(UIButton);
[_connectWiFiBtn setTitle:@"去设置WiFi" forState:UIControlStateNormal];
[_connectWiFiBtn setBackgroundColor:RGB(88, 151, 231)];
[_connectWiFiBtn setTitleColor:XWhite forState:UIControlStateNormal];
[_connectWiFiBtn.titleLabel setFont:FontSize(14)];
_connectWiFiBtn.layer.cornerRadius = kSizeFrom750(10);
_connectWiFiBtn.layer.masksToBounds = YES;
[_connectWiFiBtn addTarget:self action:@selector(connectWiFiBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}
return _connectWiFiBtn;
}
-(void)addContentLabel{
UILabel *preLabel;
NSString*tip1=@"1.请将产品重置后15秒内断电通电3次进入热点模式";
NSString*tip2=@"2.点击去设置:设置-WiFi-选择ifish-xxx的网络输入密码12345678连接好后返回爱奇鱼app。";
if (self.deviceType==DEVICECAMERA)
{
tip2=@"2.点击去设置:设置-WiFi-选择GW-AP-xxx的网络一般无密码或12345678或查看说明书连接好后返回爱奇鱼app。";
}
NSArray*textArr=@[tip1,tip2];
for (int i=0; i<2; i++) {
UILabel *label = InitObject(UILabel);
label.text = textArr[i];
label.textColor = RGB_92;
label.font = FontSize(12);
label.numberOfLines = 0;
[self.view addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(kSizeFrom750(60));
make.right.mas_equalTo(-kSizeFrom750(60));
if (preLabel==nil) {
make.top.mas_equalTo(self.topImage.mas_bottom).offset(kSizeFrom750(120));
} else {
make.top.mas_equalTo(preLabel.mas_bottom).offset(kSizeFrom750(30));
}
}];
preLabel = label;
}
}
//跳转到WiFi设置页面
-(void)connectWiFiBtnClick:(UIButton *)sender{
NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if([[UIApplication sharedApplication] canOpenURL:url]) {
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
}else {
[[UIApplication sharedApplication] openURL:url];
}
self.isGoChangeWiFi = YES;
}
}
-(void)checkWiFi{
NSDictionary *ifs = [self getSSIDInfo];
NSString *wifiName = [ifs objectForKey:@"SSID"];
//先判断之前的wifi名称是否和热点的wifi名称重复如果冲突则提示wifi名称错误
if ([wifiName isEqualToString:self.wifiName]) {
[self.view makeToast:@"请先连接设备热点"];
return;
}
if ([wifiName rangeOfString:@"ifish"].location!=NSNotFound||[wifiName rangeOfString:@"GW"].location!=NSNotFound) {
//wifi正确则尝试连接设备
if(self.deviceType==DEVICECAMERA)
{
YooseeNextConnectViewController *nextvc=[[YooseeNextConnectViewController alloc] init];
nextvc.wifiName=self.wifiName;
nextvc.wifiPwd=self.wifiPwd;
nextvc.conectType =conectType_ap;
[self.navigationController pushViewController:nextvc animated:YES];
}
else
{
ConnectingAquarVC *connecting = InitObject(ConnectingAquarVC);
connecting.ssid = self.ssid;
connecting.bssid = self.bssid;
connecting.wifiName = self.wifiName;
connecting.wifiPassword = self.wifiPwd;
[self.navigationController pushViewController:connecting animated:YES];
}
}else{
[self.view makeToast:@"WiFi名称不匹配请重试"];
}
}
- (NSDictionary *)getSSIDInfo
{
NSArray *interfaceNames = CFBridgingRelease(CNCopySupportedInterfaces());
NSDictionary *SSIDInfo;
for (NSString *interfaceName in interfaceNames) {
SSIDInfo = CFBridgingRelease(
CNCopyCurrentNetworkInfo((__bridge CFStringRef)interfaceName));
BOOL isNotEmpty = (SSIDInfo.count > 0);
if (isNotEmpty) {
break;
}
}
return SSIDInfo;
}
//即将进入前台
-(void)viewWillEnterForeground:(NSNotification *)noti{
if (self.isGoChangeWiFi) {
[self checkWiFi];
}
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end