【问题标题】:Cordova inappbrowser cookies when application is killed应用程序被杀死时 Cordova inappbrowser cookie
【发布时间】:2015-06-11 15:02:25
【问题描述】:

在我的 Cordova 应用程序中,我尝试使用 inappbrowser 打开一个链接。 此链接指向在访问页面之前需要 google 身份验证的域。如果我尝试再次打开链接,域不需要再次验证,会话正常,效果很好。

我的问题发生在我终止应用程序时。如果我想再次访问该链接,Google 会要求我再次进行身份验证。我不想那样做。

有没有办法保存inappbrowser插件的cookies?

提前致谢!

【问题讨论】:

    标签: cordova cookies inappbrowser


    【解决方案1】:

    我遇到了同样的问题(在 android 上)并意识到我正在使用的库(ngCordova / cordovaOauth)正在删除缓存和会话数据(使用window.open(..., ..., 'clearsessioncache=yes,clearcache=yes')

    现在它可以工作了:我使用的登录名被记住了,我不必再次重新输入我的密码。所以 cookie 肯定会保留,至少在 android 上是这样。

    如果您有兴趣,这里是 ngCordova 中的相关代码:

    var redirect_uri = "http://localhost/callback";
    if(options !== undefined) {
        if(options.hasOwnProperty("redirect_uri")) {
            redirect_uri = options.redirect_uri;
        }
    }
    var browserRef = window.open('https://accounts.google.com/o/oauth2/auth?client_id=' 
        + clientId + '&redirect_uri=' + redirect_uri 
        + '&scope=' + appScope.join(" ")
        + '&prompt=select_account&response_type=token', '_blank', 
        'location=no');
    
    browserRef.addEventListener("loadstart", function(event) {
        if((event.url).indexOf(redirect_uri) === 0) {
          browserRef.removeEventListener("exit",function(event){});
          browserRef.close();
            var callbackResponse = (event.url).split("#")[1];
            var responseParameters = (callbackResponse).split("&");
            var parameterMap = [];
            for(var i = 0; i < responseParameters.length; i++) {
                parameterMap[responseParameters[i].split("=")[0]] = responseParameters[i].split("=")[1];
            }
            if(parameterMap.access_token !== undefined && parameterMap.access_token !== null) {
                deferred.resolve({ access_token: parameterMap.access_token, token_type: parameterMap.token_type, expires_in: parameterMap.expires_in });
            } else {
                deferred.reject("Problem authenticating");
            }
        }
    });
    
    browserRef.addEventListener('exit', function(event) {
        deferred.reject("The sign in flow was canceled");
    });
    

    我对其进行了一些修改以满足我的需要。您会在第 200 行附近找到原始的 here(查找 google:)。

    【讨论】:

    • 我在IOS 10中遇到同样的问题。你有IOS的解决方案
    【解决方案2】:

    这只是我想分享的一个想法(从未测试过):

    来自documentation

    var ref = window.open(url, target, options);
    
    • 选项

    clearcache:设置为 yes 以在新窗口打开之前清除浏览器的 cookie 缓存

    clearsessioncache:设置为 yes 以在新窗口打开之前清除会话 cookie 缓存

    我认为值得尝试使用这些选项,看看会发生什么。

    【讨论】:

    • 已经试过了,还是不行。但感谢您的帮助! :)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多