Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Cookies are not set/stored in iOS #77

Closed
FarhadG opened this issue Feb 5, 2019 · 28 comments
Closed

Cookies are not set/stored in iOS #77

FarhadG opened this issue Feb 5, 2019 · 28 comments
Labels

Comments

@FarhadG
Copy link

FarhadG commented Feb 5, 2019

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
@williamhilldevs
Copy link

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

@adam-h
Copy link

adam-h commented Feb 11, 2019

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

@FarhadG
Copy link
Author

FarhadG commented Feb 11, 2019

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.

@adam-h
Copy link

adam-h commented Feb 12, 2019

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.

@FarhadG
Copy link
Author

FarhadG commented Feb 12, 2019

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

@EiyuuZack
Copy link

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...

@janpio janpio added the bug label Jun 19, 2019
@janpio
Copy link
Member

janpio commented Jun 19, 2019

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.)

@mgatto
Copy link

mgatto commented Aug 8, 2019

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

@devjva
Copy link

devjva commented Oct 7, 2019

I have the same issue.

@tajinder-logiciel
Copy link

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

@matrixreal
Copy link

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

@lucky3491
Copy link

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

@pph7
Copy link

pph7 commented May 29, 2020

I have the same issue. Seems very serious.

@Aakash9974
Copy link

Can anyone please help here...

@rezunenko
Copy link

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

@lucky3491
Copy link

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

@rezunenko
Copy link

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

@jnemecek10
Copy link

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.

@ghost
Copy link

ghost commented Oct 21, 2020

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.

@lucky3491
Copy link

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 lucky3491/cordova-plugin-wkwebview-inject-cookie#1 (comment)

@iwan-uschka
Copy link

iwan-uschka commented Oct 26, 2020

Like @breautek mentioned in apache/cordova-ios#883 (comment) 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.

@samarroy
Copy link

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

@iwan-uschka
Copy link

iwan-uschka commented Nov 3, 2020

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.

@StasKalishenko
Copy link

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

@iwan-uschka
Copy link

@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.

@adamdport
Copy link

adamdport commented Feb 24, 2021

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:

@Lyfei
Copy link

Lyfei commented Oct 13, 2021

How do you solve it now

@jcesarmobile
Copy link
Member

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 jcesarmobile closed this as not planned Won't fix, can't repro, duplicate, stale Jan 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.