【问题标题】:Connection Status with reachability具有可达性的连接状态
【发布时间】:2012-04-04 00:44:02
【问题描述】:

我在我的项目中使用可达性,当我尝试获取有关连接类型的信息时,我收到了这个错误。

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

【问题讨论】:

    标签: ios connection reachability


    【解决方案1】:

    我遇到了同样的问题。 在做之前我正在检查if reachability.currentReachabilityStatus() == NotReachable

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

    问题是您在启动通知程序之前询问状态,而_reachabilityRef 为 nil。

    确保您按照需要的顺序拨打电话。首先启动通知程序,然后仅在检查/询问状态之后。

    【讨论】:

      【解决方案2】:

      检查:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-17
        • 1970-01-01
        • 2014-09-22
        相关资源
        最近更新 更多