根据用户活动注销
我一直在寻找这个答案一段时间了,但我找不到解决方案。任何人都可以告诉我,自从用户与应用程序进行任何交互以来,我们如何计算后台的时间。在一些网站中,如果您一段时间不与网页交互,您将被注销。我正在寻找这个功能。
我非常感谢您的宝贵时间。 谢谢
I have been looking for this answer from a while but I could not get find solution. Can any one please tell me how do we calculate time in back ground since user made any interaction with the app. In few websites if you don't interact with the web page for a while you will be logged out. I am looking for this functionality.
I would really appreciate your time.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为您可以通过 UIApplication 自定义子类捕获事件。您可以在那里重新启动空闲计时器,而不会弄乱所有代码。操作方法如下:
创建您自己的 UIApplication 子类:
在您的 main() 中
在您的应用程序 subclass.m 中,覆盖 sendEvent: 和 sendAction:。请参阅此处的 Apple 文档:
。 .. 与发送操作相同。您也可以将注销逻辑放入此类中:
当应用程序退出时,您仍然应该在应用程序委托中注销(并使计时器无效):
I think you can catch events via a UIApplication custom subclass. You can restart your idle timer there without mucking up all of your code. Here's how to do it:
Make your own UIApplication subclass:
In your main()
In your application subclass.m, override sendEvent: and sendAction:. See Apple docs here:
... same for send action. You can put your logout logic in this class, too:
You should still logout (and invalidate the timer) in your app delegate when the app resigns:
您保存最后一次交互的 UNIX 时间戳,然后将其与当前的时间戳进行比较。如果它们之间的差异大于您的时间限制,您将注销用户。
You save a UNIX timestamp of the last interaction and then you compare it to a current one. If the difference between them is greater than your time limit, you log user out.
我会记录
- (void)applicationWillEnterForeground:(UIApplication *)application
和- (void)applicationWillEnterBackground:(UIApplication *)application
的时间,计算不同的时间,检查它是否超过特定值,从上一个会话中注销用户。I would log the time when
- (void)applicationWillEnterForeground:(UIApplication *)application
, and when- (void)applicationWillEnterBackground:(UIApplication *)application
, calculate the different, check if it exceed certain value, log off user from previous session.归档此问题的一种方法是使用登录哈希值设置 cookie,每次用户请求页面时,该 cookie 的过期时间为 X 分钟。如果 cookie 不存在,则用户将被注销。这也可能是会话 cookie。当然,这只适用于网络应用程序:)
One way to archive this is setting a cookie with a login hash that has an expire time of X minutes every time the user request a page. If the cookie isn't present the user is logged out. This could also be a session cookie. Of course that only works if you're talking about a webapp :)