Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: migrate <WebView> from UiWebView to WKWebView in iOS #16535

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions React/Views/RCTWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

#import <React/RCTView.h>
#import <WebKit/WebKit.h>

@class RCTWebView;

Expand Down Expand Up @@ -37,6 +38,10 @@ shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
@property (nonatomic, assign) BOOL messagingEnabled;
@property (nonatomic, copy) NSString *injectedJavaScript;
@property (nonatomic, assign) BOOL scalesPageToFit;
@property (nonatomic, assign) BOOL allowsInlineMediaPlayback;
@property (nonatomic, assign) BOOL mediaPlaybackRequiresUserAction;
@property (nonatomic, assign) WKDataDetectorTypes dataDetectorTypes;


- (void)goForward;
- (void)goBack;
Expand Down
52 changes: 29 additions & 23 deletions React/Views/RCTWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

static NSString *const kPostMessageHost = @"postMessage";

@interface RCTWebView () <UIWebViewDelegate, RCTAutoInsetsProtocol>
@interface RCTWebView () <WKUIDelegate, RCTAutoInsetsProtocol>

@property (nonatomic, copy) RCTDirectEventBlock onLoadingStart;
@property (nonatomic, copy) RCTDirectEventBlock onLoadingFinish;
Expand All @@ -35,13 +35,13 @@ @interface RCTWebView () <UIWebViewDelegate, RCTAutoInsetsProtocol>

@implementation RCTWebView
{
UIWebView *_webView;
WKWebView *_webView;
NSString *_injectedJavaScript;
}

- (void)dealloc
{
_webView.delegate = nil;
_webView.UIDelegate = nil;
}

- (instancetype)initWithFrame:(CGRect)frame
Expand All @@ -50,8 +50,9 @@ - (instancetype)initWithFrame:(CGRect)frame
super.backgroundColor = [UIColor clearColor];
_automaticallyAdjustContentInsets = YES;
_contentInset = UIEdgeInsetsZero;
_webView = [[UIWebView alloc] initWithFrame:self.bounds];
_webView.delegate = self;
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
_webView = [[WKWebView alloc] initWithFrame:self.bounds configuration:theConfiguration];
_webView.UIDelegate = self;
[self addSubview:_webView];
}
return self;
Expand All @@ -72,7 +73,7 @@ - (void)goBack
- (void)reload
{
NSURLRequest *request = [RCTConvert NSURLRequest:self.source];
if (request.URL && !_webView.request.URL.absoluteString.length) {
if (request.URL && !_webView.URL.absoluteString.length) {
[_webView loadRequest:request];
}
else {
Expand All @@ -94,12 +95,12 @@ - (void)postMessage:(NSString *)message
stringWithFormat:@"document.dispatchEvent(new MessageEvent('message', %@));",
RCTJSONStringify(eventInitDict, NULL)
];
[_webView stringByEvaluatingJavaScriptFromString:source];
[_webView evaluateJavaScript:source completionHandler:nil];
}

- (void)injectJavaScript:(NSString *)script
{
[_webView stringByEvaluatingJavaScriptFromString:script];
[_webView evaluateJavaScript:script completionHandler:nil];
}

- (void)setSource:(NSDictionary *)source
Expand All @@ -123,7 +124,7 @@ - (void)setSource:(NSDictionary *)source
// passing the redirect urls back here, so we ignore them if trying to load
// the same url. We'll expose a call to 'reload' to allow a user to load
// the existing page.
if ([request.URL isEqual:_webView.request.URL]) {
if ([request.URL isEqual:_webView.URL]) {
return;
}
if (!request.URL) {
Expand All @@ -149,18 +150,18 @@ - (void)setContentInset:(UIEdgeInsets)contentInset
updateOffset:NO];
}

- (void)setScalesPageToFit:(BOOL)scalesPageToFit
{
if (_webView.scalesPageToFit != scalesPageToFit) {
_webView.scalesPageToFit = scalesPageToFit;
[_webView reload];
}
}
//- (void)setScalesPageToFit:(BOOL)scalesPageToFit
//{
// if (_webView.scalesPageToFit != scalesPageToFit) {
// _webView.scalesPageToFit = scalesPageToFit;
// [_webView reload];
// }
//}

- (BOOL)scalesPageToFit
{
return _webView.scalesPageToFit;
}
//- (BOOL)scalesPageToFit
//{
// return _webView.scalesPageToFit;
//}

- (void)setBackgroundColor:(UIColor *)backgroundColor
{
Expand All @@ -177,12 +178,17 @@ - (UIColor *)backgroundColor
- (NSMutableDictionary<NSString *, id> *)baseEvent
{
NSMutableDictionary<NSString *, id> *event = [[NSMutableDictionary alloc] initWithDictionary:@{
@"url": _webView.request.URL.absoluteString ?: @"",
@"url": _webView.URL.absoluteString ?: @"",
@"loading" : @(_webView.loading),
@"title": [_webView stringByEvaluatingJavaScriptFromString:@"document.title"],
@"title": @"",
@"canGoBack": @(_webView.canGoBack),
@"canGoForward" : @(_webView.canGoForward),
}];
[_webView evaluateJavaScript:@"document.title" completionHandler:^(id _Nullable title, NSError * _Nullable error) {
if(!error) {
event[@"title"] = title;
}
}];

return event;
}
Expand Down Expand Up @@ -253,7 +259,7 @@ - (BOOL)webView:(__unused UIWebView *)webView shouldStartLoadWithRequest:(NSURLR

NSString *source = @"document.dispatchEvent(new MessageEvent('message:received'));";

[_webView stringByEvaluatingJavaScriptFromString:source];
[_webView evaluateJavaScript:source completionHandler:nil];

_onMessage(event);
}
Expand Down
10 changes: 5 additions & 5 deletions React/Views/RCTWebViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ - (UIView *)view
RCT_EXPORT_VIEW_PROPERTY(onLoadingError, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onMessage, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onShouldStartLoadWithRequest, RCTDirectEventBlock)
RCT_REMAP_VIEW_PROPERTY(allowsInlineMediaPlayback, _webView.allowsInlineMediaPlayback, BOOL)
RCT_REMAP_VIEW_PROPERTY(mediaPlaybackRequiresUserAction, _webView.mediaPlaybackRequiresUserAction, BOOL)
RCT_REMAP_VIEW_PROPERTY(dataDetectorTypes, _webView.dataDetectorTypes, UIDataDetectorTypes)
RCT_EXPORT_VIEW_PROPERTY(allowsInlineMediaPlayback, BOOL)
RCT_EXPORT_VIEW_PROPERTY(mediaPlaybackRequiresUserAction, BOOL)
RCT_EXPORT_VIEW_PROPERTY(dataDetectorTypes, WKDataDetectorTypes)

RCT_EXPORT_METHOD(goBack:(nonnull NSNumber *)reactTag)
{
Expand All @@ -65,8 +65,8 @@ - (UIView *)view

RCT_EXPORT_METHOD(goForward:(nonnull NSNumber *)reactTag)
{
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
id view = viewRegistry[reactTag];
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTWebView *> *viewRegistry) {
RCTWebView * view = viewRegistry[reactTag];
if (![view isKindOfClass:[RCTWebView class]]) {
RCTLogError(@"Invalid view returned from registry, expecting RCTWebView, got: %@", view);
} else {
Expand Down