diff --git a/ios/RNCWKProcessPoolManager.h b/ios/RNCWKProcessPoolManager.h new file mode 100644 index 0000000..6d2d215 --- /dev/null +++ b/ios/RNCWKProcessPoolManager.h @@ -0,0 +1,15 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import + +@interface RNCWKProcessPoolManager : NSObject + ++ (instancetype) sharedManager; +- (WKProcessPool *)sharedProcessPool; + +@end diff --git a/ios/RNCWKProcessPoolManager.m b/ios/RNCWKProcessPoolManager.m new file mode 100644 index 0000000..27952b8 --- /dev/null +++ b/ios/RNCWKProcessPoolManager.m @@ -0,0 +1,36 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import +#import "RNCWKProcessPoolManager.h" + +@interface RNCWKProcessPoolManager() { + WKProcessPool *_sharedProcessPool; +} +@end + +@implementation RNCWKProcessPoolManager + ++ (id) sharedManager { + static RNCWKProcessPoolManager *_sharedManager = nil; + @synchronized(self) { + if(_sharedManager == nil) { + _sharedManager = [[super alloc] init]; + } + return _sharedManager; + } +} + +- (WKProcessPool *)sharedProcessPool { + if (!_sharedProcessPool) { + _sharedProcessPool = [[WKProcessPool alloc] init]; + } + return _sharedProcessPool; +} + +@end + diff --git a/ios/RNCWKWebView.h b/ios/RNCWKWebView.h index 9592cc9..0abc630 100644 --- a/ios/RNCWKWebView.h +++ b/ios/RNCWKWebView.h @@ -14,7 +14,7 @@ @protocol RNCWKWebViewDelegate - (BOOL)webView:(RNCWKWebView *)webView -shouldStartLoadForRequest:(NSMutableDictionary *)request + shouldStartLoadForRequest:(NSMutableDictionary *)request withCallback:(RCTDirectEventBlock)callback; @end @@ -38,6 +38,7 @@ shouldStartLoadForRequest:(NSMutableDictionary *)request @property (nonatomic, assign) BOOL automaticallyAdjustContentInsets; @property (nonatomic, assign) BOOL hideKeyboardAccessoryView; @property (nonatomic, assign) BOOL allowsBackForwardNavigationGestures; +@property (nonatomic, assign) BOOL useSharedProcessPool; @property (nonatomic, copy) NSString *userAgent; @property (nonatomic, assign) BOOL allowsLinkPreview; diff --git a/ios/RNCWKWebView.m b/ios/RNCWKWebView.m index 060c7de..6680206 100644 --- a/ios/RNCWKWebView.m +++ b/ios/RNCWKWebView.m @@ -8,6 +8,7 @@ #import "RNCWKWebView.h" #import #import +#import "RNCWKProcessPoolManager.h" #import #import "objc/runtime.h" @@ -60,7 +61,6 @@ static NSString *const MessageHanderName = @"ReactNative"; return _webkitAvailable; } - - (instancetype)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { @@ -81,6 +81,9 @@ static NSString *const MessageHanderName = @"ReactNative"; }; WKWebViewConfiguration *wkWebViewConfig = [WKWebViewConfiguration new]; + if(self.useSharedProcessPool) { + wkWebViewConfig.processPool = [[RNCWKProcessPoolManager sharedManager] sharedProcessPool]; + } wkWebViewConfig.userContentController = [WKUserContentController new]; [wkWebViewConfig.userContentController addScriptMessageHandler: self name: MessageHanderName]; wkWebViewConfig.allowsInlineMediaPlayback = _allowsInlineMediaPlayback; @@ -308,8 +311,8 @@ static NSString *const MessageHanderName = @"ReactNative"; /** * alert */ -- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler -{ +- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler +{ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { completionHandler(); diff --git a/ios/RNCWKWebViewManager.m b/ios/RNCWKWebViewManager.m index c268064..d68491c 100644 --- a/ios/RNCWKWebViewManager.m +++ b/ios/RNCWKWebViewManager.m @@ -71,6 +71,10 @@ RCT_CUSTOM_VIEW_PROPERTY(bounces, BOOL, RNCWKWebView) { view.bounces = json == nil ? true : [RCTConvert BOOL: json]; } +RCT_CUSTOM_VIEW_PROPERTY(useSharedProcessPool, BOOL, RNCWKWebView) { + view.useSharedProcessPool = json == nil ? true : [RCTConvert BOOL: json]; +} + RCT_CUSTOM_VIEW_PROPERTY(scrollEnabled, BOOL, RNCWKWebView) { view.scrollEnabled = json == nil ? true : [RCTConvert BOOL: json]; } diff --git a/ios/RNCWebView.xcodeproj/project.pbxproj b/ios/RNCWebView.xcodeproj/project.pbxproj index 6ea3e41..f40ddb4 100644 --- a/ios/RNCWebView.xcodeproj/project.pbxproj +++ b/ios/RNCWebView.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 3515965E21A3C86000623BFA /* RNCWKProcessPoolManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 3515965D21A3C86000623BFA /* RNCWKProcessPoolManager.m */; }; E914DBF6214474710071092B /* RNCUIWebViewManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E914DBF3214474710071092B /* RNCUIWebViewManager.m */; }; E914DBF7214474710071092B /* RNCUIWebView.m in Sources */ = {isa = PBXBuildFile; fileRef = E914DBF4214474710071092B /* RNCUIWebView.m */; }; E91B351D21446E6C00F9801F /* RNCWKWebViewManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E91B351B21446E6C00F9801F /* RNCWKWebViewManager.m */; }; @@ -27,6 +28,8 @@ /* Begin PBXFileReference section */ 134814201AA4EA6300B7C361 /* libRNCWebView.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNCWebView.a; sourceTree = BUILT_PRODUCTS_DIR; }; + 3515965D21A3C86000623BFA /* RNCWKProcessPoolManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNCWKProcessPoolManager.m; sourceTree = ""; }; + 3515965F21A3C87E00623BFA /* RNCWKProcessPoolManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNCWKProcessPoolManager.h; sourceTree = ""; }; E914DBF2214474710071092B /* RNCUIWebView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNCUIWebView.h; sourceTree = ""; }; E914DBF3214474710071092B /* RNCUIWebViewManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNCUIWebViewManager.m; sourceTree = ""; }; E914DBF4214474710071092B /* RNCUIWebView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNCUIWebView.m; sourceTree = ""; }; @@ -67,6 +70,8 @@ E91B351C21446E6C00F9801F /* RNCWKWebView.m */, E91B351921446E6C00F9801F /* RNCWKWebViewManager.h */, E91B351B21446E6C00F9801F /* RNCWKWebViewManager.m */, + 3515965D21A3C86000623BFA /* RNCWKProcessPoolManager.m */, + 3515965F21A3C87E00623BFA /* RNCWKProcessPoolManager.h */, 134814211AA4EA7D00B7C361 /* Products */, ); sourceTree = ""; @@ -131,6 +136,7 @@ E914DBF7214474710071092B /* RNCUIWebView.m in Sources */, E914DBF6214474710071092B /* RNCUIWebViewManager.m in Sources */, E91B351E21446E6C00F9801F /* RNCWKWebView.m in Sources */, + 3515965E21A3C86000623BFA /* RNCWKProcessPoolManager.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/js/WebView.ios.js b/js/WebView.ios.js index aa5afe1..bc66ffb 100644 --- a/js/WebView.ios.js +++ b/js/WebView.ios.js @@ -134,6 +134,7 @@ class WebView extends React.Component { static defaultProps = { useWebKit: true, originWhitelist: defaultOriginWhitelist, + useSharedProcessPool: true, }; static isFileUploadSupported = async () => { @@ -265,6 +266,7 @@ class WebView extends React.Component { this.props.mediaPlaybackRequiresUserAction } dataDetectorTypes={this.props.dataDetectorTypes} + useSharedProcessPool={this.props.useSharedProcessPool} allowsLinkPreview={this.props.allowsLinkPreview} {...nativeConfig.props} /> diff --git a/js/WebViewTypes.js b/js/WebViewTypes.js index 8afc68a..c787c0b 100644 --- a/js/WebViewTypes.js +++ b/js/WebViewTypes.js @@ -232,6 +232,13 @@ export type IOSWebViewProps = $ReadOnly<{| * back-forward list navigations. */ allowsBackForwardNavigationGestures?: ?boolean, + /** + * A Boolean value indicating whether WebKit WebView should be created using a shared + * process pool, enabling WebViews to share cookies and localStorage between each other. + * Default is true but can be set to false for backwards compatibility. + * @platform ios + */ + useSharedProcessPool?: ?boolean, /** * The custom user agent string. */