Skip to content

Commit

Permalink
feat(ios): Cookie sync improvements (#2535)
Browse files Browse the repository at this point in the history
  • Loading branch information
matiaskorhonen committed Nov 23, 2022
1 parent a5e2a9f commit 4ac0d74
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions apple/RNCWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ - (void)scrollWheel:(NSEvent *)theEvent {
@end
#endif // TARGET_OS_OSX

@interface RNCWebView () <WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler,
@interface RNCWebView () <WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler, WKHTTPCookieStoreObserver,
#if !TARGET_OS_OSX
UIScrollViewDelegate,
#endif // !TARGET_OS_OSX
Expand Down Expand Up @@ -234,6 +234,9 @@ - (void)startLongPress:(UILongPressGestureRecognizer *)pressSender
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
if (@available(iOS 11.0, *)) {
[self.webView.configuration.websiteDataStore.httpCookieStore removeObserver:self];
}
}

- (void)tappedMenuItem:(NSString *)eventType
Expand Down Expand Up @@ -1326,23 +1329,26 @@ -(void)appWillResignActive
*/
- (void)webView:(WKWebView *)webView
didFinishNavigation:(WKNavigation *)navigation
{
if (_ignoreSilentHardwareSwitch) {
[self forceIgnoreSilentHardwareSwitch:true];
}

if (_onLoadingFinish) {
_onLoadingFinish([self baseEvent]);
}
}

- (void)cookiesDidChangeInCookieStore:(WKHTTPCookieStore *)cookieStore
{
if(_sharedCookiesEnabled && @available(iOS 11.0, *)) {
// Write all cookies from WKWebView back to sharedHTTPCookieStorage
[webView.configuration.websiteDataStore.httpCookieStore getAllCookies:^(NSArray* cookies) {
[cookieStore getAllCookies:^(NSArray* cookies) {
for (NSHTTPCookie *cookie in cookies) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
}];
}

if (_ignoreSilentHardwareSwitch) {
[self forceIgnoreSilentHardwareSwitch:true];
}

if (_onLoadingFinish) {
_onLoadingFinish([self baseEvent]);
}
}

- (void)injectJavaScript:(NSString *)script
Expand Down Expand Up @@ -1559,7 +1565,9 @@ - (void)resetupScripts:(WKWebViewConfiguration *)wkWebViewConfig {
if(!_incognito && !_cacheEnabled) {
wkWebViewConfig.websiteDataStore = [WKWebsiteDataStore nonPersistentDataStore];
}
[self syncCookiesToWebView:nil];
[self syncCookiesToWebView:^{
[wkWebViewConfig.websiteDataStore.httpCookieStore addObserver:self];
}];
} else {
NSMutableString *script = [NSMutableString string];

Expand Down

0 comments on commit 4ac0d74

Please sign in to comment.