|
| 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 |
0 commit comments