網(wǎng)絡(luò)檢測(cè)

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

//開(kāi)啟監(jiān)聽(tīng)

[_reach startNotifier];

return YES;

}

- (void)chackWifi

{

if ([_reach currentReachabilityStatus] == NotReachable)

{

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"wifi已斷開(kāi)" 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];

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容