【发布时间】: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