简体   繁体   English

Nativescript webview 在 iOS 上获取 cookie

[英]Nativescript webview get cookies on iOS

in my Nativescript Angular application, i authenticate to a node.js server using express-session and a nativescript webview.在我的 Nativescript Angular 应用程序中,我使用 express-session 和 nativescript webview 对 node.js 服务器进行身份验证。 The server sends me back a cookie (connect.sid) containing the sessionid that i will use to access the node.js app.服务器向我发送回一个 cookie (connect.sid),其中包含我将用于访问 node.js 应用程序的 sessionid。

I cannot read the connect.sid cookie in response from the server on iOS.我无法从 iOS 上的服务器读取 connect.sid cookie 作为响应。 I am currently using NSHTTPCookieStorage.sharedHTTPCookieStorage.cookies, and this contains a connect.sid, but its value is not the right one.我目前正在使用 NSHTTPCookieStorage.sharedHTTPCookieStorage.cookies,它包含一个 connect.sid,但它的值不是正确的。

I managed to do that on Android using the following code:我设法使用以下代码在 Android 上做到了这一点:

var cookieManager = android.webkit.CookieManager.getInstance();
var cks = cookieManager.getCookie(myserverdomain);

I am trying to do the same on iOS:我正在尝试在 iOS 上做同样的事情:

const sharedCookieInstance: any = 
NSHTTPCookieStorage.sharedHTTPCookieStorage;
const cookies: any = sharedCookieInstance.cookies();

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)但是,正如我之前写的那样,我在 iOS 上从这里获得的 connect.sid 不是“最终”值..(这似乎是您执行第一个请求时实例化的第一个 connect.sid)

I've read half of the web without finding an answer.我已经阅读了一半的网络而没有找到答案。 Can someone please help me ?有人可以帮帮我吗 ?

Thanks in advance提前致谢

//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')
})

Note: this is a similar approach in iOS native code.注意:这是 iOS 本机代码中的类似方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM