macOS 開發- 使用 CFNetDiagnosticCopyNetworkStatusPassively 判斷網路連線狀態
阿新 • • 發佈:2018-12-08
文章目錄
判斷程式碼
- (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);