【问题标题】:automatic session timeout iphone自动会话超时 iphone
【发布时间】:2013-05-13 12:12:38
【问题描述】:

我正在开发一个 Iphone 应用程序,我需要在后台执行自动注销,即在按下 Iphone 的主页按钮之后。

我已经尝试了以下会话超时代码,它适用于桌面。但是这个解决方案在 Iphone 的后台不起作用,因为我必须等待 10 秒才能重定向到所需的页面。

<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
$(document).ready(function(){
    var wintimeout;
    function SetWinTimeout() {
         wintimeout = window.setTimeout("window.location.href='try.html';",10000); //after 5 mins i.e. 5 * 60 * 1000
    }
    $('body').click(function() {
        window.clearTimeout(wintimeout); //when user clicks remove timeout and reset it
        SetWinTimeout();
    });
    SetWinTimeout();
});
</script>
</head>
<body>
<a href = "try.html"> try link </a>
Hey there.. is this working fine?
</body>
</html>

有人可以为此提供解决方案吗?

上面代码中的会话超时间隔也没有在 Iphone 上重置,因为即使我正在点击屏幕,我也会被重定向到所需的页面。我该如何解决这个问题?

【问题讨论】:

  • 它可能对你有帮助,试试这里给出的解决方案:stackoverflow.com/a/10991974/309086
  • 我的问题不同。因为我的超时正在工作,但我想在应用程序处于后台时这样做,而且我想在用户点击屏幕时重置超时计数器。但这不起作用。
  • 对于后台场景,setTimeout 将不起作用。在此处查看可能有帮助的解决方案:stackoverflow.com/a/4910900/309086

标签: jquery iphone ios session-timeout


【解决方案1】:

iPhone Sdk 在进入后台 5 秒后挂起所有任务。如果应用程序支持音频播放位置更新等多任务处理,它将支持后台任务。我可以为您提供一种解决方案,既可以节省您进入后台的时间,也可以节省您进入前台的时间。如果它计算的时间超过了注销时间,那么点击你想要的服务。

【讨论】:

  • 你能告诉我我们如何在 Iphone 中检测到应用程序已进入后台吗?这样我就可以在那一刻获得时间.. 还有我如何检测到它在 Iphone 上再次恢复?
  • - (void)applicationDidEnterBackground:(UIApplication *)application 和 - (void)applicationWillEnterForeground:(UIApplication *)application 是应用程序进入后台或前台时调用的委托方法
  • 我需要 jquery/javascript 中的解决方案,因为我的应用程序没有使用 Objective C。
  • 对不起,我不知道 jquery 或 javascript。它只适用于 iPhone SDK
【解决方案2】:

也许是这样的?

<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
$(document).ready(function(){
    document.lastActivity = new Date().getTime();
    document.threshold = 20000; // some value
    // and set an interval to keep an eye on last activity
    var interval = setInterval(/* something here */);
    $('body').on('click keydown', function() {
        document.lastActivity = new Date().getTime(); // keep resetting this
    });
}).on('focus', function () {
    var timeSince = new Date().getTime() - document.lastActivity;
    if (timeSince > document.threshold) {/* do stuff here */}
});
</script>
</head>
<body>
<a href = "try.html"> try link </a>
Hey there.. is this working fine?
</body>
</html>

这个想法是计算自上次活动以来经过的时间。当浏览器在后台时,iOS 设备 暂停 JS,我认为这是有充分理由的。

【讨论】:

    【解决方案3】:

    这样的事情怎么样... 注意:我有几个 MVC Razor sn-ps 可以与其他任何东西一起切换。我还没有测试过,但这是一个好的开始。非 Safari iOS 浏览器将忽略 pageshow/pagehide 事件处理程序,因此这不是移动设备的完整解决方案。请随时对此进行改进,以获得更广泛的跨浏览器功能。此外,此解决方案需要使用 JQuery cookie 插件:https://github.com/carhartl/jquery-cookie

    ///////// 客户端代码

        //Safari iOS example that could be further developed to handle other mobile browsers
        var sessTimeCutOffInMs = 600000;   //600000 ms equals 10 minutes
    
        //Safari iOS event handler for resume tab and/or focus from sleep mode
        window.addEventListener("pageshow", function(e){            
            var timeIn = getTime();
            var timeOut = $.cookie('timeOut');
            if(timeOut != null) {
                //Let us compare
                compareTimes(timeIn,timeOut);
            }
        }, false);
    
        //Safari iOS event handler when creating new tab/app switching and/or putting into sleep mode
        window.addEventListener("pagehide", function(e){
            var timeOut = getTime();
            $.cookie('timeOut', timeOut, { path: '/' });
    
        }, false);  
    
    
        function getTime() {
            @{
            //MVC Razor syntax
             //formatted as milliseconds since 01.01.1970
             var _serverTime = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds.ToString("F0");
            }
            var serverTime = @_serverTime;
            return serverTime;
            //return (new Date()).getTime();            
        }   
    
        function compareTimes(timeIn,timeOut) {
            var diff = timeIn - timeOut;
            //If the mobile page was asleep for 10 minutes or more, force iOS user to logout
            //Especially useful for when forms auth is set to slidingExpiration=true
            if(diff >= sessTimeCutOffInMs) {
                //Redirect to logout routine
                //MVC Razor code shown below for redirecting to my Home Controller/Action
                simpleReset('@(Html.ResolveUrl("~/Home/SignOut"))');
            }
        }       
    
        function simpleReset(url) {
            window.location.href = url;
        }
    

    //////////// MVC 4 HomeController.cs SignOut() Action 的代码片段

        [AllowAnonymous]
        public ActionResult SignOut()
        {            
            try
            {
                ViewBag.Message = "You are signed out.";
    
                //DELETE SSO Cookie 
                HttpCookie ssoCookie = new HttpCookie("SMSESSION", "NO");
                ssoCookie.Expires = DateTime.Now.AddYears(-1);                            
                ssoCookie.Domain = ".myDomain.com";      //IMPORTANT:  you must supply the domain here
                Response.Cookies.Add(ssoCookie);
    
                //Look for an existing authorization cookie and kill it
                HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
                authCookie.Value = null;
                authCookie = null;
                Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
    
                // clear authentication cookie
                //Overriding the existing FormsAuthentication cookie with a new empty cookie ensures 
                //that even if the client winds back their system clock, they will still not be able 
                //to retrieve any user data from the cookie
    
                HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
                cookie1.Path = FormsAuthentication.FormsCookiePath;
                cookie1.Expires = DateTime.Now.AddYears(-1);
                cookie1.HttpOnly = true;
                Response.Cookies.Add(cookie1);
    
                // clear session cookie
                HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", "");
                cookie2.Expires = DateTime.Now.AddYears(-1);
                Response.Cookies.Add(cookie2);
    
                //Explicitly destroy roles object and session
                SimpleSessionHandler.myRoles = null;
                Session.Clear();
                Session.Abandon();
    
                // Invalidate the Cache on the Client Side
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Cache.SetNoStore();
    
                FormsAuthentication.SignOut();
            }
            catch (Exception ex) {
                //Swallow it
                Log.LogError("AUTH COOKIE ERROR: " + ex.Message, ex);
            }
    
            //The loginUrl on IWH is actually a LOGOUT page for the SSO cookie
            return Redirect(FormsAuthentication.LoginUrl);
    
        }
    

    【讨论】:

      猜你喜欢
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-09
      • 2018-12-25
      • 1970-01-01
      • 2013-04-05
      相关资源
      最近更新 更多