Skip to content

Commit 28b058c

Browse files
RSNarafacebook-github-bot
authored andcommitted
Dynamically load WebKit
Summary: @public We can't dynamically link `WebKit` because doing so will impact cold start of all our Apps. This diff includes a few changes: 1. Weakly link the `WebKit` framework in the `ReactInternal` library, so that the compiler doesn't die when it encounters a WebKit symbol. 2. Undo dynamic linking of WebKit in Catalyst. 3. Undo dynamic linking of WebKit in AdsManager 4. Before the first `WKWebView` is instantiated, dynamically load the `WebKit` framework. The end result of these changes is that WebKit will be loaded only when it's going to be used. Reviewed By: mmmulani Differential Revision: D6564328 fbshipit-source-id: a45a44e774d0c61c1fb578a6fa3d16bb08f68ac9
1 parent b18fdda commit 28b058c

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

React/Base/RCTConvert.h

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ typedef NSURL RCTFileURL;
6969
+ (UIReturnKeyType)UIReturnKeyType:(id)json;
7070
#if !TARGET_OS_TV
7171
+ (UIDataDetectorTypes)UIDataDetectorTypes:(id)json;
72+
#endif
73+
74+
#if TARGET_OS_IPHONE
7275
+ (WKDataDetectorTypes)WKDataDetectorTypes:(id)json;
7376
#endif
7477

React/React.xcodeproj/project.pbxproj

+10-2
Original file line numberDiff line numberDiff line change
@@ -5240,7 +5240,11 @@
52405240
"RCT_METRO_PORT=${RCT_METRO_PORT}",
52415241
);
52425242
GCC_WARN_ABOUT_MISSING_NEWLINE = YES;
5243-
OTHER_LDFLAGS = "-ObjC";
5243+
OTHER_LDFLAGS = (
5244+
"-ObjC",
5245+
"-weak_framework",
5246+
WebKit,
5247+
);
52445248
PRODUCT_NAME = "$(TARGET_NAME)";
52455249
PUBLIC_HEADERS_FOLDER_PATH = /usr/local/include/React;
52465250
RUN_CLANG_STATIC_ANALYZER = YES;
@@ -5258,7 +5262,11 @@
52585262
"RCT_METRO_PORT=${RCT_METRO_PORT}",
52595263
);
52605264
GCC_WARN_ABOUT_MISSING_NEWLINE = YES;
5261-
OTHER_LDFLAGS = "-ObjC";
5265+
OTHER_LDFLAGS = (
5266+
"-ObjC",
5267+
"-weak_framework",
5268+
WebKit,
5269+
);
52625270
PRODUCT_NAME = "$(TARGET_NAME)";
52635271
PUBLIC_HEADERS_FOLDER_PATH = /usr/local/include/React;
52645272
RUN_CLANG_STATIC_ANALYZER = NO;

React/Views/RCTWKWebView.m

+23
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,25 @@ - (void)dealloc
2323

2424
}
2525

26+
/**
27+
* See https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DisplayWebContent/Tasks/WebKitAvail.html.
28+
*/
29+
+ (BOOL)dynamicallyLoadWebKitIfAvailable
30+
{
31+
static BOOL _webkitAvailable=NO;
32+
static dispatch_once_t onceToken;
33+
34+
dispatch_once(&onceToken, ^{
35+
NSBundle *webKitBundle = [NSBundle bundleWithPath:@"/System/Library/Frameworks/WebKit.framework"];
36+
if (webKitBundle) {
37+
_webkitAvailable = [webKitBundle load];
38+
}
39+
});
40+
41+
return _webkitAvailable;
42+
}
43+
44+
2645
- (instancetype)initWithFrame:(CGRect)frame
2746
{
2847
if ((self = [super initWithFrame:frame])) {
@@ -38,6 +57,10 @@ - (instancetype)initWithFrame:(CGRect)frame
3857
- (void)didMoveToWindow
3958
{
4059
if (self.window != nil) {
60+
if (![[self class] dynamicallyLoadWebKitIfAvailable]) {
61+
return;
62+
};
63+
4164
WKWebViewConfiguration *wkWebViewConfig = [WKWebViewConfiguration new];
4265
wkWebViewConfig.userContentController = [WKUserContentController new];
4366
[wkWebViewConfig.userContentController addScriptMessageHandler: self name: MessageHanderName];

0 commit comments

Comments
 (0)