2012-04-04 29 views
0

我在我的項目上使用可達性,當我試圖獲取有關連接類型的信息時,我得到此錯誤。連接狀態與可達性

*** Assertion failure in -[Reachability currentReachabilityStatus],/myProjectPath/Reachability.m:530 
2012-04-03 21:25:45.619 Traffic[7862:11603] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'currentNetworkStatus called with NULL reachabilityRef' 

我不知道我的代碼有什麼問題。我把相同的代碼放在ReachabilityAppDelegate中來獲取連接狀態。

我的代碼:

// get type of user connection 
NSString* statusString= @"NA"; 

Reachability *curReach = [[Reachability alloc] init]; 
NetworkStatus netStatus = [curReach currentReachabilityStatus]; 
BOOL connectionRequired= [curReach connectionRequired]; 


switch (netStatus) 
{ 
    case NotReachable: 
    { 
     statusString = @"NA"; 
     //Minor interface detail- connectionRequired may return yes, even when the host is unreachable. We cover that up here... 
     connectionRequired= NO; 
     break; 
    } 

    case ReachableViaWWAN: 
    { 
     statusString = @"WWAN"; 
     break; 
    } 
    case ReachableViaWiFi: 
    { 
     statusString= @"WIFI"; 
     break; 
    } 
} 

回答

0

檢查:

//Called by Reachability whenever status changes. 
- (void) reachabilityChanged: (NSNotification*)note 
{ 
    Reachability* curReach = [note object]; 
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]); 
    [self updateInterfaceWithReachability: curReach]; 
} 
在應用程序委託

。 NSParameterAssert導致我的情況下崩潰

0

我有同樣的問題。 我是做

reachability = Reachability.forInternetConnection() 
reachability.startNotifier() 

的問題之前檢查if reachability.currentReachabilityStatus() == NotReachable是你問的狀態啓動前通知和_reachabilityRef是零。

確保您按照需要的順序進行呼叫。首先啓動通知程序,然後才檢查/詢問狀態。