我试图用两种方式监视可重复性。连接到主机并连接到互联网。无论我想显示一个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;
}
}
http://stackoverflow.com/a/2051969/792677 – 2013-03-14 09:26:31