drivetimeinc / cordova-plugin-cookie-persistence Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
Hi - thank you for doing this - have been pulling out my limited hair trying to get the server and session cookies to persist. I had determined that a Cordova Suspend was saving server cookies and a resume was returning them. Was about to try and figure out what it was doing...
We'd like to support iOS devices with this plugin but the are technical challenges due to Apple's SDK implementation. This plugin does two things, this issue focuses on persisting the cookies:
Prefer to support iOS 10+.
There are two ways to to this:
document.cookie
), and sends them to the native layerHaving JS call into the native code with cookies to store should work fine, but using all native code may be better. Lets look at options on how to do this.
To extract cookies on pause only through native code, there are known issues with getting access to the cookies reliably. See:
WKWebView Persistent Storage of Cookies - There are known issues getting cookies and it seems like people are just shooting in the dark
Getting all cookies from WKWebView - the NSHTTPCookieStorage.sharedHTTPCookieStorage()
method does not appear to work. The httpCookieStore
method doesn't support iOS 10 which would be good to support at this time.
/**
* Register the listener for pause and resume events.
*/
- (void) observeLifeCycle
{
NSNotificationCenter* listener = [NSNotificationCenter
defaultCenter];
[listener addObserver:self
selector:@selector(onPause)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
}
/**
* Call on app pause
*/
- (void) onPause
{
NSLog(@"CookiePersistencePlugin - onPause");
//fetch cookies from js layer
//storeCookies
//[self storeCookies:startPageUrl];
}
We'd like to support iOS devices with this plugin but the are technical challenges due to Apple's SDK implementation. This plugin does two things, this issue focuses on restoring saved cookies:
WKWebView
web view.On plugin initialize:
WKWebView
is used the cordova-plugin-wkwebview-engine plugin may need to be modified to allow this plugin to inject the cookies.I have not confirmed this code works, but it compiles. There may be parts that could be used in a solution.
This uses the WKHTTPCookieStore
which is only available in iOS 11, so not optimal.
#import <Foundation/Foundation.h>
#import "CDVWKWebViewEngine.h"
#import "CDVWKWebViewUIDelegate.h"
#import "CDVWKProcessPoolFactory.h"
- (void)restoreCookie
{
WKWebView* wkWebView = (WKWebView *)self.webView;
WKWebsiteDataStore* ds = WKWebsiteDataStore.defaultDataStore;
WKHTTPCookieStore* cs = ds.httpCookieStore;
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:@"testCookie" forKey:NSHTTPCookieName];
[cookieProperties setObject:@"someValue123456" forKey:NSHTTPCookieValue];
//[cookieProperties setObject:@"dt-mobile-test-west-mobile-app.azurewebsites.net" forKey:NSHTTPCookieDomain];
//[cookieProperties setObject:@"dt-mobile-test-west-mobile-app.azurewebsites.net" forKey:NSHTTPCookieOriginURL];
//[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
//[cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];
// set expiration to one month from now or any NSDate of your choosing
// this makes the cookie sessionless and it will persist across web sessions and app launches
/// if you want the cookie to be destroyed when your app exits, don't set this
[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
[cs setCookie:cookie completionHandler:^{
}];
wkWebView.configuration.websiteDataStore = ds;
}
Setting the domain. If the cookie's domain needs to be set, you can get the config's domain through this code.
/**
* Get the configured startup url in config.xml
*/
- (NSString*)getConfigLaunchUrl
{
CDVConfigParser* delegate = [[CDVConfigParser alloc] init];
NSString* configPath = [[NSBundle mainBundle] pathForResource:@"config" ofType:@"xml"];
NSURL* configUrl = [NSURL fileURLWithPath:configPath];
NSXMLParser* configParser = [[NSXMLParser alloc] initWithContentsOfURL:configUrl];
[configParser setDelegate:((id < NSXMLParserDelegate >)delegate)];
[configParser parse];
return delegate.startPage;
}
From @kyleamadio in #3
If you can expose a method to call the storage logic from JS at anytime - we can then call the code to refresh the Cookies details whenever we make a HTTPs call to the server. That way we will not need to worry about Exit/Closing of the app as the developer can manage the cookie refresh as they are loaded from the the web server.
Hi Greg - wonder if you are seeing this. we have a cookie
magical .dev.magic.al secure
If we kill the app - on start up we get two cookies, the duplicate drops the leading .
Its also random in that any one of our 5 or so cookies will duplicate.
magical .dev.magic.al secure
magical dev.magic.al
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.