使用網路診斷(Using Network Diagnostics)

來源:互聯網
上載者:User

標籤:ios開發   網路診斷   cfnetdiagnostics   network diagnostics   

在許多基於網路的應用中,會發生基於網路的錯誤,這些錯誤與你的應用無關。然而,大多數使用者可能不知道為什麼應用失敗。CFNetDiagnostics API為你提供一種快速而簡單的方法來協助使用者解決網路問題。

如果你的應用使用一個CFStream 對象,然後調用CFNetDiagnosticCreateWithStreams函數建立一個網路診斷引用(CFNetDiagnosticRef) 。CFNetDiagnosticCreateWithStreams有一個分配器,讀取流和寫入流作為參數。如果你的應用只使用讀取流或寫入流,未使用的參數設定為NULL。

如果不存在流,你還可以直接從URL建立一個網路診斷引用。要做到這一點,調用CFNetDiagnosticCreateWithURL 函數,並傳遞一個分配器,URL到CFURLRef。它將返回一個網路診斷引用供你使用。

為了通過網路診斷助手診斷問題,調用CFNetDiagnosticDiagnoseProblemInteractively 函數並傳遞網路診斷引用。清單6-1展示了如何使用CFNetDiagnostics 診斷在運行迴圈上實現的流。

清單6-1 當發生流錯誤時使用CFNetDiagnostics API

 case kCFStreamEventErrorOccurred:        CFNetDiagnosticRef diagRef =            CFNetDiagnosticCreateWithStreams(NULL, stream, NULL);        (void)CFNetDiagnosticDiagnoseProblemInteractively(diagRef);        CFStreamError error = CFReadStreamGetError(stream);        reportError(error);        CFReadStreamClose(stream);        CFRelease(stream);        break;


CFNetworkDiagnostics 同樣可以檢索問題的狀態,而不是使用網路診斷助手。通過調用CFNetDiagnosticCopyNetworkStatusPassively,便可以完成。該函數返回一個常數例如kCFNetDiagnosticConnectionUp 或kCFNetDiagnosticConnectionIndeterminate。


官方原文地址:

https://developer.apple.com/library/ios/documentation/Networking/Conceptual/CFNetwork/UsingNetworkDiagnostics/UsingNetworkDiagnostics.html#//apple_ref/doc/uid/TP30001132-CH7-SW1

使用網路診斷(Using Network Diagnostics)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.