如何从iOS Reachability Class更改网络连接通知? [英] How to get change in network connection notification from iOS Reachability Class?

查看:68
本文介绍了如何从iOS Reachability Class更改网络连接通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我想捕获每当用户在我的应用程序中获得网络连接时我已经添加了苹果Reachability类,下面是我在appDelegate类中使用的片段didFinishLaunchingWithOptions方法,

Hi I want to capture whenever user gets a network connectivity in my application for this I have added apples Reachability class and below is the snippet I am using in my appDelegate class didFinishLaunchingWithOptions method,

Reachability* reachability = [Reachability reachabilityForInternetConnection];
        [reachability startNotifier];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];

我的reachabilityChanged选择器方法如下

and my reachabilityChanged selector method is as below

- (void)reachabilityChanged:(NSNotification*)notification
{
    Reachability* reachability = notification.object;
    if(reachability.currentReachabilityStatus == NotReachable)
        NSLog(@"Internet off");
    else
        NSLog(@"Internet on");
}

但在这里我关闭飞机时没有收到任何通知模式,当我在手机中获得网络连接时。

but here I am not getting any kind of notification when I switch off my Airplane mode and when I get a network connectivity in my phone.

我错过了什么吗?

推荐答案

我在appdelegate中使用变量将当前网络状态存储为bool

I use a variable in appdelegate to store the current network status as a bool

@property (nonatomic, assign) BOOL hasInet;

.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self setUpRechability];
}


-(void)setUpRechability
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNetworkChange:) name:kReachabilityChangedNotification object:nil];

    reachability = [Reachability reachabilityForInternetConnection];
    [reachability startNotifier];

    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

    if          (remoteHostStatus == NotReachable)      {NSLog(@"no");      self.hasInet-=NO;   }
    else if     (remoteHostStatus == ReachableViaWiFi)  {NSLog(@"wifi");    self.hasInet-=YES;  }
    else if     (remoteHostStatus == ReachableViaWWAN)  {NSLog(@"cell");    self.hasInet-=YES;  }

}

- (void) handleNetworkChange:(NSNotification *)notice
{
    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

    if          (remoteHostStatus == NotReachable)      {NSLog(@"no");      self.hasInet-=NO;   }
    else if     (remoteHostStatus == ReachableViaWiFi)  {NSLog(@"wifi");    self.hasInet-=YES;  }
    else if     (remoteHostStatus == ReachableViaWWAN)  {NSLog(@"cell");    self.hasInet-=YES;  }

//    if (self.hasInet) {
//        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Net avail" message:@"" delegate:self cancelButtonTitle:OK_EN otherButtonTitles:nil, nil];
//        [alert show];
//    }
}

这篇关于如何从iOS Reachability Class更改网络连接通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆