iOS wifi 路由器 连接 (3年前的代码,现在已经忘记啥意思了)
目的: 让智能家居设备 连接指定的wifi
原理:在路由器的 管理页面模拟登录。。。
// // WifiSelectViewController.h // AirAppTest // // Created by apple on 13-11-30. // Copyright (c) 2013年 apple. All rights reserved. // #import <UIKit/UIKit.h> #import "ASIHTTPRequest.h" #import "Wifi.h" #import "Utils.h" #import "LoginViewController.h" #import "LYViewController.h" @interface WifiSelectViewController : UIViewController<UITableViewDataSource, UITableViewDelegate,ASIHTTPRequestDelegate>{ NSMutableArray* aryItems; BOOL _reloading; int prevItemCount; } @property (nonatomic,retain) UITableView *tableView; @property (nonatomic,retain) UITableViewCell *tableViewCell; @property (nonatomic,retain) NSMutableArray *aryItems; @end
// // WifiSelectViewController.m // AirAppTest // // Created by apple on 13-11-30. // Copyright (c) 2013年 apple. All rights reserved. // #import "WifiSelectViewController.h" #import "Wifi.h" @interface WifiSelectViewController () @end @implementation WifiSelectViewController @synthesize tableView ; @synthesize tableViewCell; @synthesize aryItems ; Wifi *yyy; #pragma mark - page 界面 #pragma mark 初始化 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { } return self; } #pragma mark 加载界面 -(void)loadView{ [self.navigationController setToolbarHidden:YES animated:YES]; [self.navigationController setNavigationBarHidden:YES animated:NO]; //代码实现界面 //添加一个唯一父视图 UIView *baseView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; UIColor *bgColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]]; baseView.backgroundColor = bgColor; self.view = baseView ; //蓝底面板 //设置logo UIImageView *yfrdlogo = [[UIImageView alloc] init]; [yfrdlogo setFrame:CGRectMake(90,15,140,40)]; UIImage *imgyfrdlogo =[Utils stretch:@"logo.png"]; yfrdlogo.image= imgyfrdlogo; [self.view addSubview:yfrdlogo]; // // UIImage *buttonbg = [Utils stretch:@"login_midbutton_background_highlighted.png"]; // //login_midbutton_background_highlighted.9.png // // UIImageView *buttonbgbg = [[UIImageView alloc] init]; // [buttonbgbg setFrame:CGRectMake(10,65,300,25)]; // buttonbgbg.image= buttonbg; // [self.view addSubview:buttonbgbg]; // UIButton *fhbutton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // [fhbutton setTitle:@"返回登陆页面" forState:UIControlStateNormal]; // [fhbutton setBackgroundImage:[UIImage imageNamed:@"history_nav_mid.png"] forState:UIControlStateNormal]; // [fhbutton setFrame:CGRectMake(10, 60, 100, 30)]; // [fhbutton addTarget:self // action:@selector(goback) // forControlEvents:UIControlEventTouchUpInside]; // [self.view addSubview:fhbutton]; // // UIButton *sxbutton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // [sxbutton setTitle:@"刷新" forState:UIControlStateNormal]; // [sxbutton setBackgroundImage:[UIImage imageNamed:@"history_nav_mid.png"] forState:UIControlStateNormal]; // [sxbutton setFrame:CGRectMake(110, 60, 100, 30)]; // [sxbutton addTarget:self // action:@selector(showagain) // forControlEvents:UIControlEventTouchUpInside]; // [self.view addSubview:sxbutton]; UIImageView *bgimageview = [[UIImageView alloc] init]; [bgimageview setFrame:CGRectMake(0,55,320,30)]; UIImage *bgimage =[Utils stretch:@"login_midbutton_background_highlighted.9.png"]; bgimageview.image= bgimage; [self.view addSubview:bgimageview]; UIButton *cqbutton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [cqbutton setTitle:@"重启" forState:UIControlStateNormal]; [cqbutton setTitleColor:[UIColor colorWithRed:.196 green:.309 blue:.521 alpha:1] forState:UIControlStateNormal]; [cqbutton setFrame:CGRectMake(130,55,50,30)]; [cqbutton addTarget:self action:@selector(restart) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:cqbutton]; CGRect rect = [[UIScreen mainScreen] bounds]; CGSize size = rect.size; CGFloat width = size.width; CGFloat height = size.height; UIImageView *bg2imageview = [[UIImageView alloc] init]; [bg2imageview setFrame:CGRectMake(0,85,320,30)]; UIImage *bg2image =[Utils stretch:@"login_midbutton_background_highlighted.9.png"]; bg2imageview.image= bg2image; [self.view addSubview:bg2imageview]; // UILabel *yiqibianhao = [[UILabel alloc] initWithFrame:CGRectMake(20,85,75,25)]; yiqibianhao.text =@"WIFI信号"; yiqibianhao.font = [UIFont systemFontOfSize:15]; yiqibianhao.textAlignment = NSTextAlignmentCenter ; yiqibianhao.backgroundColor = [UIColor clearColor]; [self.view addSubview:yiqibianhao]; // UILabel *wangluojieru = [[UILabel alloc] initWithFrame:CGRectMake(130,85,75,25)]; wangluojieru.text =@"加密"; wangluojieru.font = [UIFont systemFontOfSize:15]; wangluojieru.textAlignment = NSTextAlignmentCenter ; wangluojieru.backgroundColor = [UIColor clearColor]; [self.view addSubview:wangluojieru]; // UILabel *caozuo = [[UILabel alloc] initWithFrame:CGRectMake(155,85,170,25)]; caozuo.text =@"计算"; caozuo.font = [UIFont systemFontOfSize:15]; caozuo.textAlignment = NSTextAlignmentCenter ; //caozuo.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"history_nav_mid.png"]]; caozuo.backgroundColor = [UIColor clearColor]; [self.view addSubview:caozuo]; UILabel *diLabelFrame = [[UILabel alloc] init]; [diLabelFrame setFrame:CGRectMake(0,height-40,width,40)]; diLabelFrame.textAlignment = NSTextAlignmentCenter ; diLabelFrame.backgroundColor = [UIColor blackColor]; [self.view addSubview:diLabelFrame]; //返回按钮 UIButton *telbutton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [telbutton setFrame:CGRectMake(5, height-35, 30, 30)]; [telbutton setBackgroundImage:[UIImage imageNamed:@"btn_back1.png"] forState:UIControlStateNormal]; [telbutton addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:telbutton]; //shuaixin按钮 UIButton *sxbutton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [sxbutton setFrame:CGRectMake(280, height-35, 30, 30)]; [sxbutton setBackgroundImage:[UIImage imageNamed:@"refresh.png"] forState:UIControlStateNormal]; [sxbutton addTarget:self action:@selector(showagain) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:sxbutton]; } #pragma mark - 方法功能 #pragma mark 返回登陆界面 - (void)goback{ [self.navigationController popViewControllerAnimated:NO]; } #pragma mark 刷新wifi -(void)showagain{ //刷新等待标志 [tooles showHUD:@"刷新WIFI……"]; //连接网络 NSString *urlstr = @"http://10.10.100.254/site_survey.html"; NSURL *myurl = [NSURL URLWithString:urlstr]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:myurl]; //Basic认证 [request setAuthenticationScheme:(NSString *)kCFHTTPAuthenticationSchemeBasic]; [request setUseKeychainPersistence:YES]; [request setUsername:@"admin"]; [request setPassword:@"admin"]; [request setDelegate:self]; [request setDidFinishSelector:@selector(GetWifiResult:)]; [request setDidFailSelector:@selector(GetWifiErr:)]; [request startAsynchronous]; } #pragma mark 跳转wifi界面并获取数据 - (void)GetWifiResult:(ASIHTTPRequest *)request{ NSData *data =[request responseData]; NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSArray *array = [result componentsSeparatedByString:@"selectedSSIDChange('"]; NSMutableArray *listData = [[NSMutableArray alloc] init]; for (int i=1; i<[array count]; i++) { NSString *tablex = [array objectAtIndex:i]; NSArray *arrayx = [tablex componentsSeparatedByString:@"')"]; NSString *tablen = [arrayx objectAtIndex:0]; NSArray *arrayn = [tablen componentsSeparatedByString:@"', '"]; Wifi *xyz = [[Wifi alloc] init]; xyz.SSID = [arrayn objectAtIndex:0]; xyz.BSSID = [arrayn objectAtIndex:1]; // xyz.RSSI = [arrayn objectAtIndex:3]; xyz.Channel = [arrayn objectAtIndex:2]; xyz.Encryption = [[[arrayn objectAtIndex:3] componentsSeparatedByString:@"','"]objectAtIndex:0]; xyz.Authentication = [[[arrayn objectAtIndex:3] componentsSeparatedByString:@"','"]objectAtIndex:1]; // xyz.NetworkType = [[[arrayn objectAtIndex:7] componentsSeparatedByString:@"</td> </tr>"]objectAtIndex:0]; // NSLog(@"---%@-%@-%@-%@-%@---",xyz.SSID,xyz.BSSID , xyz.Channel,xyz.Encryption,xyz.Authentication); [listData addObject: xyz]; } //跳转wifi界面 WifiSelectViewController *mainVC = [[WifiSelectViewController alloc] init]; mainVC.aryItems = [[NSMutableArray alloc] initWithArray:listData]; [self.navigationController pushViewController:mainVC animated:NO]; //关闭等待标志 [tooles removeHUD]; } #pragma mark 跳转wifi界面失败 - (void) GetWifiErr:(ASIHTTPRequest *)request{ [tooles removeHUD]; [tooles MsgBox:@"网络连接状态差,再进行网络连接……"]; } #pragma mark wifi设置成功后重启路由 -(void)restart{ [tooles showHUD:@"正在重启…"]; NSString *urlstr = @"http://10.10.100.254/success_cn.html"; NSURL *myurl = [NSURL URLWithString:urlstr]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:myurl]; //Basic认证 [request setAuthenticationScheme:(NSString *)kCFHTTPAuthenticationSchemeBasic]; [request setUseKeychainPersistence:YES]; [request setUsername:@"admin"]; [request setPassword:@"admin"]; [request addRequestHeader:@"Accept" value:@"*/*"]; [request addRequestHeader:@"Accept-Language" value:@"zh-CN"]; [request addRequestHeader:@"Referer" value:@"http://10.10.100.254/do_cmd_cn.html"]; [request addRequestHeader:@"User-Agent" value:@"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"]; [request addRequestHeader:@"Accept-Encoding" value:@"gzip, deflate"]; [request addRequestHeader:@"Host" value:@"10.10.100.254"]; [request addRequestHeader:@"Connection" value:@"Keep-Alive"]; [request setPostValue:@"RESTART" forKey:@"HF_PROCESS_CMD"]; [request setDelegate:self]; [request setDidFinishSelector:@selector(GetPswRestartResult:)]; [request setDidFailSelector:@selector(GetPswRestartErr:)]; [request startAsynchronous]; } - (void) GetPswRestartResult:(ASIHTTPRequest *)request{ [tooles removeHUD]; [tooles MsgBox:@"重启成功…"]; } - (void) GetPswRestartErr:(ASIHTTPRequest *)request{ [tooles removeHUD]; [tooles MsgBox:@"网络连接状态差,请重新重启…"]; } #pragma mark 点击tablecell 获取响应 得到实时数据 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"连接网络" message:@"输入密码" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"连接",nil]; alert.alertViewStyle = UIAlertViewStylePlainTextInput; yyy = [aryItems objectAtIndex:[indexPath row]]; [alert show]; } #pragma mark 获取alert界面的按钮动作 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex == 0) { NSLog(@"--------quexiao"); }else if(buttonIndex == 1){ UITextField *tf=[alertView textFieldAtIndex:0]; NSString *urlstr = @"http://10.10.100.254/do_cmd_cn.html"; NSURL *myurl = [NSURL URLWithString:urlstr]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:myurl]; //Basic认证 [request setAuthenticationScheme:(NSString *)kCFHTTPAuthenticationSchemeBasic]; [request setUseKeychainPersistence:YES]; [request setUsername:@"admin"]; [request setPassword:@"admin"]; [request addRequestHeader:@"Accept" value:@"*/*"]; [request addRequestHeader:@"Accept-Language" value:@"zh-CN"]; [request addRequestHeader:@"Referer" value:@"http://10.10.100.254/wireless_cn.html"]; [request addRequestHeader:@"User-Agent" value:@"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"]; [request addRequestHeader:@"Accept-Encoding" value:@"gzip, deflate"]; [request addRequestHeader:@"Host" value:@"10.10.100.254"]; [request addRequestHeader:@"Connection" value:@"Keep-Alive"]; [request setPostValue:yyy.SSID forKey:@"sta_setting_ssid"]; [request setPostValue:tf.text forKey:@"sta_setting_wpakey"]; [request setPostValue:yyy.Encryption forKey:@"sta_setting_encry"]; [request setPostValue:yyy.Authentication forKey:@"sta_setting_auth"]; [request setPostValue:yyy.Authentication forKey:@"sta_setting_auth_sel"]; [request setPostValue:yyy.Encryption forKey:@"sta_setting_encry_sel"]; [request setPostValue:@"ASCII" forKey:@"sta_setting_type_sel"]; [request setPostValue:@"DHCP" forKey:@"wan_setting_dhcp"]; [request setDelegate:self]; [request setDidFinishSelector:@selector(GetWifiPswResult:)]; [request setDidFailSelector:@selector(GetWifiPswErr:)]; [request startAsynchronous]; } } - (void) GetWifiPswErr:(ASIHTTPRequest *)request{ [tooles MsgBox:@"网络连接状态差,请重新设置…"]; } - (void) GetWifiPswResult:(ASIHTTPRequest *)request{ [tooles MsgBox:@"网络设置成功,请重启一下设置…"]; } #pragma mark - 加载tableview 加载界面 #pragma mark 获取table cell的个数 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [aryItems count]; } #pragma mark 显示cell的数据 -(UITableViewCell *) tableView:(UITableView *)tableView2 cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //创建cell static NSString *CellTableIdentifier = @"CellTabeIndentifier"; UITableViewCell *cell = [tableView2 dequeueReusableCellWithIdentifier:CellTableIdentifier]; if (cell == nil) { cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellTableIdentifier]; CGRect labelSSIDValueRect = CGRectMake(10, 0,110,44); UILabel *labelSSIDValue = [[UILabel alloc] initWithFrame:labelSSIDValueRect]; labelSSIDValue.textAlignment = NSTextAlignmentLeft; labelSSIDValue.textColor =[UIColor colorWithRed:.196 green:.309 blue:.521 alpha:1]; labelSSIDValue.tag = 601; labelSSIDValue.font = [UIFont italicSystemFontOfSize:10]; [cell.contentView addSubview:labelSSIDValue]; CGRect lableEncryptionRect = CGRectMake(150, 0,50,44); UILabel *lableEncryptionValue = [[UILabel alloc] initWithFrame:lableEncryptionRect]; lableEncryptionValue.textAlignment = NSTextAlignmentLeft; lableEncryptionValue.textColor =[UIColor colorWithRed:.196 green:.309 blue:.521 alpha:1]; lableEncryptionValue.tag = 602; lableEncryptionValue.font = [UIFont italicSystemFontOfSize:10]; [cell.contentView addSubview:lableEncryptionValue]; CGRect lableAuthenticationRect = CGRectMake(230, 0,40,44); UILabel *lableAuthenticationValue = [[UILabel alloc] initWithFrame:lableAuthenticationRect]; lableAuthenticationValue.textAlignment = NSTextAlignmentLeft; lableAuthenticationValue.textColor =[UIColor colorWithRed:.196 green:.309 blue:.521 alpha:1]; lableAuthenticationValue.tag = 603; lableAuthenticationValue.font = [UIFont italicSystemFontOfSize:10]; [cell.contentView addSubview:lableAuthenticationValue]; //加载向右的图标 UIImageView *imagearrow = [[UIImageView alloc]initWithFrame:CGRectMake(280, 15, 10 ,10 )]; imagearrow.image = [UIImage imageNamed:@"arrow.png"]; [cell.contentView addSubview:imagearrow]; UILabel *yiqidLabelFrame = [[UILabel alloc] init]; [yiqidLabelFrame setFrame:CGRectMake(0,43,320,1)]; yiqidLabelFrame.textAlignment = NSTextAlignmentCenter ; yiqidLabelFrame.backgroundColor = [UIColor blackColor]; [cell.contentView addSubview:yiqidLabelFrame]; } if (indexPath.row % 2) { [cell setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]]; }else { [cell setBackgroundColor:[UIColor whiteColor]]; } NSUInteger row = [indexPath row]; Wifi *xxx= [aryItems objectAtIndex:row]; //加载数据ssid数据 UILabel *labelSSID = (UILabel *)[cell.contentView viewWithTag:601]; labelSSID.text =xxx.SSID; UILabel *lableEncryption = (UILabel *)[cell.contentView viewWithTag:602]; lableEncryption.text =xxx.Encryption; UILabel *lableAuthentication = (UILabel *)[cell.contentView viewWithTag:603]; lableAuthentication.text =xxx.Authentication; return cell; } #pragma mark 设置cell的高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 44.f; } #pragma mark 加载视图完成 - (void)viewDidLoad{ [super viewDidLoad]; //设置tableView高度 CGRect rect = [[UIScreen mainScreen] bounds]; CGSize size = rect.size; CGFloat width = size.width; CGFloat height = size.height; //加载tableView数据 tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,115, width, height - 135 )]; tableView.backgroundColor = nil; tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.delegate=self; self.tableView.dataSource=self; [self.view addSubview:tableView]; //重新加载 [tableView reloadData]; } #pragma mark 警告提示 - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
#import <Foundation/Foundation.h> @interface Wifi : NSObject @property(nonatomic,retain) NSString * SSID; @property(nonatomic,retain) NSString * BSSID; @property(nonatomic,retain) NSString * RSSI; @property(nonatomic,retain) NSString * Channel; @property(nonatomic,retain) NSString * Encryption; @property(nonatomic,retain) NSString * Authentication; @property(nonatomic,retain) NSString * NetworkType; @property(nonatomic,retain) NSString * Password; @end
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!