1

我在我的应用程序中使用可达性 v2.0。

我有一个有效的互联网连接,并且能够在网络浏览器中浏览谷歌页面。当我尝试在我的应用程序中测试可达性状态时,它总是显示“主机不可访问”。

    NSString *host = @"http://www.google.co.in";

    NSLog(@"host : %@", host);
    Reachability *hostReach = [Reachability reachabilityWithHostName:host];
    if([hostReach currentReachabilityStatus] == NotReachable) {
        NSLog(@"Host is not Reachable");
    } else {
        NSLog(@"Host is reachable");
    }

上面的代码有什么问题??

4

2 回答 2

3

问题是reachabilityWithHostname 方法需要一个主机名,而不是您定义的URL。

您需要将主机定义为:-

NSString *host = @"www.google.co.in";
于 2011-10-30T23:47:43.223 回答
0

尝试在任何需要互联网连接的功能上实现它!

#import "testConnectionViewController.h"
#import "Reachability.h"

@implementation testConnectionViewController




- (void)doWeHasInternets{

  UIAlertView *errorView;
  Reachability *r = [Reachability reachabilityWithHostName:@"m.google.com"];
  NetworkStatus theInternets = [r currentReachabilityStatus];


  if(theInternets == NotReachable) {
    errorView = [[UIAlertView alloc]
                 initWithTitle: @"Network Error"
                 message: @"The internets are down, everyone run for your lives!"
                 delegate: self
                 cancelButtonTitle: @"Nooooooo!" otherButtonTitles: nil];    
  }
  else
  {
    errorView = [[UIAlertView alloc]
                 initWithTitle: @"Whew!"
                 message: @"Relax, the internets are here."
                 delegate: self
                 cancelButtonTitle: @"Yay!" otherButtonTitles: nil];    
  }

  [errorView show];
  [errorView autorelease];
}

@end
于 2011-06-07T08:36:05.773 回答