Skip to content

Commit 3eac1f5

Browse files
Philipp Wallrichjigfox
authored andcommitted
Added new useWKCookieStore option
1 parent ddf71c8 commit 3eac1f5

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ If set to true, links with `target="_blank"` or `window.open` will be opened in
5757

5858
Set `sendCookies` to true to copy cookies from `sharedHTTPCookieStorage` when calling loadRequest. This emulates the behavior of react-native's `WebView` component. You can set cookies using `react-native-cookies` Default is false.
5959

60+
- **useWKCookieStore**
61+
62+
Set `useWKCookieStore` to true to use the webView's `WKHTTPCookieStorage`. All Cookies from `sharedHTTPCookieStorage` will be copied to it.
63+
6064
- **source={{file: '', allowingReadAccessToURL: '' }}**
6165

6266
This allows WKWebView loads a local HTML file. Please note the underlying API is only introduced in iOS 9+. So in iOS 8, it will simple ignores these two properties.

WKWebView.ios.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ class WKWebView extends React.Component {
228228
* Set this to true to emulate behavior of WebView component.
229229
*/
230230
sendCookies: PropTypes.bool,
231+
/**
232+
* Initializes the webView's WKHTTPCookieStorage and copies all cookies from sharedHTTPCookieStorage
233+
*/
234+
useWKCookieStore: PropTypes.bool,
231235
/**
232236
* If set to true, target="_blank" or window.open will be opened in WebView, instead
233237
* of new window. Default is false to be backward compatible.
@@ -313,6 +317,7 @@ class WKWebView extends React.Component {
313317
source.sendCookies = this.props.sendCookies;
314318
source.customUserAgent =
315319
this.props.customUserAgent || this.props.userAgent;
320+
source.useWKCookieStore: this.props.useWKCookieStore;
316321
}
317322

318323
if (this.props.html) {

ios/RCTWKWebView/RCTWKWebView.m

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ @interface RCTWKWebView () <WKNavigationDelegate, RCTAutoInsetsProtocol, WKScrip
3434
@property (nonatomic, copy) RCTDirectEventBlock onMessage;
3535
@property (nonatomic, copy) RCTDirectEventBlock onScroll;
3636
@property (assign) BOOL sendCookies;
37+
@property (assign) BOOL useWKCookieStore;
3738
@property (nonatomic, strong) WKUserScript *atStartScript;
3839
@property (nonatomic, strong) WKUserScript *atEndScript;
3940

@@ -317,13 +318,13 @@ - (NSString *) cookieDescription:(NSHTTPCookie *)cookie {
317318
}
318319

319320
- (void) copyCookies {
320-
321+
321322
NSHTTPCookieStorage* storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
322323
NSArray* array = [storage cookies];
323-
324-
324+
325+
325326
if (@available(ios 11,*)) {
326-
327+
327328
// The webView websiteDataStore only gets initialized, when needed. Setting cookies on the dataStore's
328329
// httpCookieStore doesn't seem to initialize it. That's why fetchDataRecordsOfTypes is called.
329330
// All the cookies of the sharedHttpCookieStorage, which is used in react-native-cookie,
@@ -340,10 +341,10 @@ - (void) copyCookies {
340341
for (NSHTTPCookie* cookie in array){
341342
NSString* cookieSource = [NSString stringWithFormat:@"document.cookie = '%@'", [self cookieDescription:cookie]];
342343
WKUserScript* cookieScript = [[WKUserScript alloc]
343-
initWithSource:cookieSource
344-
injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
345-
346-
344+
initWithSource:cookieSource
345+
injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
346+
347+
347348
[_webView.configuration.userContentController addUserScript:cookieScript];
348349
}
349350
}
@@ -354,8 +355,9 @@ - (void)setSource:(NSDictionary *)source
354355
if (![_source isEqualToDictionary:source]) {
355356
_source = [source copy];
356357
_sendCookies = [source[@"sendCookies"] boolValue];
358+
_useWKCookieStore = [source[@"useWKCookieStore"] boolValue];
357359

358-
if (_sendCookies) {
360+
if (_useWKCookieStore) {
359361
[self copyCookies];
360362
}
361363

0 commit comments

Comments
 (0)