9

我想知道我的应用程序何时会暂停?一段时间内不活动或被用户终止的状态。我需要这个,因为我需要关闭一个 Web 套接字的连接。不过,我想在应用程序处于后台状态时保持连接处于活动状态。

我该怎么做呢?

谢谢

4

3 回答 3

4

您还可以添加通知观察者

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(receiveSuspendNotification:)
                                             name:UIApplicationWillResignActiveNotification
                                           object:nil];

- (void) receiveSuspendNotification:(NSNotification*)notif
{
}

方法将被调用,您可以执行所需的任务。

于 2013-12-13T07:27:38.897 回答
-2

如果您的应用程序未注册在后台运行,则在收到 UIApplicationDidEnterBackgroundNotification 时,您的应用程序将暂停在 RAM 中。

于 2013-12-13T09:32:46.020 回答
-3

在您的 AppDelegate.m 文件中,当用户点击主页按钮并且应用程序将转到后台时,将调用此方法(在这里您可以保持连接,但您应该阅读有关后台任务的苹果文档,因为您的连接不能如果应用程序保留在后台,则永远存在。还有其他方法可以使您的应用程序保持最新状态,例如推送通知更新等):

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

当应用程序终止(从多任务处理中完全关闭)时,将调用此方法。

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

您可以在此方法中处理您的连接。

于 2013-12-13T06:10:58.000 回答