Bez popisu

RNCWKWebViewManager.m 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
  58. RCT_EXPORT_VIEW_PROPERTY(contentInsetAdjustmentBehavior, UIScrollViewContentInsetAdjustmentBehavior)
  59. #endif
  60. /**
  61. * Expose methods to enable messaging the webview.
  62. */
  63. RCT_EXPORT_VIEW_PROPERTY(messagingEnabled, BOOL)
  64. RCT_EXPORT_VIEW_PROPERTY(onMessage, RCTDirectEventBlock)
  65. RCT_EXPORT_VIEW_PROPERTY(onScroll, RCTDirectEventBlock)
  66. RCT_EXPORT_METHOD(postMessage:(nonnull NSNumber *)reactTag message:(NSString *)message)
  67. {
  68. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWKWebView *> *viewRegistry) {
  69. RNCWKWebView *view = viewRegistry[reactTag];
  70. if (![view isKindOfClass:[RNCWKWebView class]]) {
  71. RCTLogError(@"Invalid view returned from registry, expecting RNCWKWebView, got: %@", view);
  72. } else {
  73. [view postMessage:message];
  74. }
  75. }];
  76. }
  77. RCT_CUSTOM_VIEW_PROPERTY(bounces, BOOL, RNCWKWebView) {
  78. view.bounces = json == nil ? true : [RCTConvert BOOL: json];
  79. }
  80. RCT_CUSTOM_VIEW_PROPERTY(useSharedProcessPool, BOOL, RNCWKWebView) {
  81. view.useSharedProcessPool = json == nil ? true : [RCTConvert BOOL: json];
  82. }
  83. RCT_CUSTOM_VIEW_PROPERTY(scrollEnabled, BOOL, RNCWKWebView) {
  84. view.scrollEnabled = json == nil ? true : [RCTConvert BOOL: json];
  85. }
  86. RCT_CUSTOM_VIEW_PROPERTY(sharedCookiesEnabled, BOOL, RNCWKWebView) {
  87. view.sharedCookiesEnabled = json == nil ? false : [RCTConvert BOOL: json];
  88. }
  89. RCT_CUSTOM_VIEW_PROPERTY(decelerationRate, CGFloat, RNCWKWebView) {
  90. view.decelerationRate = json == nil ? UIScrollViewDecelerationRateNormal : [RCTConvert CGFloat: json];
  91. }
  92. RCT_CUSTOM_VIEW_PROPERTY(directionalLockEnabled, BOOL, RNCWKWebView) {
  93. view.directionalLockEnabled = json == nil ? true : [RCTConvert BOOL: json];
  94. }
  95. RCT_CUSTOM_VIEW_PROPERTY(showsHorizontalScrollIndicator, BOOL, RNCWKWebView) {
  96. view.showsHorizontalScrollIndicator = json == nil ? true : [RCTConvert BOOL: json];
  97. }
  98. RCT_CUSTOM_VIEW_PROPERTY(showsVerticalScrollIndicator, BOOL, RNCWKWebView) {
  99. view.showsVerticalScrollIndicator = json == nil ? true : [RCTConvert BOOL: json];
  100. }
  101. RCT_CUSTOM_VIEW_PROPERTY(keyboardDisplayRequiresUserAction, BOOL, RNCWKWebView) {
  102. view.keyboardDisplayRequiresUserAction = json == nil ? true : [RCTConvert BOOL: json];
  103. }
  104. RCT_EXPORT_METHOD(injectJavaScript:(nonnull NSNumber *)reactTag script:(NSString *)script)
  105. {
  106. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWKWebView *> *viewRegistry) {
  107. RNCWKWebView *view = viewRegistry[reactTag];
  108. if (![view isKindOfClass:[RNCWKWebView class]]) {
  109. RCTLogError(@"Invalid view returned from registry, expecting RNCWKWebView, got: %@", view);
  110. } else {
  111. [view injectJavaScript:script];
  112. }
  113. }];
  114. }
  115. RCT_EXPORT_METHOD(goBack:(nonnull NSNumber *)reactTag)
  116. {
  117. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWKWebView *> *viewRegistry) {
  118. RNCWKWebView *view = viewRegistry[reactTag];
  119. if (![view isKindOfClass:[RNCWKWebView class]]) {
  120. RCTLogError(@"Invalid view returned from registry, expecting RNCWKWebView, got: %@", view);
  121. } else {
  122. [view goBack];
  123. }
  124. }];
  125. }
  126. RCT_EXPORT_METHOD(goForward:(nonnull NSNumber *)reactTag)
  127. {
  128. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWKWebView *> *viewRegistry) {
  129. RNCWKWebView *view = viewRegistry[reactTag];
  130. if (![view isKindOfClass:[RNCWKWebView class]]) {
  131. RCTLogError(@"Invalid view returned from registry, expecting RNCWKWebView, got: %@", view);
  132. } else {
  133. [view goForward];
  134. }
  135. }];
  136. }
  137. RCT_EXPORT_METHOD(reload:(nonnull NSNumber *)reactTag)
  138. {
  139. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWKWebView *> *viewRegistry) {
  140. RNCWKWebView *view = viewRegistry[reactTag];
  141. if (![view isKindOfClass:[RNCWKWebView class]]) {
  142. RCTLogError(@"Invalid view returned from registry, expecting RNCWKWebView, got: %@", view);
  143. } else {
  144. [view reload];
  145. }
  146. }];
  147. }
  148. RCT_EXPORT_METHOD(stopLoading:(nonnull NSNumber *)reactTag)
  149. {
  150. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWKWebView *> *viewRegistry) {
  151. RNCWKWebView *view = viewRegistry[reactTag];
  152. if (![view isKindOfClass:[RNCWKWebView class]]) {
  153. RCTLogError(@"Invalid view returned from registry, expecting RNCWKWebView, got: %@", view);
  154. } else {
  155. [view stopLoading];
  156. }
  157. }];
  158. }
  159. #pragma mark - Exported synchronous methods
  160. - (BOOL) webView:(RNCWKWebView *)webView
  161. shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
  162. withCallback:(RCTDirectEventBlock)callback
  163. {
  164. _shouldStartLoadLock = [[NSConditionLock alloc] initWithCondition:arc4random()];
  165. _shouldStartLoad = YES;
  166. request[@"lockIdentifier"] = @(_shouldStartLoadLock.condition);
  167. callback(request);
  168. // Block the main thread for a maximum of 250ms until the JS thread returns
  169. if ([_shouldStartLoadLock lockWhenCondition:0 beforeDate:[NSDate dateWithTimeIntervalSinceNow:.25]]) {
  170. BOOL returnValue = _shouldStartLoad;
  171. [_shouldStartLoadLock unlock];
  172. _shouldStartLoadLock = nil;
  173. return returnValue;
  174. } else {
  175. RCTLogWarn(@"Did not receive response to shouldStartLoad in time, defaulting to YES");
  176. return YES;
  177. }
  178. }
  179. RCT_EXPORT_METHOD(startLoadWithResult:(BOOL)result lockIdentifier:(NSInteger)lockIdentifier)
  180. {
  181. if ([_shouldStartLoadLock tryLockWhenCondition:lockIdentifier]) {
  182. _shouldStartLoad = result;
  183. [_shouldStartLoadLock unlockWithCondition:0];
  184. } else {
  185. RCTLogWarn(@"startLoadWithResult invoked with invalid lockIdentifier: "
  186. "got %lld, expected %lld", (long long)lockIdentifier, (long long)_shouldStartLoadLock.condition);
  187. }
  188. }
  189. @end