我试图以两种方式监控 Rechability。连接到主机并连接到 Internet。对于任何一个我都在尝试显示 UIAlertView。目前这工作正常。
我想要实现的是,如果用户单击警报视图按钮以再次检查可达性,但我不确定如何执行此操作。理想情况下,我不希望 alertview 再次关闭并显示,而是保持显示并再次请求可达性检查。这可能吗?
这是我当前的 appdelegate imp 代码,在 appdelegate 中使用,因为我想随时检查连接。
- (void)monitorReachability {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:ReachabilityChangedNotification object:nil];
self.hostReach = [Reachability reachabilityWithHostName: @"api.parse.com"];
[self.hostReach startNotifier];
self.internetReach = [Reachability reachabilityForInternetConnection];
[self.internetReach startNotifier];
}
//Called by Reachability whenever status changes.
- (void)reachabilityChanged:(NSNotification* )note {
NetworkStatus internetStatus = [self.internetReach currentReachabilityStatus];
switch (internetStatus) {
case NotReachable:
{
isReachable=NO;
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.");
isReachable=YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
isReachable=YES;
break;
}
}
NetworkStatus hostStatus = [self.hostReach currentReachabilityStatus];
switch (hostStatus) {
case ReachableViaWWAN:
{
hostActive=YES;
break;
}
case ReachableViaWiFi:
{
hostActive=YES;
break;
}
case NotReachable:
{
hostActive=NO;
break;
}
}
if (hostActive == YES && isReachable == YES) {
connectionNotReachable.hidden = YES;
}
if (isReachable == NO) {
NSLog(@"Internet connection lost");\
connectionNotReachable = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"connectionNotReachableTitle", @"Title for alert view - connection Not Reachable") message:NSLocalizedString(@"connectionNotReachableMessage", @"Message for alert view - connection Not Reachable") delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"connectionNotReachableTryAgain", @"Try again button for alert view - connection Not Reachable"), nil];
[connectionNotReachable show];
return;
}
if (hostActive == NO) {
NSLog(@"Host not active, internet connection");
UIAlertView *hostNotReachable = [[UIAlertView alloc] initWithTitle:@"Host Not Reachable" message:@"No Host" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[hostNotReachable show];
return;
}
}