连接状态具有可达性

时间:2012-04-04 00:44:02

标签: ios connection reachability

我在我的项目中使用Reachability,当我尝试获取有关连接类型的信息时,我收到此错误。

*** 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;
    }
}

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。 在做

之前我正在检查if reachability.currentReachabilityStatus() == NotReachable
reachability = Reachability.forInternetConnection()
reachability.startNotifier()

问题是您在启动通知程序之前要求提供状态,_reachabilityRef为零。

确保按所需顺序拨打电话。首先启动通知程序,然后才检查/询问状态。

答案 1 :(得分:0)

检查:

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

在app委托中。 NSParameterAssert在我的情况下导致崩溃