在 applicationWillEnterForeground 之前更改视图
我想在用户处于后台一段时间后将其锁定在我的应用程序之外。我在 AppDelegate 的 applicationWillEnterForeground 中捕获了这一点(并与 applicationWillResignActive 中存储的时间进行比较)。如果小于超时时间,则不会发生任何操作。如果超过超时时间,我调用:
[_navigationController popToRootViewControllerAnimated:NO];
这会将用户带回根视图。
只要有一次视觉中断,它就能完美地工作。先前的视图(应用程序处于非活动状态时用户正在查看的视图)在弹出到根目录之前会出现非常短暂。在测试中,视图似乎在调用 applicationWillEnterForeground
之前重新出现。
我唯一的想法是在不活动之前隐藏所有内容,例如用填充的矩形遮挡视图。这对我来说就像是一个肮脏的黑客,所以我想有一个更好的方法。
我也愿意采用不同的方式来达到相同的最终结果。谢谢你!
I want to lock users out of my app after a period of being in the background. I'm catching this in the the AppDelegate's applicationWillEnterForeground
(and comparing to time stored in applicationWillResignActive
). If less than the timeout period no action takes place. If more than the timeout period I call:
[_navigationController popToRootViewControllerAnimated:NO];
which brings the user back to the root view.
It works perfectly fine with one visual interruption. The prior view (that which the user was viewing when the application went inactive) appears very briefly before popping to root. In testing it seems the view reappears prior to applicationWillEnterForeground
being called.
The only thought I've had is to hide everything before going inactive, such as by obscuring the view with a filled rectangle. This smells like a dirty hack to me, so I'm thinking there's a better way.
I'm also open to different ways to reach the same end result. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过使视图不可见解决了这个问题。在 applicationWillResignActive 我有 _navigationController.view.alpha=0;,并且我放置了 _navigationController.view.alpha=100;在(如有必要)弹出到登录视图之后的 applicationWillEnterForeground 中。比弹出和恢复视图更容易(在这种情况下相当重)。
@TheBlack 指出了隐藏属性,这在设备上可能稍微容易一些。我保留 alpha 是因为 A) 使用 50% alpha 视图进行调试很有趣,B) 我只是喜欢 alpha。但除非你像我一样是阿尔法粉丝,否则隐藏可能会更好一些。
I've solved this by making the view invisible. In applicationWillResignActive I have _navigationController.view.alpha=0;, and I've placed _navigationController.view.alpha=100; in applicationWillEnterForeground after (if necessary) popping to the login view. Easier than popping and restoring views (which in this case are pretty heavy).
@TheBlack points out the hidden property, which is probably slightly easier on the device. I'm leaving alpha in because A) it's fun debugging with a 50% alpha view, and B) I just like alpha's. But unless you're an alpha fan like me, hidden might be a little better.
来自文档: 处理应用程序状态转换的策略
From the docs: Strategies for Handling App State Transitions