Skip to content

Commit 1442c26

Browse files
RSNarafacebook-github-bot
authored andcommitted
Implement WKWebView to replace WebView
Summary: @public `UIWebView` has been deprecated and replaced by `WKWebView`. This diff introduces a new component `WKWebView` that simply renders a `WKWebView` on iOS. This is the first in the stack of many diffs that'll be required to fully replace `UIWebView` with `WKWebView` in the `<WebView/>` React Native component. Eventually, I hope to introduce a prop called `useWebKitImplementation`, which, when true, will force RN to use `WKWebView` instead of `UIWebView` for the `<WebView/>` component. The only thing that's been implemented so far is the `source` property. Reviewed By: mmmulani Differential Revision: D6266100 fbshipit-source-id: 65862e34bd98db7fff0349cf26888afee43a56e4
1 parent 877212e commit 1442c26

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
* @format
8+
* @flow
9+
* @providesModule WKWebView
10+
*/
11+
12+
const React = require('React');
13+
const View = require('View');
14+
const Text = require('Text');
15+
16+
module.exports = () => {
17+
return (
18+
<View>
19+
<Text>Android version not implemented.</Text>
20+
</View>
21+
);
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
* @format
8+
* @flow
9+
* @providesModule WKWebView
10+
*/
11+
12+
const requireNativeComponent = require('requireNativeComponent');
13+
14+
module.exports = requireNativeComponent('RCTWKWebView');

React/Views/RCTWKWebView.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import <React/RCTView.h>
11+
12+
@class RCTWKWebView;
13+
14+
@protocol RCTWKWebViewDelegate <NSObject>
15+
@end
16+
17+
@interface RCTWKWebView : RCTView
18+
19+
@property (nonatomic, weak) id<RCTWKWebViewDelegate> delegate;
20+
@property (nonatomic, copy) NSDictionary *source;
21+
22+
@end

React/Views/RCTWKWebView.m

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#import "RCTWKWebView.h"
2+
3+
#import <WebKit/WebKit.h>
4+
5+
#import <React/RCTConvert.h>
6+
7+
#import "RCTAutoInsetsProtocol.h"
8+
9+
@interface RCTWKWebView () <WKUIDelegate>
10+
@end
11+
12+
@implementation RCTWKWebView
13+
{
14+
WKWebView *_webView;
15+
}
16+
17+
- (void)dealloc
18+
{
19+
20+
}
21+
22+
- (instancetype)initWithFrame:(CGRect)frame
23+
{
24+
if ((self = [super initWithFrame:frame])) {
25+
super.backgroundColor = [UIColor clearColor];
26+
_webView = [[WKWebView alloc] initWithFrame:self.bounds];
27+
_webView.UIDelegate = self;
28+
[self addSubview:_webView];
29+
}
30+
return self;
31+
}
32+
33+
- (void)setSource:(NSDictionary *)source
34+
{
35+
if (![_source isEqualToDictionary:source]) {
36+
_source = [source copy];
37+
38+
// Check for a static html source first
39+
NSString *html = [RCTConvert NSString:source[@"html"]];
40+
if (html) {
41+
NSURL *baseURL = [RCTConvert NSURL:source[@"baseUrl"]];
42+
if (!baseURL) {
43+
baseURL = [NSURL URLWithString:@"about:blank"];
44+
}
45+
[_webView loadHTMLString:html baseURL:baseURL];
46+
return;
47+
}
48+
49+
NSURLRequest *request = [RCTConvert NSURLRequest:source];
50+
// Because of the way React works, as pages redirect, we actually end up
51+
// passing the redirect urls back here, so we ignore them if trying to load
52+
// the same url. We'll expose a call to 'reload' to allow a user to load
53+
// the existing page.
54+
if ([request.URL isEqual:_webView.URL]) {
55+
return;
56+
}
57+
if (!request.URL) {
58+
// Clear the webview
59+
[_webView loadHTMLString:@"" baseURL:nil];
60+
return;
61+
}
62+
[_webView loadRequest:request];
63+
}
64+
}
65+
66+
- (void)layoutSubviews
67+
{
68+
[super layoutSubviews];
69+
_webView.frame = self.bounds;
70+
}
71+
72+
@end

React/Views/RCTWKWebViewManager.m

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#import "RCTViewManager.h"
2+
#import "RCTWKWebView.h"
3+
4+
@interface RCTWKWebViewManager : RCTViewManager
5+
@end
6+
7+
@implementation RCTWKWebViewManager
8+
9+
RCT_EXPORT_MODULE()
10+
11+
- (UIView *)view
12+
{
13+
return [RCTWKWebView new];
14+
}
15+
16+
RCT_EXPORT_VIEW_PROPERTY(source, NSDictionary)
17+
18+
@end

0 commit comments

Comments
 (0)