{"action":"created","issue":{"url":"https://api.github.com/repos/DriveTimeInc/cordova-plugin-cookie-persistence/issues/4","repository_url":"https://api.github.com/repos/DriveTimeInc/cordova-plugin-cookie-persistence","labels_url":"https://api.github.com/repos/DriveTimeInc/cordova-plugin-cookie-persistence/issues/4/labels{/name}","comments_url":"https://api.github.com/repos/DriveTimeInc/cordova-plugin-cookie-persistence/issues/4/comments","events_url":"https://api.github.com/repos/DriveTimeInc/cordova-plugin-cookie-persistence/issues/4/events","html_url":"https://github.com/DriveTimeInc/cordova-plugin-cookie-persistence/issues/4","id":305686364,"number":4,"title":"iOS Support - Restore Persisted Cookies to Web View","user":{"login":"gregberns","id":1767458,"avatar_url":"https://avatars0.githubusercontent.com/u/1767458?v=4","gravatar_id":"","url":"https://api.github.com/users/gregberns","html_url":"https://github.com/gregberns","followers_url":"https://api.github.com/users/gregberns/followers","following_url":"https://api.github.com/users/gregberns/following{/other_user}","gists_url":"https://api.github.com/users/gregberns/gists{/gist_id}","starred_url":"https://api.github.com/users/gregberns/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gregberns/subscriptions","organizations_url":"https://api.github.com/users/gregberns/orgs","repos_url":"https://api.github.com/users/gregberns/repos","events_url":"https://api.github.com/users/gregberns/events{/privacy}","received_events_url":"https://api.github.com/users/gregberns/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-03-15T19:18:48Z","updated_at":"2018-05-24T21:36:31Z","closed_at":null,"author_association":"COLLABORATOR","body":"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:\r\n\r\n1. Persist cookies to storage when the application is paused - see issue #3 \r\n2. **Restore persisted cookies to the web view so they're in included in first request** - this issue (#4)\r\n\r\n* Prefer to support iOS 10+\r\n* Prefer to use the `WKWebView` web view.\r\n\r\n## Basic Intuition (taken from Android implementation)\r\n\r\nOn plugin initialize:\r\n1. Retrieve stored cookies\r\n2. Set cookies in WebView prior to first Http request\r\n\r\n## Technical Challenges/Solutions\r\n\r\n* [Can I set the cookies to be used by a WKWebView?](https://stackoverflow.com/questions/26573137/can-i-set-the-cookies-to-be-used-by-a-wkwebview) - This is an interesting solution but since Cordova is responsible for the WebView creation and initialization, its difficult to see how this plugin can do the work. If the `WKWebView` is used the [cordova-plugin-wkwebview-engine](https://github.com/apache/cordova-plugin-wkwebview-engine) plugin may need to be modified to allow this plugin to inject the cookies.\r\n\r\n## Code\r\n\r\nI have not confirmed this code works, but it compiles. There may be parts that could be used in a solution.\r\nThis uses the `WKHTTPCookieStore` which is only available in iOS 11, so not optimal.\r\n\r\n```\r\n#import <Foundation/Foundation.h>\r\n#import \"CDVWKWebViewEngine.h\"\r\n#import \"CDVWKWebViewUIDelegate.h\"\r\n#import \"CDVWKProcessPoolFactory.h\"\r\n\r\n- (void)restoreCookie\r\n{\r\n    WKWebView* wkWebView = (WKWebView *)self.webView;\r\n\r\n    WKWebsiteDataStore* ds = WKWebsiteDataStore.defaultDataStore;\r\n    WKHTTPCookieStore* cs = ds.httpCookieStore;\r\n\r\n\r\n    NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];\r\n    [cookieProperties setObject:@\"testCookie\" forKey:NSHTTPCookieName];\r\n    [cookieProperties setObject:@\"someValue123456\" forKey:NSHTTPCookieValue];\r\n    //[cookieProperties setObject:@\"dt-mobile-test-west-mobile-app.azurewebsites.net\" forKey:NSHTTPCookieDomain];\r\n    //[cookieProperties setObject:@\"dt-mobile-test-west-mobile-app.azurewebsites.net\" forKey:NSHTTPCookieOriginURL];\r\n    //[cookieProperties setObject:@\"/\" forKey:NSHTTPCookiePath];\r\n    //[cookieProperties setObject:@\"0\" forKey:NSHTTPCookieVersion];\r\n\r\n    // set expiration to one month from now or any NSDate of your choosing\r\n    // this makes the cookie sessionless and it will persist across web sessions and app launches\r\n    /// if you want the cookie to be destroyed when your app exits, don't set this\r\n    [cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];\r\n\r\n    NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];\r\n\r\n    [cs setCookie:cookie completionHandler:^{\r\n        \r\n    }];\r\n    \r\n    wkWebView.configuration.websiteDataStore = ds;\r\n}\r\n```\r\n\r\nSetting the domain. If the cookie's domain needs to be set, you can get the config's domain through this code.\r\n\r\n```\r\n/**\r\n * Get the configured startup url in config.xml\r\n */\r\n- (NSString*)getConfigLaunchUrl\r\n{\r\n    CDVConfigParser* delegate = [[CDVConfigParser alloc] init];\r\n    NSString* configPath = [[NSBundle mainBundle] pathForResource:@\"config\" ofType:@\"xml\"];\r\n    NSURL* configUrl = [NSURL fileURLWithPath:configPath];\r\n \r\n    NSXMLParser* configParser = [[NSXMLParser alloc] initWithContentsOfURL:configUrl];\r\n    [configParser setDelegate:((id < NSXMLParserDelegate >)delegate)];\r\n    [configParser parse];\r\n \r\n    return delegate.startPage;\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/DriveTimeInc/cordova-plugin-cookie-persistence/issues/comments/391870840","html_url":"https://github.com/DriveTimeInc/cordova-plugin-cookie-persistence/issues/4#issuecomment-391870840","issue_url":"https://api.github.com/repos/DriveTimeInc/cordova-plugin-cookie-persistence/issues/4","id":391870840,"user":{"login":"gregberns","id":1767458,"avatar_url":"https://avatars0.githubusercontent.com/u/1767458?v=4","gravatar_id":"","url":"https://api.github.com/users/gregberns","html_url":"https://github.com/gregberns","followers_url":"https://api.github.com/users/gregberns/followers","following_url":"https://api.github.com/users/gregberns/following{/other_user}","gists_url":"https://api.github.com/users/gregberns/gists{/gist_id}","starred_url":"https://api.github.com/users/gregberns/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gregberns/subscriptions","organizations_url":"https://api.github.com/users/gregberns/orgs","repos_url":"https://api.github.com/users/gregberns/repos","events_url":"https://api.github.com/users/gregberns/events{/privacy}","received_events_url":"https://api.github.com/users/gregberns/received_events","type":"User","site_admin":false},"created_at":"2018-05-24T21:36:31Z","updated_at":"2018-05-24T21:36:31Z","author_association":"COLLABORATOR","body":"We still haven't found a good solution to this. If any one is aware of a solution, please let us know."}}
{"actor":{"display_login":"gregberns"}}