2013-04-11 63 views
0

所以,我知道這裏有很多關於實現可達性的帖子,但我找不到任何回答我的具體問題。可達性hostStatus返回false否

我正在實現下面的代碼,但由於某種原因,hostStatus總是返回爲NotReachable。

我的.h文件:

#import <UIKit/UIKit.h> 
#import "Reachability.h" 

@class Reachability; 
@interface ViewController : UIViewController<UINavigationControllerDelegate>{ 

    Reachability* hostReachable; 
    Reachability* internetReachable; 

} 

@property (nonatomic, assign) BOOL wifiReachable; 
@property (nonatomic, assign) BOOL networkReachable; 
@property (nonatomic, assign) BOOL internetUsable; 

我.m文件:

#import "ViewController.h" 
#import "Reachability.h" 
#import "Reachability.m" 

@interface ViewController() 
@end 

@implementation ViewController 
- (void)viewDidLoad{ 
    [super viewDidLoad]; 

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

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

    [internetReachable startNotifier]; 
    [hostReachable startNotifier]; 
    // now patiently wait for the notification 
    [self checkNetworkStatus:kReachabilityChangedNotification]; 

    if(self.internetUsable==TRUE){ 
    //DO STUFF 
    } 
    else{ 
    [self internetAlert]; 
    //DO OTHER STUFF 
    } 
} 


-(void) checkNetworkStatus:(NSNotification *)notice{ 


    // called after network status changes 
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; 
    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus]; 
    NSLog(@"%u", hostStatus); 
    if(internetStatus==NotReachable){ 
    NSLog(@"The internet is down."); 
    self.internetUsable=FALSE; 
    } 
    else{ 
    if(internetStatus==ReachableViaWWAN){ 
     NSLog(@"The internet is working via WWAN."); 
     self.networkReachable=TRUE; 
     self.internetUsable=TRUE; 
    } 
    else if (internetStatus==ReachableViaWiFi) { 
     NSLog(@"The internet is working via WIFI."); 
     self.wifiReachable=TRUE; 
     self.internetUsable=TRUE; 
    } 
    else{ 
     self.networkReachable=FALSE; 
     self.wifiReachable=FALSE; 
     self.internetUsable=FALSE; 
     NSLog(@"The internet is NOT useable."); 
    } 
    } 

    if(self.internetUsable==TRUE) 
    { 

    if(hostStatus==NotReachable) 
    { 
     self.internetUsable=FALSE; 
     NSLog(@"Could not connect to the host"); 
    } 
    } 

} 

我的猜測是,它是進入CheckNetworkStatus方法hostStatus連接已被適當的檢查之前。

任何幫助真的很感謝!

回答

0

你是對的。問題是Reachability調用不同步。方法-checkNetworkStatus:callback,這意味着您不要直接調用它。相反,只要系統發現網絡可達性發生了變化,它就會調用該方法本身。實際上,代碼在實例化對象之後立即檢查網絡狀態,並且很久之後才能從網絡獲得任何答覆。 從-viewDidLoad中刪除-checkNetworkStatus:,你應該得到正確的結果。

(你會得到無論如何編譯器警告,因爲kReachabilityChangedNotificationNSString而不是UINotification#define無論如何 - 選擇並單擊鼠標右鍵,並做「跳轉到」看到這一點 - 它在Reachability.h)。 - 注意 - 確保你打電話[[NSNotificationCenter defaultCenter]removeObserver:self name:kReachabilityChangedNotification object:nil];-dealloc - 如果你忘記了,並且對象被釋放(這會讓我感覺不到),那麼你會得到惱人的崩潰。