2012-11-06 26 views
1

在我的項目中,我使用Apple提供的Reachability類。當沒有互聯網連接時,我顯示一條警告消息。一切工作正常,當我在模擬器上測試它,但是當它在iPad上運行時,當沒有互聯網時,警告消息不會顯示。在模擬器上工作但不在設備上的可達性

我在iOS 5.0上運行代碼。

任何幫助,將不勝感激。

編輯:

-(BOOL)isInternetConnectionPresent{ 

Reachability *objReachability = [Reachability reachabilityForInternetConnection];  
NetworkStatus internetStatus = [objReachability currentReachabilityStatus]; 

if(internetStatus != NotReachable) 
{ 
    return YES; 
} 


    return NO; 
} 

UPDATE:

使用NSLog的調試。似乎WWAN出現了一些問題,即使沒有SIM卡。重新啓動iPad &關閉&再次打開Wi Fi。現在它工作正常。謝謝你們的幫助。

+1

你可能要去如果你添加一些細節得到更好的答案。例如,你可以發佈你的實現代碼。也嘗試做一些調試:至少有一些'NSLog'。 「可達性」當然也適用於設備。我使用了很多應用程序。 –

+0

正如我所說的,它在模擬器中工作正常!! ... 我已編輯我的問題添加代碼。 – footyapps27

+0

你可能只需要清理項目,從設備上刪除應用程序...無論如何:你真的會更好使用觀察員 - 最好的例子之一可以找到[這裏](http://stackoverflow.com/a/3597085/653513) –

回答

1

你必須檢查所有NetworkStatus和交叉檢查設備WiFi連接狀態再次

例子:

// to check if, wifi connected properly in current device. 
- (BOOL)networkCheck { 

    Reachability *wifiReach = [Reachability reachabilityForInternetConnection]; 
    NetworkStatus netStatus = [wifiReach currentReachabilityStatus]; 

    switch (netStatus) 
    { 
     case NotReachable: 
     { 
      NSLog(@"NETWORKCHECK: Not Connected");   
      return NO; 
      break; 
     } 
     case ReachableViaWWAN: 
     { 
      NSLog(@"NETWORKCHECK: Connected Via WWAN"); 
      return NO; 
      break; 
     } 
     case ReachableViaWiFi: 
     { 
      NSLog(@"NETWORKCHECK: Connected Via WiFi"); 
      return YES; 
      break; 
     } 
    } 
    return false; 

} 
+0

我剛剛添加了我的代碼來檢查互聯網..它應該也是這樣做的。 – footyapps27

+0

應用我的答案並檢查NSlogs。它應該說明確的原因,爲什麼有一個連接 –

0

在我的情況下,下面的代碼片段與工作完美iOS 5.在這裏我正在檢查與WIFI的互聯網連接。

- (NSString *)stringFromStatus:(NetworkStatus) status { 
NSString *string; 
switch(status) { 
    case NotReachable: 
     string = @"Not Reachable"; 
     break; 
    case ReachableViaWiFi: 
     string = @"Reachable via WiFi"; 
     break; 
    case ReachableViaWWAN: 
     string = @"Reachable via WWAN"; 
     break; 
    default: 
     string = @"Unknown"; 
     break; 
} 
return string; 

}

------------------------現在下面的代碼,你可以檢查線路。

Reachability *reach =[Reachability reachabilityForLocalWiFi] ; 
NetworkStatus status = [reach currentReachabilityStatus]; 
NSLog(@"%@", [self stringFromStatus: status]); 

if ([[self stringFromStatus: status] isEqualToString: @"Not Reachable"]) 
{ 
    UIAlertView *alert = [[UIAlertView alloc] 
          initWithTitle:@"Connection Failure !" 
          message:@"Your Device is not Connected to any active WIFI Connection." 
          delegate:self 
          cancelButtonTitle:@"OK" 
          otherButtonTitles:nil]; 
    [alert show]; 
} 
else 
{ //connected to internet. 

}

相關問題