Skip to content

Commit 232de7e

Browse files
Philipp Wallrichjigfox
authored andcommitted
Added new useWKCookieStore option
1 parent d54fd07 commit 232de7e

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

README.md

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

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

64+
- **useWKCookieStore**
65+
66+
Set `useWKCookieStore` to true to use the webView's `WKHTTPCookieStorage`. All Cookies from `sharedHTTPCookieStorage` will be copied to it.
67+
6468
- **source={{file: '', allowingReadAccessToURL: '' }}**
6569

6670
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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ class WKWebView extends React.Component {
232232
* Set this to true to emulate behavior of WebView component.
233233
*/
234234
sendCookies: PropTypes.bool,
235+
/**
236+
* Initializes the webView's WKHTTPCookieStorage and copies all cookies from sharedHTTPCookieStorage
237+
*/
238+
useWKCookieStore: PropTypes.bool,
235239
/**
236240
* If set to true, target="_blank" or window.open will be opened in WebView, instead
237241
* of new window. Default is false to be backward compatible.
@@ -316,7 +320,8 @@ class WKWebView extends React.Component {
316320
if (this.props.source && typeof this.props.source === 'object') {
317321
source = Object.assign({}, this.props.source, {
318322
sendCookies: this.props.sendCookies,
319-
customUserAgent: this.props.customUserAgent || this.props.userAgent
323+
customUserAgent: this.props.customUserAgent || this.props.userAgent,
324+
useWKCookieStore: this.props.useWKCookieStore
320325
});
321326

322327
if (this.props.html) {

ios/RCTWKWebView/RCTWKWebView.m

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

@@ -324,13 +325,13 @@ - (NSString *) cookieDescription:(NSHTTPCookie *)cookie {
324325
}
325326

326327
- (void) copyCookies {
327-
328+
328329
NSHTTPCookieStorage* storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
329330
NSArray* array = [storage cookies];
330-
331-
331+
332+
332333
if (@available(ios 11,*)) {
333-
334+
334335
// The webView websiteDataStore only gets initialized, when needed. Setting cookies on the dataStore's
335336
// httpCookieStore doesn't seem to initialize it. That's why fetchDataRecordsOfTypes is called.
336337
// All the cookies of the sharedHttpCookieStorage, which is used in react-native-cookie,
@@ -347,10 +348,10 @@ - (void) copyCookies {
347348
for (NSHTTPCookie* cookie in array){
348349
NSString* cookieSource = [NSString stringWithFormat:@"document.cookie = '%@'", [self cookieDescription:cookie]];
349350
WKUserScript* cookieScript = [[WKUserScript alloc]
350-
initWithSource:cookieSource
351-
injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
352-
353-
351+
initWithSource:cookieSource
352+
injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
353+
354+
354355
[_webView.configuration.userContentController addUserScript:cookieScript];
355356
}
356357
}
@@ -361,8 +362,9 @@ - (void)setSource:(NSDictionary *)source
361362
if (![_source isEqualToDictionary:source]) {
362363
_source = [source copy];
363364
_sendCookies = [source[@"sendCookies"] boolValue];
365+
_useWKCookieStore = [source[@"useWKCookieStore"] boolValue];
364366

365-
if (_sendCookies) {
367+
if (_useWKCookieStore) {
366368
[self copyCookies];
367369
}
368370

0 commit comments

Comments
 (0)