Skip to content

Commit

Permalink
Added new useWKCookieStore option
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Wallrich committed Jul 24, 2018
1 parent aeac4a2 commit 0e784be
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ If set to true, links with `target="_blank"` or `window.open` will be opened in

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.

- **useWKCookieStore**

Set `useWKCookieStore` to true to use the webView's `WKHTTPCookieStorage`. All Cookies from `sharedHTTPCookieStorage` will be copied to it.

- **source={{file: '', allowingReadAccessToURL: '' }}**

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.
Expand Down
7 changes: 6 additions & 1 deletion WKWebView.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ class WKWebView extends React.Component {
* Set this to true to emulate behavior of WebView component.
*/
sendCookies: PropTypes.bool,
/**
* Initializes the webView's WKHTTPCookieStorage and copies all cookies from sharedHTTPCookieStorage
*/
useWKCookieStore: PropTypes.bool,
/**
* If set to true, target="_blank" or window.open will be opened in WebView, instead
* of new window. Default is false to be backward compatible.
Expand Down Expand Up @@ -312,7 +316,8 @@ class WKWebView extends React.Component {
if (this.props.source && typeof this.props.source == 'object') {
source = Object.assign({}, this.props.source, {
sendCookies: this.props.sendCookies,
customUserAgent: this.props.customUserAgent || this.props.userAgent
customUserAgent: this.props.customUserAgent || this.props.userAgent,
useWKCookieStore: this.props.useWKCookieStore
});
}

Expand Down
20 changes: 11 additions & 9 deletions ios/RCTWKWebView/RCTWKWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ @interface RCTWKWebView () <WKNavigationDelegate, RCTAutoInsetsProtocol, WKScrip
@property (nonatomic, copy) RCTDirectEventBlock onMessage;
@property (nonatomic, copy) RCTDirectEventBlock onScroll;
@property (assign) BOOL sendCookies;
@property (assign) BOOL useWKCookieStore;
@property (nonatomic, strong) WKUserScript *atStartScript;
@property (nonatomic, strong) WKUserScript *atEndScript;

Expand Down Expand Up @@ -317,13 +318,13 @@ - (NSString *) cookieDescription:(NSHTTPCookie *)cookie {
}

- (void) copyCookies {

NSHTTPCookieStorage* storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* array = [storage cookies];


if (@available(ios 11,*)) {

// The webView websiteDataStore only gets initialized, when needed. Setting cookies on the dataStore's
// httpCookieStore doesn't seem to initialize it. That's why fetchDataRecordsOfTypes is called.
// All the cookies of the sharedHttpCookieStorage, which is used in react-native-cookie,
Expand All @@ -340,10 +341,10 @@ - (void) copyCookies {
for (NSHTTPCookie* cookie in array){
NSString* cookieSource = [NSString stringWithFormat:@"document.cookie = '%@'", [self cookieDescription:cookie]];
WKUserScript* cookieScript = [[WKUserScript alloc]
initWithSource:cookieSource
injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
initWithSource:cookieSource
injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];


[_webView.configuration.userContentController addUserScript:cookieScript];
}
}
Expand All @@ -354,8 +355,9 @@ - (void)setSource:(NSDictionary *)source
if (![_source isEqualToDictionary:source]) {
_source = [source copy];
_sendCookies = [source[@"sendCookies"] boolValue];
_useWKCookieStore = [source[@"useWKCookieStore"] boolValue];

if (_sendCookies) {
if (_useWKCookieStore) {
[self copyCookies];
}

Expand Down

0 comments on commit 0e784be

Please sign in to comment.