macOS 开发- 使用 CFNetDiagnosticCopyNetworkStatusPassively 判断网络连接状态

版权声明:本文为博主原创文章,转载请附上本文链接地址。from : https://blog.csdn.net/lovechris00 https://blog.csdn.net/lovechris00/article/details/84870092


判断代码

- (BOOL)checkReachability{

  CFNetDiagnosticRef dReference;
  dReference = CFNetDiagnosticCreateWithURL (NULL, (__bridge CFURLRef)[NSURL URLWithString:@"https://www.baidu.com"]);

  CFNetDiagnosticStatus status;
  status = CFNetDiagnosticCopyNetworkStatusPassively (dReference, NULL);

  CFRelease (dReference);

  if ( status == kCFNetDiagnosticConnectionUp ){
  NSLog(@"连接正常");
  return YES;
  }else{
  NSLog(@"连接失败");
  return NO;
  }
}

CFNetDiagnosticStatus 枚举

typedef CF_ENUM(int, CFNetDiagnosticStatusValues) {

  /*
  * There is no status, but no error has occured
  */
  kCFNetDiagnosticNoErr = 0,  //没有状态返回,也没有报错

  /*
  * An error occured that prevented the call from completing
  */
  kCFNetDiagnosticErr = -66560L,  //发生问题

  /*
  * The connection appears to be working
  */
  kCFNetDiagnosticConnectionUp = -66559L, //连接正常
  kCFNetDiagnosticConnectionIndeterminate = -66558L,  //不确定的

  /*
  * The connection does not appear to be working
  */
  kCFNetDiagnosticConnectionDown = -66557L  //连接失败

} CF_DEPRECATED(10_4, 10_13, 2_0, 11_0);

猜你喜欢

转载自blog.csdn.net/lovechris00/article/details/84870092