WkWebView 有时会丢失会话变量 [英] WkWebView sometimes loses the session variable

查看:46
本文介绍了WkWebView 有时会丢失会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Ruby on Rails 后端 Web 应用程序.我使用 iOS 应用程序作为 swift 编写的 Web 客户端(WKWebView).身份验证令牌作为会话 cookie 变量存储在后端:

I have a Ruby on Rails backend web application. I use iOS application as a web client (WKWebView) written in swift. The authentication token is stored as a session cookies variable on the backend:

session['token'] = generate_token

问题是不小心这个会话变量丢失了,流程被重定向到登录页面.WKWebView 没有关闭,只是用来处理 HTTP 请求.我无法弄清楚,为什么会意外发生这种情况.

The problem is that accidentally this session variable is lost and the flow is redirected to login page. The WKWebView is not closed, just used to process HTTP requests. I'm not able to figure out, why this accidentally happens.

推荐答案

由于您仅使用 webView 而不是默认的 Safari 浏览器,这意味着您正在制作自定义浏览器,因此您有责任处理诸如缓存当前令牌之类的事情会议.我的建议是,如果您希望令牌仅存储和使用一次,则创建一个类似

Since you are using just a webView and not default Safari browser meaning you are making your custom browser hence you are responsible to handle things like caching your token for the current session. My suggestion, if you want the token to be stored and used only once then create a variable something like

let accessToken = "your json web token"

然后在向您的服务器发出请求时将其作为标头传递.

Then pass it as a header when making a request to your server.

如果您想保持访问权限,则可能需要根据安全级别将令牌保存在 keyChain 或 userDefaults 中.

If you want to maintain access then you might want to save your token in keyChain or userDefaults depending on the level of security.

导航和其他用户交互

正如我所提到的,使用 wkwebView 意味着您正在制作自己的浏览器,因此您必须在用户与您的网页交互时检测后续请求.对于导航,您必须使用 WKNavigationDelegate 方法.

As I mentioned, using wkwebView meaning you are making your own browser so you have to detect subsequent requests as the user interact with your web pages. For navigation, you have to use WKNavigationDelegate methods.

如果 WKNavigationDelegate 不能解决您的问题,那么您必须使用 Javascript 事件,即.. 在用户访问某个页面时发布一个事件并使用 WKScriptMessageHandler 通过传递访问令牌等来响应这些事件.

If WKNavigationDelegate does not solve your problem then you have to make use of Javascript events ie.. post an event when user visit a certain page and use WKScriptMessageHandler to respond to those events by passing the acess token etc..

这篇关于WkWebView 有时会丢失会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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