Nativescript Webview在iOS上获取Cookie [英] Nativescript webview get cookies on iOS
问题描述
在我的Nativescript Angular应用程序中,我使用express-session和一个nativescript Webview认证到一个node.js服务器。
服务器发回给我一个cookie(connect.sid),其中包含我将用于访问node.js应用程序的sessionid。
in my Nativescript Angular application, i authenticate to a node.js server using express-session and a nativescript webview. The server sends me back a cookie (connect.sid) containing the sessionid that i will use to access the node.js app.
我无法读取响应来自iOS上服务器的connect.sid cookie。
我当前正在使用NSHTTPCookieStorage.sharedHTTPCookieStorage.cookies,其中包含一个connect.sid,但它的值不正确。
I cannot read the connect.sid cookie in response from the server on iOS. I am currently using NSHTTPCookieStorage.sharedHTTPCookieStorage.cookies, and this contains a connect.sid, but its value is not the right one.
我设法做到了在Android上使用以下代码进行修改:
I managed to do that on Android using the following code:
var cookieManager = android.webkit.CookieManager.getInstance();
var cks = cookieManager.getCookie(myserverdomain);
我正在尝试在iOS上执行相同操作:
I am trying to do the same on iOS:
const sharedCookieInstance: any =
NSHTTPCookieStorage.sharedHTTPCookieStorage;
const cookies: any = sharedCookieInstance.cookies();
但是,正如我之前写的那样,我在iOS上从中获得的connect.sid不是最终价值..(这似乎是您发出第一个请求时实例化的第一个connect.sid)
but, as i wrote before, the connect.sid that i get from this on iOS is not the 'final' value.. (it seems to be the first connect.sid instantiated when you do a first request)
我已经阅读了一半的网络,但没有找到回答。有人可以帮我吗?
I've read half of the web without finding an answer. Can someone please help me ?
预先感谢
//Android, working
var cookieManager = android.webkit.CookieManager.getInstance();
var cks = cookieManager.getCookie(myserverdomain);
//iOS, not working
const sharedCookieInstance: any =
NSHTTPCookieStorage.sharedHTTPCookieStorage;
const cookies: any = sharedCookieInstance.cookies();
推荐答案
我知道这是一篇较晚的文章。
I know it's a late post. But still, if it helps someone.
WKWebsiteDataStore.defaultDataStore().httpCookieStore.getAllCookies(cookies => {
console.log('Received Cookies Are:'+ cookies)
//here is the way to get all the cookie information as a string.
NSHTTPCookie.requestHeaderFieldsWithCookies(cookies).objectForKey('Cookie')
})
注意:这是iOS本机代码中的类似方法。
这篇关于Nativescript Webview在iOS上获取Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!