//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];
//開啟監(jiān)聽
[_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
//檢測(cè)站點(diǎn)是否可連接
- (IBAction)conn:(UIButton *)sender
{
Reachability *reach = [Reachability reachabilityWithHostName:@"http://www.baidu.com"];
//檢測(cè)連接方式
switch ([reach currentReachabilityStatus])
{
case NotReachable:
alert = [[UIAlertView alloc]initWithTitle:@"網(wǎng)絡(luò)無(wú)法連接" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"確定", nil];
[alert show];
break;
case ReachableViaWiFi:
alert = [[UIAlertView alloc]initWithTitle:@"通過(guò)wifi連接站點(diǎn)" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"確定", nil];
[alert show];
break;
case ReachableViaWWAN:
alert = [[UIAlertView alloc]initWithTitle:@"通過(guò)2/3/4G網(wǎng)連接" 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已關(guān)閉" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
[alert show];
//NSLog(@"234G已關(guān)閉");
}
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已關(guān)閉" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
[alert show];
}
else
{? alert = [[UIAlertView alloc]initWithTitle:@"wifi已連接" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
[alert show];
}
}