天天看點

iOS wifi 路由器 連接配接

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捐助),沒錢捧個人場,謝謝各位。

iOS wifi 路由器 連接配接
iOS wifi 路由器 連接配接
iOS wifi 路由器 連接配接

 謝謝您的贊助,我會做的更好!