gpt4 book ai didi

ios - 当可达性连接丢失时显示警报 View

转载 作者:行者123 更新时间:2023-11-29 04:04:59 25 4
gpt4 key购买 nike

我正在尝试通过两种方式监控 Rechability。连接到主机和连接到互联网。对于任一我尝试显示 UIAlertView。目前效果很好。

我想要实现的是,如果用户单击警报 View 按钮以再次检查可达性,但我不确定如何执行此操作。理想情况下,我不希望警报 View 关闭并再次显示,而是保持显示状态并再次请求可达性检查。这可能吗?

这是我当前的 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;
}
}

最佳答案

基本上,您可以做的是禁用 UIAlertView 按钮,直到您再次可以访问网络。

[alert addButtonWithTitle:@"OK"];
UIButton *submitButton = [[alert subviews] lastObject];
[submitButton setEnabled: … ];

连接恢复后,您只需再次执行相同的操作:)

关于ios - 当可达性连接丢失时显示警报 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15405325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com