作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在使用 Reachability
类来检查我何时获得 Internet 连接以及何时出现故障。
这是我的代码:
IN VIEW DID LOAD:
internetReachable = [Reachability reachabilityForInternetConnection];
[internetReachable startNotifier];
// check if a pathway to a random host exists
hostReachable = [Reachability reachabilityWithHostname:@"www.google.com"];
[hostReachable startNotifier];
Then the notification method
-(void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
NSLog(@"The internet is down. IN AGENDA");
self.internetActive = NO;
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.IN AGENDA");
self.internetActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.IN AGENDA");
self.internetActive = YES;
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
NSLog(@"A gateway to the host server is down.IN AGENDA");
self.hostActive = NO;
break;
}
case ReachableViaWiFi:
{
NSLog(@"A gateway to the host server is working via WIFI.IN AGENDA");
self.hostActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@"A gateway to the host server is working via WWAN.IN AGENDA");
self.hostActive = YES;
break;
}
}
}
And finally how I add en remove the observer
-(void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
}
-(void)viewDidDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
}
问题
看起来 viewcontroller 多次收到通知。这是我的 NSLOG
2014-01-04 10:32:16.299 Adsolut[10009:333b] CALLED reachabilityChanged
2014-01-04 10:32:16.299 Adsolut[10009:470b] CALLED reachabilityChanged
2014-01-04 10:32:16.300 Adsolut[10009:70b] The internet is down. IN AGENDA
2014-01-04 10:32:16.301 Adsolut[10009:70b] A gateway to the host server is down.IN AGENDA
2014-01-04 10:32:16.301 Adsolut[10009:70b] The internet is down. IN AGENDA
2014-01-04 10:32:16.302 Adsolut[10009:70b] A gateway to the host server is down.IN AGENDA
2014-01-04 10:32:16.313 Adsolut[10009:470b] CALLED reachabilityChanged
2014-01-04 10:32:16.313 Adsolut[10009:4a03] CALLED reachabilityChanged
2014-01-04 10:32:16.314 Adsolut[10009:70b] The internet is working via WIFI.IN AGENDA
2014-01-04 10:32:16.314 Adsolut[10009:333b] CALLED reachabilityChanged
2014-01-04 10:32:16.315 Adsolut[10009:70b] A gateway to the host server is down.IN AGENDA
2014-01-04 10:32:16.314 Adsolut[10009:f63] CALLED reachabilityChanged
2014-01-04 10:32:16.315 Adsolut[10009:70b] The internet is working via WIFI.IN AGENDA
2014-01-04 10:32:16.316 Adsolut[10009:70b] A gateway to the host server is down.IN AGENDA
2014-01-04 10:32:16.316 Adsolut[10009:70b] The internet is working via WIFI.IN AGENDA
2014-01-04 10:32:16.317 Adsolut[10009:70b] A gateway to the host server is down.IN AGENDA
2014-01-04 10:32:16.317 Adsolut[10009:70b] The internet is working via WIFI.IN AGENDA
2014-01-04 10:32:16.318 Adsolut[10009:70b] A gateway to the host server is down.IN AGENDA
有人可以帮忙吗?
最佳答案
仅在appdelegate didFinishLaunchingWithOptions中添加reachability通知,并在appDelegate中的bool变量isInternetAvailable中设置reachability的值
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(checkNetworkStatus:)
name:kReachabilityChangedNotification object:nil];
}
- (void)checkNetworkStatus:(NSNotification *)notice {
// called after network status changes
NetworkStatus internetStatus = [self.internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
//#######NSLog(@"The internet is down.");
self.isInternetAvailable = FALSE;
break;
}
case ReachableViaWiFi:
{
//#######NSLog(@"The internet is working via WIFI");
self.isInternetAvailable = TRUE;
break;
}
case ReachableViaWWAN:
{
//#######NSLog(@"The internet is working via WWAN!");
self.isInternetAvailable = TRUE;
break;
}
default:
{
//#######NSLog(@"The internet is working via mobile SIM!");
self.isInternetAvailable = FALSE;
break;
}
}
}
关于ios - kReachabilityChangedNotification 被多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20919244/
iOS 12 版本不适用于 kReachabilityChangedNotification。 (网络变更通知。 无无障碍技术) 但 iOS 8 版本运行良好。 发生了什么? Xcode 版本为 10
我正在使用 Reachability 类来检查我何时获得 Internet 连接以及何时出现故障。 这是我的代码: IN VIEW DID LOAD: internetRe
我是一名优秀的程序员,十分优秀!