2012-02-28 27 views
1

我正在使用導航控制器和prepareforsegue使用Storyboard。 我有兩個UITableViews。如果你點擊第一個表格中的一行,你會看到第二個表格。 第二個表格從plist中選取其數據,具體取決於您在第一個表格中單擊的行。只要有互聯網連接,這工作正常。如果沒有互聯網連接,它會崩潰。從服務器加載Plist for tableview前檢查Internet

現在我想檢查第二張表加載前是否有互聯網連接。如果沒有互聯網連接,我想顯示一個UIAlertView。

我想用NSURLConnection做到這一點,但我不知道在哪裏實現代碼。 我會把它放在prepareforsegue第一張表的.m或第二張表的.m中嗎?

謝謝。

回答

4

您應該使用由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  
} 
+0

看起來不錯,我想,但我使用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

+0

@ halloway4b閱讀此項仔細http://stackoverflow.com/questions/7560239/xcode-command-developer-usr-bin-clang-failed-with-exit-code-1 – Kamarshad 2012-02-28 10:40:40

+0

嗯...我想我沒有得到它,但感謝鏈接。我剛剛通過你展示的例子,並得到了這個。請幫忙。鏗鏘:錯誤:沒有這樣的文件或目錄:'add' – halloway4b 2012-02-28 10:54:19