您應該使用由Apple撰寫的Reachability代碼。
Please Go Through This Link For Downloading the Reachability Files.
對於使用該代碼需要導入SystemConfiguration框架。
按照目標 - > BuildPhase-> LinkBinaryWithLibraries->點擊「+」 - >選擇SystemConfiguration。
然後在ViewController中導入#import「Reachability.h」標題。
然後隨便寫幾行代碼就在瀏覽到anotherView
Reachability *reach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [reach currentReachabilityStatus];
if (netStatus == NotReachable) {
NSLog(@"No internet connection!");
UIAlertView *information = [[UIAlertView alloc] initWithTitle:@"Server Connection is not available" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[information show];
[information release];
}
else {
//Write your logic here Like As navigating to anotherview
}
看起來不錯,我想,但我使用ARC和Xcode 4.3。看起來蘋果的可達性與Xcode 4.3不兼容。我已經爲Reachability.m禁用了ARC,但仍然出現錯誤:命令/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang失敗,退出代碼爲1 – halloway4b 2012-02-28 10:35:52
@ halloway4b閱讀此項仔細http://stackoverflow.com/questions/7560239/xcode-command-developer-usr-bin-clang-failed-with-exit-code-1 – Kamarshad 2012-02-28 10:40:40
嗯...我想我沒有得到它,但感謝鏈接。我剛剛通過你展示的例子,並得到了這個。請幫忙。鏗鏘:錯誤:沒有這樣的文件或目錄:'add' – halloway4b 2012-02-28 10:54:19