//AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(chackWifi) name:kReachabilityChangedNotification object:nil];
_reach = [Reachability reachabilityForLocalWiFi];
//开启监听
[_reach startNotifier];
return YES;
}
- (void)chackWifi
{
if ([_reach currentReachabilityStatus] == NotReachable)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"wifi已断开" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alert show];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"wifi已连接" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alert show];
}
}
//viewcontroller.m
//检测站点是否可连接
- (IBAction)conn:(UIButton *)sender
{
Reachability *reach = [Reachability reachabilityWithHostName:@"http://www.baidu.com"];
//检测连接方式
switch ([reach currentReachabilityStatus])
{
case NotReachable:
alert = [[UIAlertView alloc]initWithTitle:@"网络无法连接" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alert show];
break;
case ReachableViaWiFi:
alert = [[UIAlertView alloc]initWithTitle:@"通过wifi连接站点" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alert show];
break;
case ReachableViaWWAN:
alert = [[UIAlertView alloc]initWithTitle:@"通过2/3/4G网连接" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alert show];
break;
default:
break;
}
}
- (IBAction)G:(id)sender
{
Reachability *reach = [Reachability reachabilityForInternetConnection];
if ([reach currentReachabilityStatus] == NotReachable)
{
alert = [[UIAlertView alloc]initWithTitle:@"234G已关闭" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alert show];
//NSLog(@"234G已关闭");
}
else
{
alert = [[UIAlertView alloc]initWithTitle:@"234G已连接" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alert show];
//NSLog(@"234G已连接");
}
}
- (IBAction)wifi:(id)sender
{
Reachability *reach = [Reachability reachabilityForLocalWiFi];
if ([reach currentReachabilityStatus] == NotReachable)
{alert = [[UIAlertView alloc]initWithTitle:@"wifi已关闭" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alert show];
}
else
{ alert = [[UIAlertView alloc]initWithTitle:@"wifi已连接" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alert show];
}
}