本文介绍了使用LocalStorage存储Cookie在移动Web应用程序中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SO的大师



我已经在iOS主屏幕上发布了一个网络应用程序,想不必每次打开应用程序时登录。所以我试图将Cookie推送到LocalStorage。



我使用以下代码尝试将我的Cookie存储在移动网络应用程序的LocalStorage中(从iPhone复制的代码web应用ruby宝石)。但不知何故它不工作。任何建议?



谢谢。

 < script type = text / javascript> 
(function(){
var RESEND_REQUEST = {{RESEND}};

function isFullScreen(){
return navigator.userAgent.match(/WebKit.* Mobile /)&&
!navigator.userAgent.match(/ Safari /);
}

if(isFullScreen()){
if .cookie ==''){
var storedValues = localStorage.getItem('__ cookie__');
if(storedValues){
var values = storedValues.split(';');
for(var i = 0; i document.cookie = values [i];
}
document.cookie ='_cookieset_ = ;

if(RESEND_REQUEST){
window.location.reload();
}

}

var lastCookie = null;
setInterval(function(){
if(lastCookie!=''+ document.cookie){
lastCookie =''+ document.cookie;
localStorage.setItem ('__cookie__',''+ document.cookie);
}
},1000);

}
})()
解决方案


1 if(document.cookie =='')

上述语句并不总是假设返回true 你的web_app从第一次主屏幕,即document.cookie确实包含一些价值(垃圾,但仍然),甚至从主屏幕打开(至少我发现)。我敦促你提醒同样警告



进入上面的警告(document.cookie)提到 if子句
如果是( document.cookie确实包含一些值),那么我想你需要修复上述if子句,像这样

 > if(!document.cookie.match(/ _ session_id /)){
> //其余代码在这里
> }

如果您使用ActiveRecord :: Base.session_store



 > if(!document.cookie.match(/ {{YOUR SESSION KEY}} /){
> //其余的代码在这里
>}



如果使用Cookie Store,您的会话密钥下面的键可以找到我看看 config / initializer / session_store。 rb 文件



2 。注意以下代码

确实有意义,但是有一些麻烦:
除了 document.cookie应用程序的cookie 维护
并由浏览器存储,但我注意到 document.cookie 并不一定是相同



例如浏览器为我的应用程序存储以下cookie



__ cookieset = 1; KBD = 0en-3; _session_id = 896c455928f3dd9e7bb0b660efb7063c
$ b

但在检查document.cookie时,我发现它包含

 __ cookieset = 1 ; KBD = 0en-3;

注意document.cookie不包含 _session_id = 896c455928f3dd9e7bb0b660efb7063c



由各种授权gem(devise或authlogic)确定当前用户是否具有有效会话?



所以我请求存储从请求对象的cookie从获取Rack :: Request.new (env)
加入localStorage



3

如果您使用ActiveRecord :: Base.session_store,我猜测您的放置中间件的补丁代码可以找到相同的宝石解决您的目的


Gurus of SO

I have posted a web app to the iOS Home Screen & want to not have to login each time the app opens up. So I am trying to push the cookie into LocalStorage.

I am using the following code to try to store my cookies in LocalStorage for a mobile web app (code copied from iphone web app ruby gem). But somehow its not working. Any suggestions?

Thank you.

<script type="text/javascript">
(function(){
var RESEND_REQUEST = {{RESEND}};

function isFullScreen(){
  return navigator.userAgent.match(/WebKit.*Mobile/) &&
         !navigator.userAgent.match(/Safari/);
}

if(isFullScreen()){
  if(document.cookie == ''){
    var storedValues = localStorage.getItem('__cookie__');
      if(storedValues){
        var values = storedValues.split(';');
        for(var i=0; i < values.length; i++)
          document.cookie = values[i];
      }
      document.cookie = '_cookieset_=1';

    if(RESEND_REQUEST){
      window.location.reload();
    }

  }

  var lastCookie = null;
  setInterval(function(){
    if(lastCookie != ''+document.cookie){
      lastCookie = ''+document.cookie;
      localStorage.setItem('__cookie__', ''+document.cookie);
    }
  },1000);

}
})()
解决方案

There are couple thing that does fit in the above code

1. if(document.cookie == '')
The above statement not always suppose return true even when you are opening your web_app from iOS Home Screen for the first time i.e the document.cookie does contain some value (junk though but still) even opening from Home screen(atleast what I found). I urge you to prompt the same with alert

Something like alert(document.cookie) before running into the above mentionif clause If yes(document.cookie does contain some value) then I guess you need to fix the above if clause something like this

>       if(!document.cookie.match(/_session_id/) ) {
>          // Rest of the code goes here
>       }

if your using ActiveRecord::Base.session_store

or

>     if (!document.cookie.match(/{{YOUR SESSION KEY}}/) {
>               // Rest of the code goes here
>      }

your Session Key if using Cookie Store "the following key can be found my looking at the config/initializer/session_store.rb file

2. As notice the below code

does make sense when reading though it but there is twist to it one would except the document.cookie to contain cookie for the application maintained and stored by the browser but as I notice that document.cookie does not turn out to be same

e.g browser stored the following cookie for my application

"__cookieset=1;KBD=0en-3;_session_id=896c455928f3dd9e7bb0b660efb7063c"

but when inspected the document.cookie I found it to be contain

"__cookieset=1;KBD=0en-3;"

Notice that document.cookie doesnot contain "_session_id=896c455928f3dd9e7bb0b660efb7063c"

Which is must as It used by various authorization gem(devise or authlogic) to determine whether the current user has a valid session ?

so I request you store the cookie from the request object obtain from Rack::Request.new(env)into the localStorage

3. The middleware placement make sure your placing middleware at right place.

If your using ActiveRecord::Base.session_store I guess the patch code of the same gem can be found here solve your purpose

这篇关于使用LocalStorage存储Cookie在移动Web应用程序中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 18:24