我试图以两种方式监控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;
}
}
答案 0 :(得分:1)
基本上,您可以执行的操作是禁用UIAlertView按钮,直到您再次访问网络。
[alert addButtonWithTitle:@"OK"];
UIButton *submitButton = [[alert subviews] lastObject];
[submitButton setEnabled: … ];
在你的Connection回来之后,你再次做同样的事情:)
答案 1 :(得分:0)
您可以使用自定义AlertView来实现此方法
-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
if (![self connected])
return;
[super dismissWithClickedButtonIndex:buttonIndex animated:animated];
}
警报启动时,您可以通过此方法自行检查连接性
- (BOOL)connected
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
最后看if([self connected])
然后关闭按钮