【问题标题】:Reachability - Host appears down while Internet is reconnecting可达性 - Internet 重新连接时主机出现故障
【发布时间】:2012-08-31 03:30:22
【问题描述】:

我使用this问题来检查互联网连接(WiFi \ 3G)是否处于活动状态。

它工作得很好,但有一个小问题。

当我关闭互联网时,应用程序会发出 Internet 已关闭的警报;但是当我再次打开它时,直到连接肯定是打开的,它说主机已经关闭,而实际上是打开的,事实上,过了一会儿,它记录了它是打开的。

我想知道我该怎么做才能让该消息仅在服务器实际关闭时出现,而不是在重新连接到互联网时出现!

这是我的代码

-(void) checkNetworkStatus:(NSNotification *)notice
{
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    if (internetStatus == NotReachable){
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Reloading Failed" message: @Seems like you're offline! \n Some features, such as updating contents and downloading screenshots won't be available while you're offline.\nNavigate to Settings > Wi-Fi to connect to the Internet and enjoy those features!") delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
        [alert show];
        [alert release];
        NSLog(@"offline");
    }
    else if (internetStatus == ReachableViaWiFi || internetStatus == ReachableViaWWAN){
        NSLog(@"online");
        if (hostStatus == NotReachable)
        {   NSLog(@"Server offline");
            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Reloading Failed" message:@"Seems like there's a communication problem with the remote network. \n Please try again in a while!" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
            [alert show];
            [alert release];
        }
        else if (hostStatus == ReachableViaWWAN || hostStatus == ReachableViaWiFi){
            NSLog(@"server online");
           //download data from remote server 
        }
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
    internetReachable = [[Reachability reachabilityForInternetConnection] retain];
    [internetReachable startNotifier];
    hostReachable = [[Reachability reachabilityWithHostName: @"www.mywebsite.com"] retain];
    [hostReachable startNotifier];

有什么想法吗?

【问题讨论】:

  • 您使用的是什么版本的可达性?
  • 可达性版本 2.2
  • 要确认一个特定的主机实际上已经启动并且您可以连接到它,您需要打开一个到它的连接(NSURLConnection 等)。可达性仅告诉您设备的网络连接是什么。

标签: objective-c ios cocoa-touch reachability


【解决方案1】:

Reachability 仅用于检查network is reachable 是否存在,不会告诉您服务器是否已关闭。您需要结合使用ReachabilityNSURLConnection 或其他第三方库来检查HTTP 状态200 和其他有效状态。您将使用Reachability 显示设备未连接的消息,一旦连接,您将使用 Web 请求通知用户服务器是否在线。为您的设备重新打开互联网后,您无法解决连接延迟问题,因为打开收音机并连接到手机网络或 wifi 需要时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 2015-05-24
    相关资源
    最近更新 更多