Permalink
Browse files

Implement 'injectJavaScript' method

Summary:
@public

This diff introduces a method called `injectJavaScript(script)` on the React Native `<WKWebView/>` component. When called with a string, it evaluates that string as JavaScript within the web view.

Reviewed By: shergin

Differential Revision: D6367445

fbshipit-source-id: f68afeff42535dc991747f96a63f3c956faf13d3
  • Loading branch information...
RSNara authored and kelset committed Aug 16, 2018
1 parent 5662598 commit 1984f4bc2709f80065bb416b984684a2955b1467
Showing with 18 additions and 0 deletions.
  1. +1 −0 React/Views/RCTWKWebView.h
  2. +5 −0 React/Views/RCTWKWebView.m
  3. +12 −0 React/Views/RCTWKWebViewManager.m
@@ -24,5 +24,6 @@
@property (nonatomic, assign) CGFloat decelerationRate;
- (void)postMessage:(NSString *)message;
- (void)injectJavaScript:(NSString *)script;
@end
@@ -255,4 +255,9 @@ - (void) webView:(WKWebView *)webView
}
}
- (void)injectJavaScript:(NSString *)script
{
[self evaluateJS: script thenCall: nil];
}
@end
@@ -45,4 +45,16 @@ - (UIView *)view
view.decelerationRate = json == nil ? UIScrollViewDecelerationRateNormal : [RCTConvert CGFloat: json];
}
RCT_EXPORT_METHOD(injectJavaScript:(nonnull NSNumber *)reactTag script:(NSString *)script)
{
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTWKWebView *> *viewRegistry) {
RCTWKWebView *view = viewRegistry[reactTag];
if (![view isKindOfClass:[RCTWKWebView class]]) {
RCTLogError(@"Invalid view returned from registry, expecting RCTWebView, got: %@", view);
} else {
[view injectJavaScript:script];
}
}];
}
@end

0 comments on commit 1984f4b

Please sign in to comment.