react-native-webview.git

RNCWKWebViewManager.m 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 RCTConvert (UIScrollView)
  14. #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
  15. RCT_ENUM_CONVERTER(UIScrollViewContentInsetAdjustmentBehavior, (@{
  16. @"automatic": @(UIScrollViewContentInsetAdjustmentAutomatic),
  17. @"scrollableAxes": @(UIScrollViewContentInsetAdjustmentScrollableAxes),
  18. @"never": @(UIScrollViewContentInsetAdjustmentNever),
  19. @"always": @(UIScrollViewContentInsetAdjustmentAlways),
  20. }), UIScrollViewContentInsetAdjustmentNever, integerValue)
  21. #endif
  22. @end
  23. @implementation RNCWKWebViewManager
  24. {
  25. NSConditionLock *_shouldStartLoadLock;
  26. BOOL _shouldStartLoad;
  27. }
  28. RCT_EXPORT_MODULE()
  29. - (UIView *)view
  30. {
  31. RNCWKWebView *webView = [RNCWKWebView new];
  32. webView.delegate = self;
  33. return webView;
  34. }
  35. RCT_EXPORT_VIEW_PROPERTY(source, NSDictionary)
  36. RCT_EXPORT_VIEW_PROPERTY(onLoadingStart, RCTDirectEventBlock)
  37. RCT_EXPORT_VIEW_PROPERTY(onLoadingFinish, RCTDirectEventBlock)
  38. RCT_EXPORT_VIEW_PROPERTY(onLoadingError, RCTDirectEventBlock)
  39. RCT_EXPORT_VIEW_PROPERTY(onLoadingProgress, RCTDirectEventBlock)
  40. RCT_EXPORT_VIEW_PROPERTY(onShouldStartLoadWithRequest, RCTDirectEventBlock)
  41. RCT_EXPORT_VIEW_PROPERTY(injectedJavaScript, NSString)
  42. RCT_EXPORT_VIEW_PROPERTY(allowsInlineMediaPlayback, BOOL)
  43. RCT_EXPORT_VIEW_PROPERTY(mediaPlaybackRequiresUserAction, BOOL)
  44. #if WEBKIT_IOS_10_APIS_AVAILABLE
  45. RCT_EXPORT_VIEW_PROPERTY(dataDetectorTypes, WKDataDetectorTypes)
  46. #endif
  47. RCT_EXPORT_VIEW_PROPERTY(contentInset, UIEdgeInsets)
  48. RCT_EXPORT_VIEW_PROPERTY(automaticallyAdjustContentInsets, BOOL)
  49. RCT_EXPORT_VIEW_PROPERTY(hideKeyboardAccessoryView, BOOL)
  50. RCT_EXPORT_VIEW_PROPERTY(allowsBackForwardNavigationGestures, BOOL)
  51. RCT_EXPORT_VIEW_PROPERTY(incognito, BOOL)
  52. RCT_EXPORT_VIEW_PROPERTY(pagingEnabled, BOOL)
  53. RCT_EXPORT_VIEW_PROPERTY(userAgent, NSString)
  54. RCT_EXPORT_VIEW_PROPERTY(applicationNameForUserAgent, NSString)
  55. RCT_EXPORT_VIEW_PROPERTY(cacheEnabled, BOOL)
  56. RCT_EXPORT_VIEW_PROPERTY(allowsLinkPreview, BOOL)
  57. RCT_EXPORT_VIEW_PROPERTY(allowingReadAccessToURL, NSString)
  58. #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
  59. RCT_EXPORT_VIEW_PROPERTY(contentInsetAdjustmentBehavior, UIScrollViewContentInsetAdjustmentBehavior)
  60. #endif
  61. /**
  62. * Expose methods to enable messaging the webview.
  63. */
  64. RCT_EXPORT_VIEW_PROPERTY(messagingEnabled, BOOL)
  65. RCT_EXPORT_VIEW_PROPERTY(onMessage, RCTDirectEventBlock)
  66. RCT_EXPORT_VIEW_PROPERTY(onScroll, RCTDirectEventBlock)
  67. RCT_EXPORT_METHOD(postMessage:(nonnull NSNumber *)reactTag message:(NSString *)message)
  68. {
  69. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWKWebView *> *viewRegistry) {
  70. RNCWKWebView *view = viewRegistry[reactTag];
  71. if (![view isKindOfClass:[RNCWKWebView class]]) {
  72. RCTLogError(@"Invalid view returned from registry, expecting RNCWKWebView, got: %@", view);
  73. } else {
  74. [view postMessage:message];
  75. }
  76. }];
  77. }
  78. RCT_CUSTOM_VIEW_PROPERTY(bounces, BOOL, RNCWKWebView) {
  79. view.bounces = json == nil ? true : [RCTConvert BOOL: json];
  80. }
  81. RCT_CUSTOM_VIEW_PROPERTY(useSharedProcessPool, BOOL, RNCWKWebView) {
  82. view.useSharedProcessPool = json == nil ? true : [RCTConvert BOOL: json];
  83. }
  84. RCT_CUSTOM_VIEW_PROPERTY(scrollEnabled, BOOL, RNCWKWebView) {
  85. view.scrollEnabled = json == nil ? true : [RCTConvert BOOL: json];
  86. }
  87. RCT_CUSTOM_VIEW_PROPERTY(sharedCookiesEnabled, BOOL, RNCWKWebView) {
  88. view.sharedCookiesEnabled = json == nil ? false : [RCTConvert BOOL: json];
  89. }
  90. RCT_CUSTOM_VIEW_PROPERTY(decelerationRate, CGFloat, RNCWKWebView) {
  91. view.decelerationRate = json == nil ? UIScrollViewDecelerationRateNormal : [RCTConvert CGFloat: json];
  92. }
  93. RCT_CUSTOM_VIEW_PROPERTY(directionalLockEnabled, BOOL, RNCWKWebView) {
  94. view.directionalLockEnabled = json == nil ? true : [RCTConvert BOOL: json];
  95. }
  96. RCT_CUSTOM_VIEW_PROPERTY(showsHorizontalScrollIndicator, BOOL, RNCWKWebView) {
  97. view.showsHorizontalScrollIndicator = json == nil ? true : [RCTConvert BOOL: json];
  98. }
  99. RCT_CUSTOM_VIEW_PROPERTY(showsVerticalScrollIndicator, BOOL, RNCWKWebView) {
  100. view.showsVerticalScrollIndicator = json == nil ? true : [RCTConvert BOOL: json];
  101. }
  102. RCT_CUSTOM_VIEW_PROPERTY(keyboardDisplayRequiresUserAction, BOOL, RNCWKWebView) {
  103. view.keyboardDisplayRequiresUserAction = json == nil ? true : [RCTConvert BOOL: json];
  104. }
  105. RCT_EXPORT_METHOD(injectJavaScript:(nonnull NSNumber *)reactTag script:(NSString *)script)
  106. {
  107. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWKWebView *> *viewRegistry) {
  108. RNCWKWebView *view = viewRegistry[reactTag];
  109. if (![view isKindOfClass:[RNCWKWebView class]]) {
  110. RCTLogError(@"Invalid view returned from registry, expecting RNCWKWebView, got: %@", view);
  111. } else {
  112. [view injectJavaScript:script];
  113. }
  114. }];
  115. }
  116. RCT_EXPORT_METHOD(goBack:(nonnull NSNumber *)reactTag)
  117. {
  118. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWKWebView *> *viewRegistry) {
  119. RNCWKWebView *view = viewRegistry[reactTag];
  120. if (![view isKindOfClass:[RNCWKWebView class]]) {
  121. RCTLogError(@"Invalid view returned from registry, expecting RNCWKWebView, got: %@", view);
  122. } else {
  123. [view goBack];
  124. }
  125. }];
  126. }
  127. RCT_EXPORT_METHOD(goForward:(nonnull NSNumber *)reactTag)
  128. {
  129. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWKWebView *> *viewRegistry) {
  130. RNCWKWebView *view = viewRegistry[reactTag];
  131. if (![view isKindOfClass:[RNCWKWebView class]]) {
  132. RCTLogError(@"Invalid view returned from registry, expecting RNCWKWebView, got: %@", view);
  133. } else {
  134. [view goForward];
  135. }
  136. }];
  137. }
  138. RCT_EXPORT_METHOD(reload:(nonnull NSNumber *)reactTag)
  139. {
  140. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWKWebView *> *viewRegistry) {
  141. RNCWKWebView *view = viewRegistry[reactTag];
  142. if (![view isKindOfClass:[RNCWKWebView class]]) {
  143. RCTLogError(@"Invalid view returned from registry, expecting RNCWKWebView, got: %@", view);
  144. } else {
  145. [view reload];
  146. }
  147. }];
  148. }
  149. RCT_EXPORT_METHOD(stopLoading:(nonnull NSNumber *)reactTag)
  150. {
  151. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWKWebView *> *viewRegistry) {
  152. RNCWKWebView *view = viewRegistry[reactTag];
  153. if (![view isKindOfClass:[RNCWKWebView class]]) {
  154. RCTLogError(@"Invalid view returned from registry, expecting RNCWKWebView, got: %@", view);
  155. } else {
  156. [view stopLoading];
  157. }
  158. }];
  159. }
  160. #pragma mark - Exported synchronous methods
  161. - (BOOL) webView:(RNCWKWebView *)webView
  162. shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
  163. withCallback:(RCTDirectEventBlock)callback
  164. {
  165. _shouldStartLoadLock = [[NSConditionLock alloc] initWithCondition:arc4random()];
  166. _shouldStartLoad = YES;
  167. request[@"lockIdentifier"] = @(_shouldStartLoadLock.condition);
  168. callback(request);
  169. // Block the main thread for a maximum of 250ms until the JS thread returns
  170. if ([_shouldStartLoadLock lockWhenCondition:0 beforeDate:[NSDate dateWithTimeIntervalSinceNow:.25]]) {
  171. BOOL returnValue = _shouldStartLoad;
  172. [_shouldStartLoadLock unlock];
  173. _shouldStartLoadLock = nil;
  174. return returnValue;
  175. } else {
  176. RCTLogWarn(@"Did not receive response to shouldStartLoad in time, defaulting to YES");
  177. return YES;
  178. }
  179. }
  180. RCT_EXPORT_METHOD(startLoadWithResult:(BOOL)result lockIdentifier:(NSInteger)lockIdentifier)
  181. {
  182. if ([_shouldStartLoadLock tryLockWhenCondition:lockIdentifier]) {
  183. _shouldStartLoad = result;
  184. [_shouldStartLoadLock unlockWithCondition:0];
  185. } else {
  186. RCTLogWarn(@"startLoadWithResult invoked with invalid lockIdentifier: "
  187. "got %lld, expected %lld", (long long)lockIdentifier, (long long)_shouldStartLoadLock.condition);
  188. }
  189. }
  190. @end