iOS 使用Reachability检测网络状态

  一:  添加源文件和framework

  (1)添加源文件,将Reachability类拷贝到工程中
Reachability文件可以在这里下载:https://github.com/tonymillion/Reachability/blob/master/Reachability.h(支持ARC和GCD)
  (2).添加framework:
  将SystemConfiguration.framework 框架添加进工程。
二:检查网络状态

 Reachability.h中定义了三种网络状态:
  typedef enum {
    NotReachable = 0,      //无连接
    ReachableViaWiFi,      //使用WiFi网络
    ReachableViaWWAN      //使用3G/GPRS网络
  } NetworkStatus;

因此可以在以下方法中这样写:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

   //检查网络

    Reachability *reach = [Reachability reachabilityWithHostname:@"www.apple.com"];

    NetworkStatus status = [reach currentReachabilityStatus];

    [self connectInState:status];

     returnYES;

}

//提示用户正在使用哪种连接方式

- (void)connectInState:(NetworkStatus)state

{

    switch (state) {

            //网络未连接

        case NotReachable:{

            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络未连接" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:Nil,nil];

            [alert dismissWithClickedButtonIndex:1 animated:NO];

            [alert show];

        }

            break;

            //正在使用WiFi连接

        case ReachableViaWiFi:{

            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"正使用WiFi连接" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:Nil,nil];

            [alert dismissWithClickedButtonIndex:1 animated:NO];

            [alert show];

        }

            break;

            //正在使用3G/GPRS网络连接

        case ReachableViaWWAN:{

            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"正使用3G/GPRS网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:Nil,nil];

            [alert dismissWithClickedButtonIndex:1 animated:NO];

            [alert show];

        }

        default:

            break;

   }

}

三. 链接状态的实时通知

  网络连接状态的实时检查,通知在网络应用中也是十分必要的。接续状态发生变化时,需要及时地通知用户:

  Reachability 2.0版本

// 设置网络状态变化时的通知函数

- (void)viewDidLoad

{

    [super viewDidLoad];

   Reachability * reach = [Reachability reachabilityWithHostname:@"www.apple.com"];

  [[NSNotificationCenter defaultCenter] addObserver:self 

    selector:@selector(reachabilityChanged:) 

                                                 name:kReachabilityChangedNotification 

                                               object:nil];

[reach startNotifier];

}

-(void)reachabilityChanged:(NSNotification*)note

{

    Reachability* curReach = [note object];

    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);

    NetworkStatus status = [curReach currentReachabilityStatus];

    if (status == NotReachable){

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"未连接" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];

        [alert show];

  // [alert release];

    } else if (status == ReachableViaWiFi) {

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"正在使用WiFi" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];

        [alert show];

    // [alert release];

    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值