這種情況下(的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;
}
}
}
你能連接到其他應用程序互聯網?你有連接權嗎? – Bergasms 2013-02-14 22:06:36
更改'[可達性reachabilityWithHostName:@ 「www.google.com」];'到'[可達性reachabilityWithHostName:@ 「http://www.google.com」];'和檢查。 – iDev 2013-02-14 22:09:03
www.google.com是正確的。使用http://從不起作用。在我工作的應用程序中發現了一些代碼,它具有http://並且從未將主機顯示爲可訪問。當更改爲www.google.com時,主機變得可以訪問,但直到網絡出現故障(計算機進入睡眠狀態)。此後,每當網絡再次出現時,主機總是停機。必須重新啓動模擬器以使其再次工作。我認爲模擬器(Xcode 6.2)有內部問題,我必須重新啓動過程才能在喚醒後重新獲得互聯網連接。直到那時總是出現錯誤1005。在真實設備上沒有問題。 – ghr 2015-07-25 03:17:21