Browse Source

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}
kylemantesso 5 years ago
parent
commit
afadc62ada

+ 15
- 0
ios/RNCWKProcessPoolManager.h View File

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

+ 36
- 0
ios/RNCWKProcessPoolManager.m View File

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
+

+ 2
- 1
ios/RNCWKWebView.h View File

14
 @protocol RNCWKWebViewDelegate <NSObject>
14
 @protocol RNCWKWebViewDelegate <NSObject>
15
 
15
 
16
 - (BOOL)webView:(RNCWKWebView *)webView
16
 - (BOOL)webView:(RNCWKWebView *)webView
17
-shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
17
+   shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
18
    withCallback:(RCTDirectEventBlock)callback;
18
    withCallback:(RCTDirectEventBlock)callback;
19
 
19
 
20
 @end
20
 @end
38
 @property (nonatomic, assign) BOOL automaticallyAdjustContentInsets;
38
 @property (nonatomic, assign) BOOL automaticallyAdjustContentInsets;
39
 @property (nonatomic, assign) BOOL hideKeyboardAccessoryView;
39
 @property (nonatomic, assign) BOOL hideKeyboardAccessoryView;
40
 @property (nonatomic, assign) BOOL allowsBackForwardNavigationGestures;
40
 @property (nonatomic, assign) BOOL allowsBackForwardNavigationGestures;
41
+@property (nonatomic, assign) BOOL useSharedProcessPool;
41
 @property (nonatomic, copy) NSString *userAgent;
42
 @property (nonatomic, copy) NSString *userAgent;
42
 @property (nonatomic, assign) BOOL allowsLinkPreview;
43
 @property (nonatomic, assign) BOOL allowsLinkPreview;
43
 
44
 

+ 6
- 3
ios/RNCWKWebView.m View File

8
 #import "RNCWKWebView.h"
8
 #import "RNCWKWebView.h"
9
 #import <React/RCTConvert.h>
9
 #import <React/RCTConvert.h>
10
 #import <React/RCTAutoInsetsProtocol.h>
10
 #import <React/RCTAutoInsetsProtocol.h>
11
+#import "RNCWKProcessPoolManager.h"
11
 #import <UIKit/UIKit.h>
12
 #import <UIKit/UIKit.h>
12
 
13
 
13
 #import "objc/runtime.h"
14
 #import "objc/runtime.h"
60
   return _webkitAvailable;
61
   return _webkitAvailable;
61
 }
62
 }
62
 
63
 
63
-
64
 - (instancetype)initWithFrame:(CGRect)frame
64
 - (instancetype)initWithFrame:(CGRect)frame
65
 {
65
 {
66
   if ((self = [super initWithFrame:frame])) {
66
   if ((self = [super initWithFrame:frame])) {
81
     };
81
     };
82
 
82
 
83
     WKWebViewConfiguration *wkWebViewConfig = [WKWebViewConfiguration new];
83
     WKWebViewConfiguration *wkWebViewConfig = [WKWebViewConfiguration new];
84
+    if(self.useSharedProcessPool) {
85
+        wkWebViewConfig.processPool = [[RNCWKProcessPoolManager sharedManager] sharedProcessPool];
86
+    }
84
     wkWebViewConfig.userContentController = [WKUserContentController new];
87
     wkWebViewConfig.userContentController = [WKUserContentController new];
85
     [wkWebViewConfig.userContentController addScriptMessageHandler: self name: MessageHanderName];
88
     [wkWebViewConfig.userContentController addScriptMessageHandler: self name: MessageHanderName];
86
     wkWebViewConfig.allowsInlineMediaPlayback = _allowsInlineMediaPlayback;
89
     wkWebViewConfig.allowsInlineMediaPlayback = _allowsInlineMediaPlayback;
308
 /**
311
 /**
309
 * alert
312
 * alert
310
 */
313
 */
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
+{
313
     UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert];
316
     UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert];
314
     [alert addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
317
     [alert addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
315
         completionHandler();
318
         completionHandler();

+ 4
- 0
ios/RNCWKWebViewManager.m View File

71
   view.bounces = json == nil ? true : [RCTConvert BOOL: json];
71
   view.bounces = json == nil ? true : [RCTConvert BOOL: json];
72
 }
72
 }
73
 
73
 
74
+RCT_CUSTOM_VIEW_PROPERTY(useSharedProcessPool, BOOL, RNCWKWebView) {
75
+  view.useSharedProcessPool = json == nil ? true : [RCTConvert BOOL: json];
76
+}
77
+
74
 RCT_CUSTOM_VIEW_PROPERTY(scrollEnabled, BOOL, RNCWKWebView) {
78
 RCT_CUSTOM_VIEW_PROPERTY(scrollEnabled, BOOL, RNCWKWebView) {
75
   view.scrollEnabled = json == nil ? true : [RCTConvert BOOL: json];
79
   view.scrollEnabled = json == nil ? true : [RCTConvert BOOL: json];
76
 }
80
 }

+ 6
- 0
ios/RNCWebView.xcodeproj/project.pbxproj View File

7
 	objects = {
7
 	objects = {
8
 
8
 
9
 /* Begin PBXBuildFile section */
9
 /* Begin PBXBuildFile section */
10
+		3515965E21A3C86000623BFA /* RNCWKProcessPoolManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 3515965D21A3C86000623BFA /* RNCWKProcessPoolManager.m */; };
10
 		E914DBF6214474710071092B /* RNCUIWebViewManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E914DBF3214474710071092B /* RNCUIWebViewManager.m */; };
11
 		E914DBF6214474710071092B /* RNCUIWebViewManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E914DBF3214474710071092B /* RNCUIWebViewManager.m */; };
11
 		E914DBF7214474710071092B /* RNCUIWebView.m in Sources */ = {isa = PBXBuildFile; fileRef = E914DBF4214474710071092B /* RNCUIWebView.m */; };
12
 		E914DBF7214474710071092B /* RNCUIWebView.m in Sources */ = {isa = PBXBuildFile; fileRef = E914DBF4214474710071092B /* RNCUIWebView.m */; };
12
 		E91B351D21446E6C00F9801F /* RNCWKWebViewManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E91B351B21446E6C00F9801F /* RNCWKWebViewManager.m */; };
13
 		E91B351D21446E6C00F9801F /* RNCWKWebViewManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E91B351B21446E6C00F9801F /* RNCWKWebViewManager.m */; };
27
 
28
 
28
 /* Begin PBXFileReference section */
29
 /* Begin PBXFileReference section */
29
 		134814201AA4EA6300B7C361 /* libRNCWebView.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNCWebView.a; sourceTree = BUILT_PRODUCTS_DIR; };
30
 		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>"; };
30
 		E914DBF2214474710071092B /* RNCUIWebView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNCUIWebView.h; sourceTree = "<group>"; };
33
 		E914DBF2214474710071092B /* RNCUIWebView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNCUIWebView.h; sourceTree = "<group>"; };
31
 		E914DBF3214474710071092B /* RNCUIWebViewManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNCUIWebViewManager.m; sourceTree = "<group>"; };
34
 		E914DBF3214474710071092B /* RNCUIWebViewManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNCUIWebViewManager.m; sourceTree = "<group>"; };
32
 		E914DBF4214474710071092B /* RNCUIWebView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNCUIWebView.m; sourceTree = "<group>"; };
35
 		E914DBF4214474710071092B /* RNCUIWebView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNCUIWebView.m; sourceTree = "<group>"; };
67
 				E91B351C21446E6C00F9801F /* RNCWKWebView.m */,
70
 				E91B351C21446E6C00F9801F /* RNCWKWebView.m */,
68
 				E91B351921446E6C00F9801F /* RNCWKWebViewManager.h */,
71
 				E91B351921446E6C00F9801F /* RNCWKWebViewManager.h */,
69
 				E91B351B21446E6C00F9801F /* RNCWKWebViewManager.m */,
72
 				E91B351B21446E6C00F9801F /* RNCWKWebViewManager.m */,
73
+				3515965D21A3C86000623BFA /* RNCWKProcessPoolManager.m */,
74
+				3515965F21A3C87E00623BFA /* RNCWKProcessPoolManager.h */,
70
 				134814211AA4EA7D00B7C361 /* Products */,
75
 				134814211AA4EA7D00B7C361 /* Products */,
71
 			);
76
 			);
72
 			sourceTree = "<group>";
77
 			sourceTree = "<group>";
131
 				E914DBF7214474710071092B /* RNCUIWebView.m in Sources */,
136
 				E914DBF7214474710071092B /* RNCUIWebView.m in Sources */,
132
 				E914DBF6214474710071092B /* RNCUIWebViewManager.m in Sources */,
137
 				E914DBF6214474710071092B /* RNCUIWebViewManager.m in Sources */,
133
 				E91B351E21446E6C00F9801F /* RNCWKWebView.m in Sources */,
138
 				E91B351E21446E6C00F9801F /* RNCWKWebView.m in Sources */,
139
+				3515965E21A3C86000623BFA /* RNCWKProcessPoolManager.m in Sources */,
134
 			);
140
 			);
135
 			runOnlyForDeploymentPostprocessing = 0;
141
 			runOnlyForDeploymentPostprocessing = 0;
136
 		};
142
 		};

+ 2
- 0
js/WebView.ios.js View File

134
   static defaultProps = {
134
   static defaultProps = {
135
     useWebKit: true,
135
     useWebKit: true,
136
     originWhitelist: defaultOriginWhitelist,
136
     originWhitelist: defaultOriginWhitelist,
137
+    useSharedProcessPool: true,
137
   };
138
   };
138
 
139
 
139
   static isFileUploadSupported = async () => {
140
   static isFileUploadSupported = async () => {
265
           this.props.mediaPlaybackRequiresUserAction
266
           this.props.mediaPlaybackRequiresUserAction
266
         }
267
         }
267
         dataDetectorTypes={this.props.dataDetectorTypes}
268
         dataDetectorTypes={this.props.dataDetectorTypes}
269
+        useSharedProcessPool={this.props.useSharedProcessPool}
268
         allowsLinkPreview={this.props.allowsLinkPreview}
270
         allowsLinkPreview={this.props.allowsLinkPreview}
269
         {...nativeConfig.props}
271
         {...nativeConfig.props}
270
       />
272
       />

+ 7
- 0
js/WebViewTypes.js View File

232
    * back-forward list navigations.
232
    * back-forward list navigations.
233
    */
233
    */
234
   allowsBackForwardNavigationGestures?: ?boolean,
234
   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,
235
   /**
242
   /**
236
    * The custom user agent string.
243
    * The custom user agent string.
237
    */
244
    */