![](/img/trans.png)
[英]How to detect loss of internet reachability when wifi source loses connection?
[英]can not detect internet connection with Reachability reachabilityForInternetConnection
我有個問題。 我正在使用可達性的reachabilityForInternetConnection方法來檢測互聯網可用性,但我沒有獲得連接狀態而不是互聯網狀態。 我的意思是,如果我打開我的Wifi連接,該方法給了我正確的指示,我沒有連接但是如果wifi打開並且互聯網連接不起作用,它似乎不起作用。 任何想法?
最好的祝福
可達性僅可用於檢測iPhone是否連接到互聯網的網關。 網關的背后是什么,它不會告訴你。 如果局域網可以訪問但你沒有退出互聯網怎么辦? iPhone如何猜測它所看到的(局域網)不是整個互聯網?
你應該向一個真實的網站提出一些真正的要求。 如果失敗,則連接到Internet時會出現問題,並且通過可達性結果,您甚至可以了解問題所在。 最簡單的方法是使用NSUrlRequest向http://www.google.com提出請求。 (如果谷歌死了,你可能會認為有更大的問題那么你的應用程序的連接:)
我在我的應用中使用它:
// Check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachable = [Reachability reachabilityForInternetConnection];
[internetReachable startNotifier];
// Check if a pathway to a random host exists
hostReachable = [Reachability reachabilityWithHostName: @"www.apple.com"];
[hostReachable startNotifier];
和:
- (void) checkNetworkStatus:(NSNotification *)notice
{
// Called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
// Case: No internet
case NotReachable:
{
internetActive = NO;
// Switch to the NoConnection page
NoConnectionViewController *notConnected = [[NoConnectionViewController alloc] initWithNibName:@"NoConnectionViewController" bundle:[NSBundle mainBundle]];
notConnected.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:notConnected animated:NO];
break;
}
case ReachableViaWiFi:
{
internetActive = YES;
break;
}
case ReachableViaWWAN:
{
internetActive = YES;
break;
}
}
// Check if the host site is online
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
hostActive = NO;
break;
}
case ReachableViaWiFi:
{
hostActive = YES;
break;
}
case ReachableViaWWAN:
{
hostActive = YES;
break;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.