iOS:如何判断应用程序何时挂起?

我想知道什么时候我的应用程序将被暂停? 在一段时间内没有被激活或被用户终止的状态。 我需要这个,因为我需要closures连接的networking套接字。 我想保持连接,而应用程序处于后台状态,虽然。

我该怎么做呢?

谢谢

编辑:这不是一个重复的问题,其他问题想要的是什么时候应用程序不再活动,我想知道该应用程序已被终止。

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

- (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. } 

这个方法会在应用程序终止时被调用(完全closures多任务)。

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

你可以在这个方法中处理你的连接。

您也可以添加通知观察者

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveSuspendNotification:) name:UIApplicationWillResignActiveNotification object:nil]; - (void) receiveSuspendNotification:(NSNotification*)notif { } 

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

如果您的应用程序没有在后台注册运行,那么在接收到UIApplicationDidEnterBackgroundNotification时,您的应用程序将被暂停在RAM中。