網絡檢測

//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,721評論 2 7
  • #import<Foundation/Foundation.h> #import #import "sqlTest...
    lichengjin閱讀 428評論 0 0
  • 用法: 先添加指針視圖,轉盤背景和開始按鈕。按鈕的點擊事件里設置開始動畫,在動畫開始的代理方法中讓開始按鈕不響應點...
    全棧的貓南北閱讀 1,632評論 1 3
  • 在北宋建立之前,中國大地還處于分裂狀態,史稱五代十國時期(公元891年-公元960年),在這69年的紛爭中,...
    問鼎君閱讀 1,046評論 0 2
  • 多肉植物的品種很多,形態各異,景天科有很多相似的外形,其中的吉娃娃是我很喜歡的一種,顏色和樣子十分討喜,看著讓人想...
    雪盈禪心閱讀 885評論 2 3