react-native-webview.git

RNCWKWebViewManager.m 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #import "RNCWKWebViewManager.h"
  8. #import <React/RCTUIManager.h>
  9. #import <React/RCTDefines.h>
  10. #import "RNCWKWebView.h"
  11. @interface RNCWKWebViewManager () <RNCWKWebViewDelegate>
  12. @end
  13. @implementation RNCWKWebViewManager
  14. {
  15. NSConditionLock *_shouldStartLoadLock;
  16. BOOL _shouldStartLoad;
  17. }
  18. RCT_EXPORT_MODULE()
  19. - (UIView *)view
  20. {
  21. RNCWKWebView *webView = [RNCWKWebView new];
  22. webView.delegate = self;
  23. return webView;
  24. }
  25. RCT_EXPORT_VIEW_PROPERTY(source, NSDictionary)
  26. RCT_EXPORT_VIEW_PROPERTY(onLoadingStart, RCTDirectEventBlock)
  27. RCT_EXPORT_VIEW_PROPERTY(onLoadingFinish, RCTDirectEventBlock)
  28. RCT_EXPORT_VIEW_PROPERTY(onLoadingError, RCTDirectEventBlock)
  29. RCT_EXPORT_VIEW_PROPERTY(onShouldStartLoadWithRequest, RCTDirectEventBlock)
  30. RCT_EXPORT_VIEW_PROPERTY(injectedJavaScript, NSString)
  31. RCT_EXPORT_VIEW_PROPERTY(allowsInlineMediaPlayback, BOOL)
  32. RCT_EXPORT_VIEW_PROPERTY(mediaPlaybackRequiresUserAction, BOOL)
  33. #if WEBKIT_IOS_10_APIS_AVAILABLE
  34. RCT_EXPORT_VIEW_PROPERTY(dataDetectorTypes, WKDataDetectorTypes)
  35. #endif
  36. RCT_EXPORT_VIEW_PROPERTY(contentInset, UIEdgeInsets)
  37. RCT_EXPORT_VIEW_PROPERTY(automaticallyAdjustContentInsets, BOOL)
  38. /**
  39. * Expose methods to enable messaging the webview.
  40. */
  41. RCT_EXPORT_VIEW_PROPERTY(messagingEnabled, BOOL)
  42. RCT_EXPORT_VIEW_PROPERTY(onMessage, RCTDirectEventBlock)
  43. RCT_EXPORT_METHOD(postMessage:(nonnull NSNumber *)reactTag message:(NSString *)message)
  44. {
  45. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWKWebView *> *viewRegistry) {
  46. RNCWKWebView *view = viewRegistry[reactTag];
  47. if (![view isKindOfClass:[RNCWKWebView class]]) {
  48. RCTLogError(@"Invalid view returned from registry, expecting RNCWKWebView, got: %@", view);
  49. } else {
  50. [view postMessage:message];
  51. }
  52. }];
  53. }
  54. RCT_CUSTOM_VIEW_PROPERTY(bounces, BOOL, RNCWKWebView) {
  55. view.bounces = json == nil ? true : [RCTConvert BOOL: json];
  56. }
  57. RCT_CUSTOM_VIEW_PROPERTY(scrollEnabled, BOOL, RNCWKWebView) {
  58. view.scrollEnabled = json == nil ? true : [RCTConvert BOOL: json];
  59. }
  60. RCT_CUSTOM_VIEW_PROPERTY(decelerationRate, CGFloat, RNCWKWebView) {
  61. view.decelerationRate = json == nil ? UIScrollViewDecelerationRateNormal : [RCTConvert CGFloat: json];
  62. }
  63. RCT_EXPORT_METHOD(injectJavaScript:(nonnull NSNumber *)reactTag script:(NSString *)script)
  64. {
  65. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWKWebView *> *viewRegistry) {
  66. RNCWKWebView *view = viewRegistry[reactTag];
  67. if (![view isKindOfClass:[RNCWKWebView class]]) {
  68. RCTLogError(@"Invalid view returned from registry, expecting RNCWKWebView, got: %@", view);
  69. } else {
  70. [view injectJavaScript:script];
  71. }
  72. }];
  73. }
  74. RCT_EXPORT_METHOD(goBack:(nonnull NSNumber *)reactTag)
  75. {
  76. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWKWebView *> *viewRegistry) {
  77. RNCWKWebView *view = viewRegistry[reactTag];
  78. if (![view isKindOfClass:[RNCWKWebView class]]) {
  79. RCTLogError(@"Invalid view returned from registry, expecting RNCWKWebView, got: %@", view);
  80. } else {
  81. [view goBack];
  82. }
  83. }];
  84. }
  85. RCT_EXPORT_METHOD(goForward:(nonnull NSNumber *)reactTag)
  86. {
  87. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWKWebView *> *viewRegistry) {
  88. RNCWKWebView *view = viewRegistry[reactTag];
  89. if (![view isKindOfClass:[RNCWKWebView class]]) {
  90. RCTLogError(@"Invalid view returned from registry, expecting RNCWKWebView, got: %@", view);
  91. } else {
  92. [view goForward];
  93. }
  94. }];
  95. }
  96. RCT_EXPORT_METHOD(reload:(nonnull NSNumber *)reactTag)
  97. {
  98. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWKWebView *> *viewRegistry) {
  99. RNCWKWebView *view = viewRegistry[reactTag];
  100. if (![view isKindOfClass:[RNCWKWebView class]]) {
  101. RCTLogError(@"Invalid view returned from registry, expecting RNCWKWebView, got: %@", view);
  102. } else {
  103. [view reload];
  104. }
  105. }];
  106. }
  107. RCT_EXPORT_METHOD(stopLoading:(nonnull NSNumber *)reactTag)
  108. {
  109. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWKWebView *> *viewRegistry) {
  110. RNCWKWebView *view = viewRegistry[reactTag];
  111. if (![view isKindOfClass:[RNCWKWebView class]]) {
  112. RCTLogError(@"Invalid view returned from registry, expecting RNCWKWebView, got: %@", view);
  113. } else {
  114. [view stopLoading];
  115. }
  116. }];
  117. }
  118. #pragma mark - Exported synchronous methods
  119. - (BOOL) webView:(RNCWKWebView *)webView
  120. shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
  121. withCallback:(RCTDirectEventBlock)callback
  122. {
  123. _shouldStartLoadLock = [[NSConditionLock alloc] initWithCondition:arc4random()];
  124. _shouldStartLoad = YES;
  125. request[@"lockIdentifier"] = @(_shouldStartLoadLock.condition);
  126. callback(request);
  127. // Block the main thread for a maximum of 250ms until the JS thread returns
  128. if ([_shouldStartLoadLock lockWhenCondition:0 beforeDate:[NSDate dateWithTimeIntervalSinceNow:.25]]) {
  129. BOOL returnValue = _shouldStartLoad;
  130. [_shouldStartLoadLock unlock];
  131. _shouldStartLoadLock = nil;
  132. return returnValue;
  133. } else {
  134. RCTLogWarn(@"Did not receive response to shouldStartLoad in time, defaulting to YES");
  135. return YES;
  136. }
  137. }
  138. RCT_EXPORT_METHOD(startLoadWithResult:(BOOL)result lockIdentifier:(NSInteger)lockIdentifier)
  139. {
  140. if ([_shouldStartLoadLock tryLockWhenCondition:lockIdentifier]) {
  141. _shouldStartLoad = result;
  142. [_shouldStartLoadLock unlockWithCondition:0];
  143. } else {
  144. RCTLogWarn(@"startLoadWithResult invoked with invalid lockIdentifier: "
  145. "got %lld, expected %lld", (long long)lockIdentifier, (long long)_shouldStartLoadLock.condition);
  146. }
  147. }
  148. @end