導(dǎo)入 Reachability3.0 第三方SDK
導(dǎo)入頭文件
#import "Reachability.h"
創(chuàng)建三個按鈕
// 按鈕1
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:@"檢測站點(diǎn)是否可連接" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(didClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
// 按鈕2
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn1.frame = CGRectMake(100, 300, 300, 30);
[btn1 setTitle:@"檢測234G是否可連接" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(didClick234G) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
// 按鈕3
UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn3.frame = CGRectMake(100, 400, 300, 30);
[btn3 setTitle:@"檢測wifi是否可連接" forState:UIControlStateNormal];
[btn3 addTarget:self action:@selector(didClickwifi) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn3];
第一個按鈕的點(diǎn)擊事件 --- 檢測站點(diǎn)是否可連接
// 檢測站點(diǎn)是否可連接
-(void)didClick
{
// 檢測站點(diǎn)奔缠,通過 Reachability 檢測
Reachability *reach = [Reachability reachabilityWithHostName:@"https://www.taobao.com"];
// 開始檢測
// 檢測reach當(dāng)前網(wǎng)絡(luò)狀態(tài)
switch ([reach currentReachabilityStatus]) {
case NotReachable:{ // 無網(wǎng)絡(luò)狀態(tài)
NSLog(@"該站點(diǎn)無法連接。");
// 提示框
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"該站點(diǎn)無法連接" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:^{
}];
}
break;
case ReachableViaWiFi:{
NSLog(@"通過wifi連接站點(diǎn)");
// 提示框
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"通過wifi連接站點(diǎn)" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:^{
}];
}
break;
case ReachableViaWWAN:{
NSLog(@"通過234G連接站點(diǎn)");
// 提示框
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"通過234G連接站點(diǎn)" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:^{
}];
}
break;
default:
break;
}
}
第二個按鈕的點(diǎn)擊事件 --- 檢測234G是否可連接
// 檢測234G是否可連接
-(void)didClick234G
{
Reachability *reach = [Reachability reachabilityForInternetConnection];
// 判斷 234G 的狀態(tài)
if ([reach currentReachabilityStatus] != NotReachable) {
NSLog(@"234G已連接");
// 提示框
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"234G已連接" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:^{
}];
}else{
NSLog(@"234G已斷開");
// 提示框
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"234G已斷開" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:^{
}];
}
}
第三個按鈕的點(diǎn)擊事件 -- 檢測wifi是否可連接
// 檢測wifi是否可連接
-(void)didClickwifi
{
Reachability *reach = [Reachability reachabilityForLocalWiFi];
// 判斷
if ([reach currentReachabilityStatus] != NotReachable) {
NSLog(@"wifi已連接");
// 提示框
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"wifi已連接" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:^{
}];
}else{
NSLog(@"wifi已斷開");
// 提示框
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"wifi已斷開" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:^{
}];
}
}
wifi狀態(tài)發(fā)生改變時彈出提示框
在 AppDelegate.h 中
// 定義成員變量
{
Reachability *_reach;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 注冊通知,檢測wifi狀態(tài)的改變
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TongZhi) name:kReachabilityChangedNotification object:nil];
// 初始化rearch
_reach = [Reachability reachabilityForLocalWiFi];
// 開始監(jiān)聽
[_reach startNotifier];
return YES;
}
點(diǎn)擊事件
// 注冊通知方法
-(void)TongZhi
{
if ([_reach currentReachabilityStatus] == NotReachable) {
NSLog(@"wifi已經(jīng)斷開");
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"已斷開" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:action];
[self.window.rootViewController presentViewController:alert animated:YES completion:^{
}];
}else{
NSLog(@"wifi已連接");
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"已連接" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:action];
[self.window.rootViewController presentViewController:alert animated:YES completion:^{
}];
}
}