为什么SFSafariWebViewController无法与Safari正确共享cookie? [英] Why is SFSafariWebViewController not sharing cookies with Safari properly?

查看:173
本文介绍了为什么SFSafariWebViewController无法与Safari正确共享cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个令人难以置信的基本应用程序,其中包括指向URL http://的SFSafariViewController www.w3schools.com/js/js_cookies.asp .这是一个用于读取和写入Cookie的测试网站.

I created an incredibly basic app which includes a SFSafariViewController pointing at the URL http://www.w3schools.com/js/js_cookies.asp . This is a test website for reading and writing cookies.

然后,我将同一网站加载到Mobile Safari中,并添加了一个cookie.我切换到我的应用程序,阅读cookie,它在那里.我回到Safari,添加另一个cookie,回到我的应用程序,但是第二个cookie没有出现.我刷新页面,没有区别.返回Safari并读取cookie,它们均已成功读取.

I then loaded the same website into Mobile Safari, and added one cookie. I switched to my app, read the cookie, it's there. I go back to Safari, add another cookie, go back to my app, but the second cookie hasn't appeared. I refresh the pages, no difference. Go back to Safari and read the cookies, they are both read successfully.

在应用之间是否需要做些什么才能正确编写和读取Cookie?

Is there anything I need to do between apps in order for the cookies to be written and read properly?

推荐答案

上的用户Apple Dev Forums 建议,它可能仅适用于持久性cookie",而不适用于会话cookie".我没有在cookie上设置有效期限.我通过将来的时间改变了这一点:

A user on the Apple Dev Forums suggested that it might only work for "persisted cookies" not "session cookies". I wasn't setting an expiry date on my cookies. I changed that by getting a time in the future:

const expireTime = new Date(Date.now() + 1000 * 60 * 60 * 24).toGMTString();

然后将其设置在标题中:

And then setting it in the header:

"Set-Cookie":`query=${uri.query}; path=/; expires=${expireTime}`

现在cookie值显示在SFSafariViewController中.

And now the cookie value appears in SFSafariViewController.

这篇关于为什么SFSafariWebViewController无法与Safari正确共享cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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