ionic-team / cordova-plugin-ionic-webview

Web View plugin for Cordova, specialized for Ionic apps.
Apache License 2.0
487 stars 395 forks source link

Cookies are not persistent and are lost causing to lose session variables #633

Closed allanevargas closed 3 years ago

allanevargas commented 3 years ago

This thread was initially a question regarding my issue but now seem to have found a fix so I thought I would update it so others with a similar issue could apply the fix to their application.

The Issue My initial issue was that I had to upgrade my UIWebView application to use WKWebView as it was getting rejected when I submitted it to the app store. So I upgraded to WKWebView but this caused issues for my app. I have an ionic-v1 app that communicates with my backend which is all PHP. I use PHP $_SESSION variables for my backend and when I upgraded to WKWebView, this caused my $_SESSION variables to be lost on each page so it failed to retrieve data. I then inspected this using the Safari dev tools and noticed that for each HTTP request, it was using a different PHPSESSID/Cookie rather than re-using the one when the session was started. This wasn't the case for Android, it worked fine and it was using the same session ID every single request until it was destroyed.

The Solution Now, this might not work for you but do check my versions of dependencies and plugins down below. There was a lot of trial and error on my part after searching for a fix and I really tried to avoid a fix that would only work for a certain version of iOS, Cordova, etc.

I installed this plugin: ionic cordova plugin add cordova-plugin-wkwebview-file-xhr

Javascript/AngularJS I already set this globally to my project $httpProvider.defaults.withCredentials = true; Here is a sample HTTP request that's used throughout my app

$http.get("https://demo.myapp.com/pages/login", { transformResponse: [] })
.success(function(data, status, headers, config) 
{
      try
      {}
      catch (e)
      {}
})
.error( function(data, status, headers, config) 
{
}); 

PHP Header Files

header("Access-Control-Allow-Origin: http://localhost");
header("Content-Type: application/json");
header("Access-Control-Allow-Headers: content-type,authorization");
header("Access-Control-Allow-Credentials: true");

Cordova Version 10.0.0

cordova-ios Version 6.1.1

macOS macOS Big Sur Version 11.1

Safari Version Version 14.0.2

Xcode Version Version 12.3

Config.xml

<access origin="*" />
<allow-navigation href="*" />
<preference name="WKWebViewOnly" value="true" />
<feature name="CDVWKWebViewEngine">`                                                                                                                                                                                                                                        
<param name="ios-package" value="CDVWKWebViewEngine" />
</feature>
<preference name="CordovaWebViewEngine" value="CDVWKWebViewEngine" />
<preference name="scheme" value="app" />
<preference name="hostname" value="localhost" />
<preference name="deployment-target" value="11.0.0" />

Plugin List

cordova-plugin-android-permissions 1.1.0 "Permissions"
cordova-plugin-androidx-adapter 1.1.1 "cordova-plugin-androidx-adapter"
cordova-plugin-androidx 2.0.0 "cordova-plugin-androidx"
cordova-plugin-camera 4.1.0 "Camera"
cordova-plugin-device 2.0.3 "Device"
cordova-plugin-file-opener2 3.0.5 "File Opener2"
cordova-plugin-file-transfer 1.7.1 "File Transfer"
cordova-plugin-file 6.0.2 "File"
cordova-plugin-firebasex 11.0.1 "Google Firebase Plugin"
cordova-plugin-inappbrowser 4.0.0 "InAppBrowser"
cordova-plugin-ionic-keyboard 2.2.0 "cordova-plugin-ionic-keyboard"
cordova-plugin-ionic-webview 5.0.0 "cordova-plugin-ionic-webview"
cordova-plugin-splashscreen 6.0.0 "Splashscreen"
cordova-plugin-statusbar 2.4.3 "StatusBar"
cordova-plugin-whitelist 1.3.4 "Whitelist"
cordova-plugin-wkwebview-file-xhr 3.0.0 "Cordova WKWebView File XHR Plugin"
cordova-plugin-x-toast 2.7.2 "Toast"
allanevargas commented 3 years ago

Read The Solution in this thread for my fix

Taxman972 commented 3 years ago

See the thread for the fix

Which thread please ?

allanevargas commented 3 years ago

See the thread for the fix

Which thread please ?

This one. The fix that worked for me is under The Solution on my first post of this thread.

rafaellop commented 3 years ago

This works https://github.com/CWBudde/cordova-plugin-wkwebview-inject-cookie/issues/11#issuecomment-883249020

acedigibits commented 3 years ago

See the thread for the fix

Which thread please ?

This one. The fix that worked for me is under The Solution on my first post of this thread.

There is no link on all of your SOLUTION messages. can you Kindly type the link ?