cordova-plugin-wkwebview-engine icon indicating copy to clipboard operation
cordova-plugin-wkwebview-engine copied to clipboard

Cookies are not set/stored in iOS

Open FarhadG opened this issue 5 years ago • 27 comments

Since we upgraded from the old UIWebView engine, we are not able to authenticate our users (using an external auth service), as none of the cookies sent by the APIs are being set in the this web view.

This is a major blocker for us and have been trying to determine what a good course of action is. If our assumptions are correct that cookies are not supported with this web view (which is very strange), we are going to try:

  • Proxy (not sure if you recommend a good plugin for this)
  • Native fetch (not sure if you recommend a good plugin for this)

Our stack:

  • Cordova
  • React
  • Fetch for calls

FarhadG avatar Feb 05 '19 14:02 FarhadG

Same issue here. I installed this plugin in my Cordova project and cookies work fine on Android but not on iOS! :(

williamhilldevs avatar Feb 09 '19 20:02 williamhilldevs

We've been using https://github.com/oracle/cordova-plugin-wkwebview-file-xhr which works for us

adam-h avatar Feb 11 '19 04:02 adam-h

Thanks for the response, @adam-h! I took a look and spent a few hours trying to read cookies in my iOS WKWebView, however, no luck :(

Do you have any examples for me to reference to see if I'm doing anything incorrectly?

I'm making a simple request with the following:

// index.js inside of Cordova application
const xhr = new XMLHttpRequest();
xhr.addEventListener('loadend', function(evt) {
  console.log({ evt, response: this.response });
});

xhr.open('GET', 'http://localhost:8000/api');
xhr.send();

And here's the mockserver attaching cookies to every request:

// server.js
const cors = require('cors');
const express = require('express');
const cookieParser = require('cookie-parser');

const app = express();

app.use(cookieParser());
app.use(cors());

app.get('*', (req, res) => {
  res.cookie('username', 'john doe', { maxAge: 900000, httpOnly: true });
  res.send('Hello World!');
});

Unfortunately, I don't see any cookies in the Cordova application after the server has responded.

FarhadG avatar Feb 11 '19 16:02 FarhadG

Oh, sorry no the patch plugin I linked will only make server side cookies function (so things like login will often just work) - it won't allow you to get/set them from the client. The way it works is by redirecting all requests to go via native code, rather than through the webview.

adam-h avatar Feb 12 '19 00:02 adam-h

Thanks, @adam-h! That's what I suspected but for some reason, this plugin didn't work for me. I assume it's because I'm using the ionic version of the WKWebView and that plugin requires the Apache one.

That being said, this one did exactly the same functionality: https://github.com/aporat/cordova-plugin-fetch

FarhadG avatar Feb 12 '19 08:02 FarhadG

I have the same issue.

My stack is similar to @FarhadG's (cordova + react). Replacing UIWebView with WKWebView I lost access to all my cookies -- document.cookie always returns empty.

In fact, opening the Safari inspector and setting document.cookie="foo=bar" followed by document.cookie has different results:

UIWebView: document.cookie returns "foo=bar" WKWebView: document.cookie returns ""

I find this extremely odd...

EiyuuZack avatar Jun 06 '19 12:06 EiyuuZack

Is this reproducible in a new, plain Cordova app? A minimal reproduction repository would really help to debug and later fix this issue. More information on how to create one: https://github.com/apache/cordova-contribute/blob/master/create-reproduction.md

(Without this, a maintainer has to spent quite some time to first produce one.)

janpio avatar Jun 19 '19 17:06 janpio

I use "cordova-plugin-wkwebview-inject-cookie": "https://github.com/CWBudde/cordova-plugin-wkwebview-inject-cookie.git"

mgatto avatar Aug 08 '19 04:08 mgatto

I have the same issue.

devjva avatar Oct 07 '19 19:10 devjva

I have the same issue. cookies are not working in ionic ios apps anyone have any solution

tajinder-logiciel avatar Oct 23 '19 10:10 tajinder-logiciel

same here cookies or cache are not saved on cordova ios using wkwebview plugin work perfect on cordova android any solution guys? thanks

matrixreal avatar Mar 09 '20 09:03 matrixreal

Here is my solution to set the cookies in ios devices but its not working in simulators https://github.com/lucky3491/cordova-plugin-wkwebview-inject-cookie

lucky3491 avatar May 03 '20 13:05 lucky3491

I have the same issue. Seems very serious.

pph7 avatar May 29 '20 14:05 pph7

Can anyone please help here...

Aakash9974 avatar Oct 06 '20 12:10 Aakash9974

Any progress in that issue? The solution to this problem is very important, especially since from December everyone will already use Webview instead of UIWebView in large quantities

rezunenko avatar Oct 09 '20 10:10 rezunenko

this is my working solution for this issue also working on ios 14 https://github.com/lucky3491/cordova-plugin-wkwebview-inject-cookie

lucky3491 avatar Oct 09 '20 10:10 lucky3491

this is my working solution for this issue also working on ios 14 https://github.com/lucky3491/cordova-plugin-wkwebview-inject-cookie

Do you have any examples of usage? I have a problem with using it

rezunenko avatar Oct 09 '20 15:10 rezunenko

I have the same problem with fresh install and start app the first time. Login screen is succesfully showed. I fill and submit credentials and try connect to socket.io. Connections failed with error - Unable to connect Socket.IO Missing cookie header. When kill app a start again everything works fine. To you have any solutions to fix this issue? I tried plugin https://github.com/lucky3491/cordova-plugin-wkwebview-inject-cookie but doesnt work.

jnemecek10 avatar Oct 21 '20 06:10 jnemecek10

This plugin (https://github.com/lucky3491/cordova-plugin-wkwebview-inject-cookie) never worked for me. I had to detect iOS platform and in that case manually add the 'Cookie' request header to every XHR call.

ghost avatar Oct 21 '20 07:10 ghost

hlo guys i am using this plugin in ionic 1 project like this var cookiesArray = [{ domain: 'your domain name', name: 'your cookie name', path: '/', value: 'your cookie string' },{...}] wkWebView.injectCookie(cookiesArray);

Simply add this code in your project and then check your request header Make sure install this plugin from cordova plugin add https://github.com/lucky3491/cordova-plugin-wkwebview-inject-cookie check this for reference https://github.com/lucky3491/cordova-plugin-wkwebview-inject-cookie/issues/1#issuecomment-634848058

lucky3491 avatar Oct 22 '20 20:10 lucky3491

Like @breautek mentioned in https://github.com/apache/cordova-ios/issues/883#issuecomment-689646840 you could give [email protected] a try to make AJAX requests work.

If anyone has any idea how to solve the issue when loading HTML assets (image for instance via <img src="...">) which are accessible with a valid session cookie only, please let me know. [email protected] only works for XMLHTTPRequest.

iwan-uschka avatar Oct 26 '20 18:10 iwan-uschka

Here is my solution to set the cookies in ios devices but its not working in simulators https://github.com/lucky3491/cordova-plugin-wkwebview-inject-cookie

In simulator it is not working but have you tested with real devices, can you please make sure about it. Thanks

samarroy avatar Oct 27 '20 23:10 samarroy

Here is my solution to set the cookies in ios devices but its not working in simulators https://github.com/lucky3491/cordova-plugin-wkwebview-inject-cookie

Hi @lucky3491 , can i actually inject any cookie into WKWebView using your cordova-plugin-wkwebview-inject-cookie or is this plugin just about the missing sync issue after starting the app the very first time (quoting the README)?

I tried using your plugin and injecting cookies (extracted with cordova-cookie-emperor) like described in your README but can't make it work (sorry to repeat myself), to load remote HTML assets like images, audio or videos via HTML tags like <img>, <audio> or <video> which are only accessible if the correct (session & CSRF) cookies are sent along.

iwan-uschka avatar Nov 03 '20 21:11 iwan-uschka

@iwan-uschka have you found solution for that? We faces with the same problem, loading resources.

StasKalishenko avatar Nov 24 '20 10:11 StasKalishenko

@StasKalishenko No, unfortunately.

But if you are the owner of the corresponding API you could think about a workaround like using access tokens (as a substitute for cookies) as query param in source URLs. For instance

<img src="https://domain.tld/path/to/binary?accessToken=🔑">

Where to get the accessToken from?

  1. Make a regular API request using https://github.com/oracle/cordova-plugin-wkwebview-file-xhr before you want to load the asset. Login call for instance.
  2. Extract cookie by using cordova-cookie-emperor.
  3. Add the value of the cookie to the binary source URL as accessToken or whatever you wanna call this param.

Hope this helps.

iwan-uschka avatar Nov 24 '20 10:11 iwan-uschka

For those arriving here from google, you should know that cordova iOS 6.0.0 uses WkWebView out of the box, and this plugin is no longer supported/required. There are still issues with that, however, and I'd encourage you to share some votes/contributions to the following:

  • Apple's issue: https://bugs.webkit.org/show_bug.cgi?id=213510
  • Niklas Merz's plugin, which bypasses WkWebView's native xhr handling: https://github.com/GEDYSIntraWare/cordova-plugin-webview-proxy

adamdport avatar Feb 24 '21 16:02 adamdport

How do you solve it now

Lyfei avatar Oct 13 '21 08:10 Lyfei

We are archiving this repository following Apache Cordova's Deprecation Policy. We will not continue to work on this repository. Therefore all issues and pull requests are being closed. Thanks for your contribution.

jcesarmobile avatar Jan 08 '23 23:01 jcesarmobile