ifish/Ifish/controllers/leftcontrollers/ConnectHotpotViewController.m

150 lines
5.5 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.

//
// ConnectHotpotViewController.m
// Ifish
//
// Created by Minghao Xue on 2018/7/16.
// Copyright © 2018年 lianlian. All rights reserved.
//
#import "ConnectHotpotViewController.h"
#import "ConnectHotspotTipViewController.h"
#import "dataContorl.h"
#import<SystemConfiguration/CaptiveNetwork.h>
#import "GCDAsyncUdpSocket.h"
@interface ConnectHotpotViewController ()<UITextFieldDelegate, GCDAsyncUdpSocketDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UITextField *nameLbl;
@property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *dashView;
@property (weak, nonatomic) IBOutlet UITextField *pwdTf;
@property (weak, nonatomic) IBOutlet UIView *containerView;
@property (nonatomic, strong) GCDAsyncUdpSocket *socket;
@end
@implementation ConnectHotpotViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"热点连接";
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keybordframeChanged:) name:UIKeyboardDidChangeFrameNotification object:nil];
for (UIImageView *view in self.dashView) {
view.backgroundColor = [UIColor clearColor];
view.image = [self imageWithLineWithSize:view.frame.size];
}
}
- (UIImage *)imageWithLineWithSize:(CGSize)size{
CGFloat width = size.width;
UIGraphicsBeginImageContext(size);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGFloat lengths[] = {2,2};
CGContextRef line = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(line, [UIColor colorWithRed:133/255.0 green:133/255.0 blue:133/255.0 alpha:1.0].CGColor);
CGContextSetLineDash(line, 0, lengths, 1);
CGContextMoveToPoint(line, 0, 1);
CGContextAddLineToPoint(line, width-10, 1);
CGContextStrokePath(line);
return UIGraphicsGetImageFromCurrentImageContext();
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keybordframeChanged:(NSNotification *)no {
NSDictionary *userInfo = no.userInfo;
// 动画的持续时间
double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 键盘的frame
CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardF = [self.view convertRect:keyboardF fromView:[UIApplication sharedApplication].keyWindow];
CGRect containerViewF = [self.view convertRect:self.containerView.frame fromView:self.scrollView];
CGFloat miniSpacing = 4;
CGFloat offset = CGRectGetMaxY(containerViewF) + miniSpacing - CGRectGetMinY(keyboardF);
if (offset <= 0) {
offset = 0;
}
[UIView animateWithDuration:duration animations:^{
self.scrollView.contentInset = UIEdgeInsetsMake(-offset, 0, 0, 0);
}];
}
- (IBAction)clickHereBtnClicked:(id)sender {
ConnectHotspotTipViewController *vc = [[ConnectHotspotTipViewController alloc] initWithNibName:@"ConnectHotspotTipViewController" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
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];
}
- (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
- (void)sendToDeviceWithSSIDName:(NSString *)name andSSIDPWD:(NSString *)pwd {
self.socket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
[self.socket sendData:[self updmessageData:name ssidPwd:pwd] toHost:@"192.168.4.1" port:333 withTimeout:-1 tag:0];
}
- (NSData *)updmessageData:(NSString *)ssidName ssidPwd:(NSString *)ssidPwd {
return [dataContorl stringToHexData:[NSString stringWithFormat:@"%@\0%@\0", ssidPwd, ssidPwd]];
}
#pragma mark - GCDAsyncUdpSocketDelegate
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tag {
[self.view makeToast:@"UDP指令发送成功"];
}
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error {
[self.view makeToast:[NSString stringWithFormat:@"UDP指令发送失败%@", [error localizedDescription]]];
}
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext {
[self.view makeToast:[NSString stringWithFormat:@"接收到设备返回UDP数据:%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]]];
}
@end