iOS/iPhone 可达性 - 如何使用 Reachability.m/.h 仅检查互联网丢失/无法可达的情况
目前,我正在使用 applereachability.m/.h 的类,它可以工作,但它会通知我任何更改,因为我只想在网络无法访问时通知用户。目前,如果我有互联网连接,然后松开网络,它会告诉我。但是,当您重新连接到网络时,它也会告诉我,这是我不想要的。我希望它只在网络丢失/无网络时告诉我。
我相信这与调用有关:
- (void)viewWillAppear:(BOOL)animated
{
// check for internet connection
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(checkNetworkStatus:)
name:kReachabilityChangedNotification
object:nil];
internetReachable = [[Reachability
reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
// check if a pathway to a random host exists
hostReachable = [[Reachability reachabilityWithHostName:
@"www.google.ca"] retain];
[hostReachable startNotifier];
// now patiently wait for the notification
}
当调用 -[NSNotificationCenter addObserver:selector:name:object:]
时,该名称是否还有其他功能,而不是字面上的名称?这是我第一次使用 NSNotificationCenter,所以我对这件事不太熟悉。
编辑:
这是我的 checkNetworkStatus 函数:(问题是我收到“NotReachable”,因为网络连接正在恢复并且 NSAlert 多次关闭)
- (void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Network Failed" message:@"Please check your connection and try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil ];
[alert show];
NSLog(@"The internet is down.");
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
NSLog(@"A gateway to the host server is down.");
break;
}
case ReachableViaWiFi:
{
NSLog(@"A gateway to the host server is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
NSLog(@"A gateway to the host server is working via WWAN.");
break;
}
}
}
Currently i am using the class by apple reachability.m/.h and it works, except it notifies me for any change, where as i would like to only notify the user if the network is not reachable. Currently if i have a internet connection and then loose the network it tells me. However when you reconnect to the network it also tells me, which i do not want. I want it to only tell me when there is a loss/no network.
I believe it has something to do with the call:
- (void)viewWillAppear:(BOOL)animated
{
// check for internet connection
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(checkNetworkStatus:)
name:kReachabilityChangedNotification
object:nil];
internetReachable = [[Reachability
reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
// check if a pathway to a random host exists
hostReachable = [[Reachability reachabilityWithHostName:
@"www.google.ca"] retain];
[hostReachable startNotifier];
// now patiently wait for the notification
}
when calling -[NSNotificationCenter addObserver:selector:name:object:]
, does the name have any other function then being literally a name? this is my first time using NSNotificationCenter so i am not well versed in this matter.
EDIT:
Here is my checkNetworkStatus function: (The problem is i am getting "NotReachable" as the network connection is coming back and NSAlert goes off multiple times)
- (void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Network Failed" message:@"Please check your connection and try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil ];
[alert show];
NSLog(@"The internet is down.");
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
NSLog(@"A gateway to the host server is down.");
break;
}
case ReachableViaWiFi:
{
NSLog(@"A gateway to the host server is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
NSLog(@"A gateway to the host server is working via WWAN.");
break;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
当状态发生变化时,可达性将发送通知,但您如何处理该通知完全取决于您。如果您不想告诉用户网络已恢复,则不必这样做。
NSNotificationCenter 方法中的“name”参数指示您要订阅的通知。当对象发布通知时,它会使用特定的名称。
Reachability will send a notification when the status has changed, but what you do with that notification is entirely up to you. If you don't want to tell the user that the network is back, you don't have to.
The "name" parameter in the NSNotificationCenter method indicates what notification you are subscribing to. When an object posts a notification, it does so with a particular name.
如果您仅将 www.hostname.com 替换为 IP 地址,则只会警报一次,而不是多次。
If you replace the www.hostname.com with just an IP address, it will only alert once instead of multiple times.
我刚刚开始研究可达性,希望我的发现对您有用。
关于重新连接时的多个“无法访问”,是否可以链接到 这个?在这里,发帖者提出了远程主机“可访问”的定义。我猜重新连接包时无法成功完成?
另一种可能性位于 Reachability Readme.txt 中
重要提示:Reachability 必须使用 DNS 来解析之前的主机名
可以确定该主机的可达性,这可能需要一些时间
某些网络连接。因此,API 将返回
在名称解析完成之前无法访问。这种延迟可能是
在某些网络的界面中可见。
也许直接给它IP看看有没有帮助?
I just started playing around with Reachability and hopefully what I discovered is of use to you.
With regards to multiple 'Not Reachable' while reconnecting, could it be linked to this? Here the poster brought up the definition of 'reachable' for a remote host. I'm guessing while reconnecting the package is not able to go through successfully?
Another possibility is in Reachability Readme.txt
IMPORTANT: Reachability must use DNS to resolve the host name before it
can determine the Reachability of that host, and this may take time on
certain network connections. Because of this, the API will return
NotReachable until name resolution has completed. This delay may be
visible in the interface on some networks.
Maybe give it the IP directly and see if it helps?
使用Reachability 2.2,可以通过添加
before
来解决这个问题。
runmad 在这里回答了这个问题:
https://stackoverflow.com/a/2157858/623260
With Reachability 2.2, you can add
before
to solve this problem.
runmad answered this problem here:
https://stackoverflow.com/a/2157858/623260
我将实现整个 Reachability 类,根据需要将其绑定到您的代码中,并删除或注释掉部分 Reachability 。只需将您不想收到通知的内容一一删除即可。显然,你需要很好地理解 obj-c、Reachability 类、通知等,但这是可以做到的。
I would implement the whole Reachability class, tie it into your code as necessary and remove or comment out parts of Reachability. Just remove, one by one, the things you do not want to be notified of. Obviously, you need a good understanding of obj-c, the Reachability class, notifications, etc., but it can be done.
您可以做的一件事是在回调中处理通知时取消订阅更改的通知 NSNotificationCenter removeObserver...。返回之前添加回观察者。
One thing you can do is unsubscribe to the changed notification NSNotificationCenter removeObserver... while you're processing one in the callback. Add back the observer before returning.
我们可以使用此代码检查 rechability
添加类 Reachability.h
Reachability.m
并通过 appdel 类中的直接调用方法使用
We can check rechability using this code
add class Reachability.h
Reachability.m
and use via direct calling method in appdel class using