Skip to content

Commit afadc62

Browse files
kylemantessoTitozzz
authored andcommitted
feat(WKWebview): Add shared process pool so cookies and localStorage are shared across webviews in iOS (#138)
* fix(WKWebview): [iOS] Add shared process pool so cookies and localStorage are shared across webviews (#68) * Add optional shared process pool BREAKING CHANGE: useSharedProcessPool prop is set to true by default. If you want the old behavior, please use useSharedProcessPool={false}
1 parent 335792c commit afadc62

8 files changed

+78
-4
lines changed

ios/RNCWKProcessPoolManager.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import <WebKit/WebKit.h>
9+
10+
@interface RNCWKProcessPoolManager : NSObject
11+
12+
+ (instancetype) sharedManager;
13+
- (WKProcessPool *)sharedProcessPool;
14+
15+
@end

ios/RNCWKProcessPoolManager.m

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import <Foundation/Foundation.h>
9+
#import "RNCWKProcessPoolManager.h"
10+
11+
@interface RNCWKProcessPoolManager() {
12+
WKProcessPool *_sharedProcessPool;
13+
}
14+
@end
15+
16+
@implementation RNCWKProcessPoolManager
17+
18+
+ (id) sharedManager {
19+
static RNCWKProcessPoolManager *_sharedManager = nil;
20+
@synchronized(self) {
21+
if(_sharedManager == nil) {
22+
_sharedManager = [[super alloc] init];
23+
}
24+
return _sharedManager;
25+
}
26+
}
27+
28+
- (WKProcessPool *)sharedProcessPool {
29+
if (!_sharedProcessPool) {
30+
_sharedProcessPool = [[WKProcessPool alloc] init];
31+
}
32+
return _sharedProcessPool;
33+
}
34+
35+
@end
36+

ios/RNCWKWebView.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
@protocol RNCWKWebViewDelegate <NSObject>
1515

1616
- (BOOL)webView:(RNCWKWebView *)webView
17-
shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
17+
shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
1818
withCallback:(RCTDirectEventBlock)callback;
1919

2020
@end
@@ -38,6 +38,7 @@ shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
3838
@property (nonatomic, assign) BOOL automaticallyAdjustContentInsets;
3939
@property (nonatomic, assign) BOOL hideKeyboardAccessoryView;
4040
@property (nonatomic, assign) BOOL allowsBackForwardNavigationGestures;
41+
@property (nonatomic, assign) BOOL useSharedProcessPool;
4142
@property (nonatomic, copy) NSString *userAgent;
4243
@property (nonatomic, assign) BOOL allowsLinkPreview;
4344

ios/RNCWKWebView.m

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#import "RNCWKWebView.h"
99
#import <React/RCTConvert.h>
1010
#import <React/RCTAutoInsetsProtocol.h>
11+
#import "RNCWKProcessPoolManager.h"
1112
#import <UIKit/UIKit.h>
1213

1314
#import "objc/runtime.h"
@@ -60,7 +61,6 @@ + (BOOL)dynamicallyLoadWebKitIfAvailable
6061
return _webkitAvailable;
6162
}
6263

63-
6464
- (instancetype)initWithFrame:(CGRect)frame
6565
{
6666
if ((self = [super initWithFrame:frame])) {
@@ -81,6 +81,9 @@ - (void)didMoveToWindow
8181
};
8282

8383
WKWebViewConfiguration *wkWebViewConfig = [WKWebViewConfiguration new];
84+
if(self.useSharedProcessPool) {
85+
wkWebViewConfig.processPool = [[RNCWKProcessPoolManager sharedManager] sharedProcessPool];
86+
}
8487
wkWebViewConfig.userContentController = [WKUserContentController new];
8588
[wkWebViewConfig.userContentController addScriptMessageHandler: self name: MessageHanderName];
8689
wkWebViewConfig.allowsInlineMediaPlayback = _allowsInlineMediaPlayback;
@@ -308,8 +311,8 @@ - (void)layoutSubviews
308311
/**
309312
* alert
310313
*/
311-
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
312-
{
314+
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
315+
{
313316
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert];
314317
[alert addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
315318
completionHandler();

ios/RNCWKWebViewManager.m

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ - (UIView *)view
7171
view.bounces = json == nil ? true : [RCTConvert BOOL: json];
7272
}
7373

74+
RCT_CUSTOM_VIEW_PROPERTY(useSharedProcessPool, BOOL, RNCWKWebView) {
75+
view.useSharedProcessPool = json == nil ? true : [RCTConvert BOOL: json];
76+
}
77+
7478
RCT_CUSTOM_VIEW_PROPERTY(scrollEnabled, BOOL, RNCWKWebView) {
7579
view.scrollEnabled = json == nil ? true : [RCTConvert BOOL: json];
7680
}

ios/RNCWebView.xcodeproj/project.pbxproj

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
3515965E21A3C86000623BFA /* RNCWKProcessPoolManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 3515965D21A3C86000623BFA /* RNCWKProcessPoolManager.m */; };
1011
E914DBF6214474710071092B /* RNCUIWebViewManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E914DBF3214474710071092B /* RNCUIWebViewManager.m */; };
1112
E914DBF7214474710071092B /* RNCUIWebView.m in Sources */ = {isa = PBXBuildFile; fileRef = E914DBF4214474710071092B /* RNCUIWebView.m */; };
1213
E91B351D21446E6C00F9801F /* RNCWKWebViewManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E91B351B21446E6C00F9801F /* RNCWKWebViewManager.m */; };
@@ -27,6 +28,8 @@
2728

2829
/* Begin PBXFileReference section */
2930
134814201AA4EA6300B7C361 /* libRNCWebView.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNCWebView.a; sourceTree = BUILT_PRODUCTS_DIR; };
31+
3515965D21A3C86000623BFA /* RNCWKProcessPoolManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNCWKProcessPoolManager.m; sourceTree = "<group>"; };
32+
3515965F21A3C87E00623BFA /* RNCWKProcessPoolManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNCWKProcessPoolManager.h; sourceTree = "<group>"; };
3033
E914DBF2214474710071092B /* RNCUIWebView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNCUIWebView.h; sourceTree = "<group>"; };
3134
E914DBF3214474710071092B /* RNCUIWebViewManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNCUIWebViewManager.m; sourceTree = "<group>"; };
3235
E914DBF4214474710071092B /* RNCUIWebView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNCUIWebView.m; sourceTree = "<group>"; };
@@ -67,6 +70,8 @@
6770
E91B351C21446E6C00F9801F /* RNCWKWebView.m */,
6871
E91B351921446E6C00F9801F /* RNCWKWebViewManager.h */,
6972
E91B351B21446E6C00F9801F /* RNCWKWebViewManager.m */,
73+
3515965D21A3C86000623BFA /* RNCWKProcessPoolManager.m */,
74+
3515965F21A3C87E00623BFA /* RNCWKProcessPoolManager.h */,
7075
134814211AA4EA7D00B7C361 /* Products */,
7176
);
7277
sourceTree = "<group>";
@@ -131,6 +136,7 @@
131136
E914DBF7214474710071092B /* RNCUIWebView.m in Sources */,
132137
E914DBF6214474710071092B /* RNCUIWebViewManager.m in Sources */,
133138
E91B351E21446E6C00F9801F /* RNCWKWebView.m in Sources */,
139+
3515965E21A3C86000623BFA /* RNCWKProcessPoolManager.m in Sources */,
134140
);
135141
runOnlyForDeploymentPostprocessing = 0;
136142
};

js/WebView.ios.js

+2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class WebView extends React.Component<WebViewSharedProps, State> {
134134
static defaultProps = {
135135
useWebKit: true,
136136
originWhitelist: defaultOriginWhitelist,
137+
useSharedProcessPool: true,
137138
};
138139

139140
static isFileUploadSupported = async () => {
@@ -265,6 +266,7 @@ class WebView extends React.Component<WebViewSharedProps, State> {
265266
this.props.mediaPlaybackRequiresUserAction
266267
}
267268
dataDetectorTypes={this.props.dataDetectorTypes}
269+
useSharedProcessPool={this.props.useSharedProcessPool}
268270
allowsLinkPreview={this.props.allowsLinkPreview}
269271
{...nativeConfig.props}
270272
/>

js/WebViewTypes.js

+7
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ export type IOSWebViewProps = $ReadOnly<{|
232232
* back-forward list navigations.
233233
*/
234234
allowsBackForwardNavigationGestures?: ?boolean,
235+
/**
236+
* A Boolean value indicating whether WebKit WebView should be created using a shared
237+
* process pool, enabling WebViews to share cookies and localStorage between each other.
238+
* Default is true but can be set to false for backwards compatibility.
239+
* @platform ios
240+
*/
241+
useSharedProcessPool?: ?boolean,
235242
/**
236243
* The custom user agent string.
237244
*/

0 commit comments

Comments
 (0)