2011-11-04 129 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. 
    } 

正如其他人在我面前说。

相关问题