网络检测

//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];

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS开发系列--网络开发 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博、微信等,这些应用本身可...
    lichengjin阅读 3,764评论 2 7
  • #import<Foundation/Foundation.h> #import #import "sqlTest...
    lichengjin阅读 438评论 0 0
  • 用法: 先添加指针视图,转盘背景和开始按钮。按钮的点击事件里设置开始动画,在动画开始的代理方法中让开始按钮不响应点...
    全栈的猫南北阅读 1,658评论 1 3
  • 在北宋建立之前,中国大地还处于分裂状态,史称五代十国时期(公元891年-公元960年),在这69年的纷争中,...
    问鼎君阅读 1,057评论 0 2
  • 多肉植物的品种很多,形态各异,景天科有很多相似的外形,其中的吉娃娃是我很喜欢的一种,颜色和样子十分讨喜,看着让人想...
    雪盈禅心阅读 888评论 2 3