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
This commit is contained in:
Ramanpreet Nara 2018-08-16 13:34:09 -07:00 committed by Facebook Github Bot
parent 90e85a4adc
commit 0022354525
3 changed files with 18 additions and 0 deletions

View File

@ -24,5 +24,6 @@
@property (nonatomic, assign) CGFloat decelerationRate; @property (nonatomic, assign) CGFloat decelerationRate;
- (void)postMessage:(NSString *)message; - (void)postMessage:(NSString *)message;
- (void)injectJavaScript:(NSString *)script;
@end @end

View File

@ -255,4 +255,9 @@ static NSString *const MessageHanderName = @"ReactNative";
} }
} }
- (void)injectJavaScript:(NSString *)script
{
[self evaluateJS: script thenCall: nil];
}
@end @end

View File

@ -45,4 +45,16 @@ RCT_CUSTOM_VIEW_PROPERTY(decelerationRate, CGFloat, RCTWKWebView) {
view.decelerationRate = json == nil ? UIScrollViewDecelerationRateNormal : [RCTConvert CGFloat: json]; 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 @end