如何从iOS可达性类更改网络连接通知?

如何从iOS可达性类更改网络连接通知?,第1张

概述嗨,我想要捕获每当用户在我的应用程序中获取网络连接我已经添加了苹果可达性类和下面是我在我的appDelegate类didFinishLaunchingWithOptions方法中使用的片段, Reachability* reachability = [Reachability reachabilityForInternetConnection]; [reachability sta 嗨,我想要捕获每当用户在我的应用程序中获取网络连接我已经添加了苹果可达性类和下面是我在我的appDelegate类dIDFinishLaunchingWithOptions方法中使用的片段,
Reachability* reachability = [Reachability reachabilityForInternetConnection];        [reachability startNotifIEr];        [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangednotification object:nil];

而我的可达性更改选择器方法如下

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

但是当我关闭我的飞行模式,以及当我在手机中获得网络连接时,我没有收到任何通知.

我错过了什么吗?

解决方法 我使用appdelegate中的变量将当前网络状态存储为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可达性类更改网络连接通知?全部内容,希望文章能够帮你解决如何从iOS可达性类更改网络连接通知?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1095708.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存