如何模拟applicationWillResignActive消息?
如何模拟 applicationWillResignActive
被调用?
锁定屏幕、进入主菜单、模拟电话——似乎都没有帮助。
如果我预计事情不会发生,请让我告诉您更多信息:我订阅此消息,当发生这种情况时,希望将通知发送到如下所列的方法:
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillResignActive:)
name: UIApplicationWillResignActiveNotification object:app];
How can one simulate applicationWillResignActive
to be called?
Locking screen, going to main menu, simulating phone call - none seemed to have helped.
In case I expect things that won't happen, let me tell you more: I subscribe to this message and when that happens hope that notification is sent to a method as listed below:
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillResignActive:)
name: UIApplicationWillResignActiveNotification object:app];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的代码是有效的,它对我有用。锁定屏幕或点击主页按钮将导致发布此通知。
*(需要注意的是,如果您的设备不支持多任务处理,或者您在
*info.plist
应用程序不在后台运行”属性设置为 yes > 在这种情况下,它将直接进入“UIApplicationWillTerminateNotification
”通知)因此,除非有两种可能性:
1)您的
addObserver
代码没有被调用, IE。这是用了错误的方法。要测试,请尝试以下操作:
2)观察者方法未正确调用。这需要与 at 选择器相同的方法。
要测试,请尝试以下操作:
NSLog(@"%@", NSStringFromSelector(_cmd));
放入每个-application...
方法中,它将记录它们当他们被召唤时。这是了解他们的好方法。Your code is valid, it works for me. And locking the screen or hitting the home button will cause this notification to be posted.
*(One caveat to this is if your device does not support multitasking or if you have the "
Application does not run in background
" property set to yes in your*info.plist
. In which case it will go straight to the "UIApplicationWillTerminateNotification
" notification)So barring that there there are two possiblities:
1) Your
addObserver
code is not being called, ie. It's in the wrong method.To test try this:
2) The observer method is not being called properly. Which requires the same method as the at selector.
To test try this:
NSLog(@"%@", NSStringFromSelector(_cmd));
in each of the-application...
methods and it will log them when they are called. It's a nice hands on way to get to know them.