2013-02-14 43 views
1

這種情況下(的NSLog(@「網關主機服務器已關閉。」); 總是從某些原因運行可達主機不可達,不管我用哪臺主機

我使用的是蘋果的可達性。一流的幕後。 我試圖插入其他主機,但沒有運氣,請幫助。

在此先感謝。

下面是代碼

@implementation ConnectionManager 
@synthesize internetActive, hostActive; 

-(id)init { 
self = [super init]; 
if(self) { 

} 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:@"NetworkReachabilityChangedNotification" object:nil]; 

internetReachable = [Reachability reachabilityForInternetConnection]; 
[internetReachable startNotifier]; 

hostReachable = [Reachability reachabilityWithHostName:@"www.google.com"]; 
[hostReachable startNotifier]; 



return self; 
} 

- (void) checkNetworkStatus:(NSNotification *)notice 
{ 
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; 
switch (internetStatus) 

{ 
    case NotReachable: 
    { 
     NSLog(@"The internet is down."); 
     self.internetActive = NO; 

     break; 

    } 
    case ReachableViaWiFi: 
    { 
     NSLog(@"The internet is working via WIFI."); 
     self.internetActive = YES; 

     break; 

    } 
    case ReachableViaWWAN: 
    { 
     NSLog(@"The internet is working via WWAN."); 
     self.internetActive = YES; 

     break; 

    } 
} 

NetworkStatus hostStatus = [hostReachable currentReachabilityStatus]; 
switch (hostStatus) 

{ 
    case NotReachable: 
    { 
     NSLog(@"A gateway to the host server is down."); 
     self.hostActive = NO; 

     break; 

    } 
    case ReachableViaWiFi: 
    { 
     NSLog(@"A gateway to the host server is working via WIFI."); 
     self.hostActive = YES; 

     break; 

    } 
    case ReachableViaWWAN: 
    { 
     NSLog(@"A gateway to the host server is working via WWAN."); 
     self.hostActive = YES; 

     break; 

    } 
} 

} 
+0

你能連接到其他應用程序互聯網?你有連接權嗎? – Bergasms 2013-02-14 22:06:36

+2

更改'[可達性reachabilityWithHostName:@ 「www.google.com」];'到'[可達性reachabilityWithHostName:@ 「http://www.google.com」];'和檢查。 – iDev 2013-02-14 22:09:03

+0

www.google.com是正確的。使用http://從不起作用。在我工作的應用程序中發現了一些代碼,它具有http://並且從未將主機顯示爲可訪問。當更改爲www.google.com時,主機變得可以訪問,但直到網絡出現故障(計算機進入睡眠狀態)。此後,每當網絡再次出現時,主機總是停機。必須重新啓動模擬器以使其再次工作。我認爲模擬器(Xcode 6.2)有內部問題,我必須重新啓動過程才能在喚醒後重新獲得互聯網連接。直到那時總是出現錯誤1005。在真實設備上沒有問題。 – ghr 2015-07-25 03:17:21

回答

0

既然你沒有提到關於internetReachable顯示了同樣的錯誤,我假設你是能夠連接到互聯網和部件工作正常。對於hostReachable部分,你可以試着去改變它爲:

變化

[Reachability reachabilityWithHostName:@"www.google.com"]; 

[Reachability reachabilityWithHostName:@"http://www.google.com"]; 
+0

是的,你是對的互聯網部分工作正常,無論如何,我已經嘗試與「http://」,並沒有同樣的事情。 但無論如何謝謝你。 – Blacky 2013-02-15 09:29:30

+0

單靠google.com'呢?那麼它工作嗎?我剛剛在我工作過的一個早期項目中進行了檢查,並且僅僅使用了這樣的東西。 – iDev 2013-02-15 09:40:49

+0

我想盡辦法,我能想到的,我甚至嘗試 - (BOOL)checkResourceIsReachableAndReturnError:(NSError **)錯誤 它返回 「NO」,以及..:\ – Blacky 2013-02-15 10:13:01