如何告诉我的应用程序即将变为非活动状态/进入后台状态?

She*_*lam 7 iphone cocoa-touch objective-c nsnotifications multitasking

我假设我需要实现:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(resignActive:)
                                                     name:UIApplicationWillResignActiveNotification
                                                   object:nil];
Run Code Online (Sandbox Code Playgroud)

但我不确定这是否是确定我的应用程序即将离开活动状态的正确通知.

这是取消网络连接以及应用程序终止的好地方吗?

dra*_*ard 11

There are cases where UIApplicationWillResignActiveNotification is sent but the application does not enter the background, for example if a call is received but the user chooses not to answer it.

Use UIApplicationDidEnterBackgroundNotification to be notified when entering the background. Be aware that this will sometimes be sent after UIApplicationWillEnterForegroundNotification if the application is quickly opened again.