在PhoneGap / Cordova中处理Cookie [英] Handling cookies in PhoneGap/Cordova

查看:951
本文介绍了在PhoneGap / Cordova中处理Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个具有服务器会话使用的PhoneGap应用程序。它需要cookie来处理会话。此外,来自负载均衡器的cookie也应该被处理。所以没有办法。 ?你怎么在你的PhoneGap应用处理Cookie

I'm working on a PhoneGap app with server session usage. It needs cookies to handle the session. Additionally, the cookie from the load balancer should be handled, too. So there is no way around. How do you handle Cookies in your PhoneGap app?

我已经完成了一些研究:

I have already accomplished some research:


  • 有人说cookie处理可能取决于服务器不设置未知用户代理的cookie(IIS)的在iOS

  • PhoneGap的会话(饼干)在JavaScript中的cookie可以用的document.cookie = ...进行设置,但不保存在PhoneGap的和丢失。

  • 在xhr请求之后,可以使用xhr.getResponseHeader('Set-Cookie')检索Cookie。但只有当实际设置在服务器上。不幸的是,jQuery会删除Cookie标头。

  • JavaScript文档.cookie属性未分配,且在(xhr)请求后不会更新。

  • 一些建议localStorage保存会话ID等等,但所有脚本可以访问它,这可能是XSS安全问题。 Cookie通过使用httponly标志解决此问题。

  • iOS:有一些修改会更改webView行为以支持cookie。但是它们似乎不适用于iOS 6和PhoneGap 2.5: https://groups.google。 com / forum /?fromgroups =#!topic / phonegap / ZJE1nxX63ow

  • AppDelegate.m(v2.5)中似乎默认启用Cookie。

  • Some say cookie handling might depend on the server not setting cookies for unknown user agents (IIS): PhoneGap session (cookies) on iOS
  • In JavaScript cookies can be set with document.cookie = ..., but they are not saved in PhoneGap and lost. Before firing xhr requests it works.
  • Cookies can be retrieved after xhr request with xhr.getResponseHeader('Set-Cookie'). But only when actually set on the server. Unfortunately, jQuery strips the "Cookie" header.
  • The JavaScript document.cookie property is not assigned and not updated after (xhr)requests.
  • Some suggest the localStorage to save session ids etc. But all scripts can access it and this might be XSS security issue. Cookies work around this issue by using the httponly flag.
  • iOS: There are some modifications which will change the webView behaviour to support cookies. But they seem not to work with iOS 6 and PhoneGap 2.5: https://groups.google.com/forum/?fromgroups=#!topic/phonegap/ZJE1nxX63ow
  • Cookies seem to be enabled by default in the AppDelegate.m (v2.5).

推荐答案

朋友,我没有成功使用cookie与phonegap。解决方案是使用localStorage。

Friend, I've tried too without success to use cookies with phonegap. The solution was use localStorage.

键快速示例:

 var keyName = window.localStorage.key(0);

设置项目快速示例:

 window.localStorage.setItem("key", "value");

获取项目快速示例

 var value = window.localStorage.getItem("key");
 // value is now equal to "value"

删除项快速示例: p>

Remove Item Quick Example:

 window.localStorage.removeItem("key");

清除快速示例:

 window.localStorage.clear();

如果您在移动和网络上都使用javascript,可以使用此代码检测环境:

If you use you javascript for both mobile and web, you can use this code to detect that enviroment:

var wl = window.location.href;
var mob = (wl.indexOf("android")>0);

参考文献:
http://docs.phonegap.com/en/1.2.0/phonegap_storage_storage.md.html#localStorage
http:// cordova。 apache.org/docs/en/6.x/cordova/storage/storage.html#page-toc-source

注意:使用匿名导航在iOS上可能使localstorage不工作像spected。一个对我很好的简单测试:

Be aware: using anonymous navigation on iOS may make localstorage not work like spected. A simple test that are working fine to me:

$(document).ready(function () {
    try {
        localStorage.setItem('test', '1');
    } catch (Err) {
        if (Err.message.indexOf('QuotaExceededError') > -1) {
            // Tell the user they are in anonymous mode
            // Sugest it to go to https://support.apple.com/pt-br/HT203036 to get help to disable it
            }
        }
    }
});

这篇关于在PhoneGap / Cordova中处理Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆