reachbility检测网络变化

来源:互联网 发布:红黑树 c语言实现 编辑:程序博客网 时间:2024/05/17 06:35

appdelegate中

Reachability* reach = [ReachabilityreachabilityWithHostname:@"www.baidu.com"];

    [reach startNotifier];

需要检测网络状态变化的controller中:

先注册下广播观察者:

  [[NSNotificationCenterdefaultCenter] addObserver:self

                                             selector:@selector(reachabilityChanged:)

                                                 name:kReachabilityChangedNotification

                                               object:nil];


-(void)reachabilityChanged:(NSNotification*)notification{

    //NSLog(@"%@",notification

          //.userInfo);

    Reachability* curReach = [notificationobject];

    NSParameterAssert([curReachisKindOfClass: [Reachabilityclass]]);

    NetworkStatus status = [curReachcurrentReachabilityStatus];

    if (status !=NotReachable)

    {

        

        self.datalist=nil;

        [_tablereloadData];

        [selfgetData];

        

        

    }

}


0 0
原创粉丝点击