2011-11-04 128 views
7

我想在互聯網連接不可用時關閉我的應用程序。當互聯網不可用時關閉應用程序

我檢查,但我如何創建一個警報,然後關閉我的應用程序?

+0

如果您正在考慮將手機作爲偶爾連接的設備,則聽起來有異常行爲。 –

+0

我已經注意到應用程序可以做到這一點(單詞與朋友,爲一)。我個人不喜歡它。 –

+0

我的應用程序使用來自Web服務器的數據,如果沒有競爭,它不起作用,這就是爲什麼我想要這種行爲。 – DaSilva

回答

19

你不應該強制關閉應用程序,以終止應用程序的標準方法是按home鍵(或使用多任務欄)

不要退出編程


永遠不要以編程方式退出iOS應用程序,因爲人們傾向於將其解釋爲崩潰。但是,如果外部環境阻止您的應用程序按預期運行,您需要告知用戶有關情況並解釋他們可以對此做些什麼。 根據應用故障的嚴重程度,您有兩個選擇 。

顯示一個有吸引力的屏幕,描述問題並提出 更正。屏幕提供的反饋可以讓用戶確信,您的應用程序沒有任何問題。它讓用戶控制, 讓他們決定是否要採取糾正措施和 繼續使用您的應用程序或按Home鍵,然後打開一個 不同的應用

如果僅僅是一些應用程序的功能無法正常運作,顯示 當人們激活該功能時,屏幕或警報。顯示 僅當人們嘗試訪問不是 功能的功能時纔會顯示警報。

Source

+2

這是真的。如果您的應用程序無法在沒有活動連接的情況下工作,您應該顯示一些靜態UI,向用戶解釋該UI。 – tjarratt

6

您的應用程序不應該關閉自己。 iOS沒有退出應用程序的概念。您可以通知用戶沒有互聯網連接,並提供等待屏幕或其他內容,顯示您的應用程序在互聯網連接可用之前無用,但應用程序應繼續運行,直到操作系統決定關閉您的應用程序。

5

根據八月份的ANS here

"On the iPhone there is no concept of quitting an app. The only action that should cause an app to quit is touching the Home button on the phone, and that's not something developers have access to. 

According to Apple, your app should not terminate on its own. Since the user did not hit the Home button, any return to the Home screen gives the user the impression that your app crashed. This is confusing, non-standard behavior and should be avoided." 


但如果你仍然想退出你的應用程序,然後 有兩個命令的退出程序。

1.exit(0) 

2.[[NSThread mainThread] exit] 
5

,而不是關閉它,可以考慮通過彈出窗口的方式說明情況給用戶。首先,請下載Reachability from Apple

將Reachability.h,.m,delegates類添加到您的項目中。然後在你的。M級進口可達

#import "Reachability.h" 

而在viewWillAppear中或當您應顯示警告:

//Connection check 
    Reachability *reach = [Reachability reachabilityForInternetConnection]; 
    NetworkStatus netStatus = [reach currentReachabilityStatus];  
    if (netStatus == NotReachable) 
    { 
     UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:@"Explain the situation to the user" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil]; 
     [alert show]; 
     [alert release];  

    }  
    else { 
    //other actions. 
    } 

正如其他人在我面前說。

相關問題