From 4ddaaacdadd48d3b05ae4c59cf2d5ef6574ac754 Mon Sep 17 00:00:00 2001 From: Raphael Eidus Date: Wed, 25 Oct 2017 10:33:43 -0400 Subject: [PATCH] migrate from UiWebView to WKWebView in iOS --- React/Views/RCTWebView.h | 5 ++++ React/Views/RCTWebView.m | 52 ++++++++++++++++++--------------- React/Views/RCTWebViewManager.m | 10 +++---- 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/React/Views/RCTWebView.h b/React/Views/RCTWebView.h index 92e9c6bb7ea791..a26ac8885d8093 100644 --- a/React/Views/RCTWebView.h +++ b/React/Views/RCTWebView.h @@ -8,6 +8,7 @@ */ #import +#import @class RCTWebView; @@ -37,6 +38,10 @@ shouldStartLoadForRequest:(NSMutableDictionary *)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; diff --git a/React/Views/RCTWebView.m b/React/Views/RCTWebView.m index 62823e27813193..520217432d99f4 100644 --- a/React/Views/RCTWebView.m +++ b/React/Views/RCTWebView.m @@ -23,7 +23,7 @@ static NSString *const kPostMessageHost = @"postMessage"; -@interface RCTWebView () +@interface RCTWebView () @property (nonatomic, copy) RCTDirectEventBlock onLoadingStart; @property (nonatomic, copy) RCTDirectEventBlock onLoadingFinish; @@ -35,13 +35,13 @@ @interface RCTWebView () @implementation RCTWebView { - UIWebView *_webView; + WKWebView *_webView; NSString *_injectedJavaScript; } - (void)dealloc { - _webView.delegate = nil; + _webView.UIDelegate = nil; } - (instancetype)initWithFrame:(CGRect)frame @@ -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; @@ -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 { @@ -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 @@ -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) { @@ -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 { @@ -177,12 +178,17 @@ - (UIColor *)backgroundColor - (NSMutableDictionary *)baseEvent { NSMutableDictionary *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; } @@ -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); } diff --git a/React/Views/RCTWebViewManager.m b/React/Views/RCTWebViewManager.m index ec6192ef5672b1..eecacb4a7141f6 100644 --- a/React/Views/RCTWebViewManager.m +++ b/React/Views/RCTWebViewManager.m @@ -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) { @@ -65,8 +65,8 @@ - (UIView *)view RCT_EXPORT_METHOD(goForward:(nonnull NSNumber *)reactTag) { - [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary *viewRegistry) { - id view = viewRegistry[reactTag]; + [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary *viewRegistry) { + RCTWebView * view = viewRegistry[reactTag]; if (![view isKindOfClass:[RCTWebView class]]) { RCTLogError(@"Invalid view returned from registry, expecting RCTWebView, got: %@", view); } else {