网络监测

#import "Reachability.h"



{

[super viewDidLoad];

UIButton *bu=[[UIButton alloc]initWithFrame:CGRectMake(60, 80, 200, 44)];

[bu setTitle:@"网络监测是否可用" forState:UIControlStateNormal];

bu.backgroundColor=[UIColor redColor];

[bu addTarget:self action:@selector(fan) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:bu];

UIButton *bu2=[[UIButton alloc]initWithFrame:CGRectMake(60, 140, 200, 44)];

[bu2 setTitle:@"网络是否可用" forState:UIControlStateNormal];

bu2.backgroundColor=[UIColor redColor];

[bu2 addTarget:self action:@selector(fa) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:bu2];

UIButton *bu3=[[UIButton alloc]initWithFrame:CGRectMake(60, 240, 200, 44)];

[bu3 setTitle:@"网络是否" forState:UIControlStateNormal];

bu3.backgroundColor=[UIColor redColor];

[bu3 addTarget:self action:@selector(f) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:bu3];

}

-(void)fan

{

Reachability *rea=[Reachability reachabilityWithHostName:@"http://www.baidu.com"];

switch ([rea currentReachabilityStatus])

{

case ReachableViaWiFi:

NSLog(@"通过无线连接");

break;

case ReachableViaWWAN:

NSLog(@"通过234G连接");

break;

case NotReachable:

NSLog(@"没有网");

break;

}

}

-(void)fa

{

Reachability *req=[Reachability reachabilityForLocalWiFi];

if ([req currentReachabilityStatus]==NotReachable)

{

NSLog(@"没有网");

}

else

{

NSLog(@"有网");

}

}

-(void)f

{

Reachability *rew=[Reachability reachabilityForInternetConnection];

if ([rew currentReachabilityStatus]==NotReachable)

{

[[[UIAlertView alloc]initWithTitle:@"提示" message:@"234G未连接" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] show];

}

else

{

[[[UIAlertView alloc]initWithTitle:@"提示" message:@"234G已连接" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] show];

}

}

你可能感兴趣的:(网络监测)