1
votes

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.

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.

I managed to do that on Android using the following code:

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

I am trying to do the same on 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)

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();
1
Looks like you are using the right api on iOS, but may be you are accessing the cookies too early, before WKWebView could write / update them to NSHTTPCookieStorage. Can you build a Playground sample where we can see the issue? - Manoj
That would be a problem as it's a federated login that I cannot share. Just wanted to add that I also tried to wait for 20 seconds before reading cookies on iOS, with a setTimeout... I've read that cookies in nshttpcookiestorage are not always immediately available....that timeout is not used instead on android.... All of this is obviously done on the loadFinished event of the webview. But even after 20secs I have the same result. For now, thanks for interesting. Also, till now I tried this only on the iOS emulator, not yet on device - Demy Mortelliti
Did you try force updating cookies in WebView? - Manoj
No, because I didn't understand how to do that. Read something about though, but not clear to me, ad i have very little xperience about native ios code - Demy Mortelliti
So that's the reason behind asking for a sample, I could try and see if force sync works. As you already mentioned, this issue is discussed all over internet without one perfect solution. - Manoj

1 Answers

0
votes

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.