Giter Site home page Giter Site logo

cordova-plugin-cookie-persistence's People

Contributors

gregberns avatar mattmcdonald39 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cordova-plugin-cookie-persistence's Issues

Cookie Plugin

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

iOS Support - Persist Cookies on App Pause

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:

  1. Persist cookies to storage when the application is paused - This issue (#3)
  2. Restore persisted cookies to the web view so they're in included in first request - See Issue #4

Prefer to support iOS 10+.

Basic Intuition (taken from Android implementation)

  1. Detect when the application is paused
  2. Extract the cookie string from the web view
  3. Store the cookie string in a text file

Detect onPause

There are two ways to to this:

  1. Add a native onPause listener which triggers a method to extract the cookies
  2. In JS, add an onPause listener which calls get cookies (document.cookie), and sends them to the native layer

Technical Challenges

Having 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:

Code

Event Listener

/**
 * 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];
}

iOS Support - Restore Persisted Cookies to Web View

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:

  1. Persist cookies to storage when the application is paused - see issue #3
  2. Restore persisted cookies to the web view so they're in included in first request - this issue (#4)
  • Prefer to support iOS 10+
  • Prefer to use the WKWebView web view.

Basic Intuition (taken from Android implementation)

On plugin initialize:

  1. Retrieve stored cookies
  2. Set cookies in WebView prior to first Http request

Technical Challenges/Solutions

Code

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;
}

Method to Cause Cookies to be Persisted

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.

Duplicate Cookies - Different domain

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.