暫無描述

RNCWKWebViewManager.m 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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(onLoadingProgress, RCTDirectEventBlock)
  30. RCT_EXPORT_VIEW_PROPERTY(onShouldStartLoadWithRequest, RCTDirectEventBlock)
  31. RCT_EXPORT_VIEW_PROPERTY(injectedJavaScript, NSString)
  32. RCT_EXPORT_VIEW_PROPERTY(allowsInlineMediaPlayback, BOOL)
  33. RCT_EXPORT_VIEW_PROPERTY(mediaPlaybackRequiresUserAction, BOOL)
  34. #if WEBKIT_IOS_10_APIS_AVAILABLE
  35. RCT_EXPORT_VIEW_PROPERTY(dataDetectorTypes, WKDataDetectorTypes)
  36. #endif
  37. RCT_EXPORT_VIEW_PROPERTY(contentInset, UIEdgeInsets)
  38. RCT_EXPORT_VIEW_PROPERTY(automaticallyAdjustContentInsets, BOOL)
  39. RCT_EXPORT_VIEW_PROPERTY(hideKeyboardAccessoryView, BOOL)
  40. RCT_EXPORT_VIEW_PROPERTY(allowsBackForwardNavigationGestures, BOOL)
  41. RCT_EXPORT_VIEW_PROPERTY(incognito, BOOL)
  42. RCT_EXPORT_VIEW_PROPERTY(pagingEnabled, BOOL)
  43. RCT_EXPORT_VIEW_PROPERTY(userAgent, NSString)
  44. RCT_EXPORT_VIEW_PROPERTY(cacheEnabled, BOOL)
  45. RCT_EXPORT_VIEW_PROPERTY(allowsLinkPreview, BOOL)
  46. /**
  47. * Expose methods to enable messaging the webview.
  48. */
  49. RCT_EXPORT_VIEW_PROPERTY(messagingEnabled, BOOL)
  50. RCT_EXPORT_VIEW_PROPERTY(onMessage, RCTDirectEventBlock)
  51. RCT_EXPORT_METHOD(postMessage:(nonnull NSNumber *)reactTag message:(NSString *)message)
  52. {
  53. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWKWebView *> *viewRegistry) {
  54. RNCWKWebView *view = viewRegistry[reactTag];
  55. if (![view isKindOfClass:[RNCWKWebView class]]) {
  56. RCTLogError(@"Invalid view returned from registry, expecting RNCWKWebView, got: %@", view);
  57. } else {
  58. [view postMessage:message];
  59. }
  60. }];
  61. }
  62. RCT_CUSTOM_VIEW_PROPERTY(bounces, BOOL, RNCWKWebView) {
  63. view.bounces = json == nil ? true : [RCTConvert BOOL: json];
  64. }
  65. RCT_CUSTOM_VIEW_PROPERTY(useSharedProcessPool, BOOL, RNCWKWebView) {
  66. view.useSharedProcessPool = json == nil ? true : [RCTConvert BOOL: json];
  67. }
  68. RCT_CUSTOM_VIEW_PROPERTY(scrollEnabled, BOOL, RNCWKWebView) {
  69. view.scrollEnabled = json == nil ? true : [RCTConvert BOOL: json];
  70. }
  71. RCT_CUSTOM_VIEW_PROPERTY(decelerationRate, CGFloat, RNCWKWebView) {
  72. view.decelerationRate = json == nil ? UIScrollViewDecelerationRateNormal : [RCTConvert CGFloat: json];
  73. }
  74. RCT_CUSTOM_VIEW_PROPERTY(directionalLockEnabled, BOOL, RNCWKWebView) {
  75. view.directionalLockEnabled = json == nil ? true : [RCTConvert BOOL: json];
  76. }
  77. RCT_CUSTOM_VIEW_PROPERTY(showsHorizontalScrollIndicator, BOOL, RNCWKWebView) {
  78. view.showsHorizontalScrollIndicator = json == nil ? true : [RCTConvert BOOL: json];
  79. }
  80. RCT_CUSTOM_VIEW_PROPERTY(showsVerticalScrollIndicator, BOOL, RNCWKWebView) {
  81. view.showsVerticalScrollIndicator = json == nil ? true : [RCTConvert BOOL: json];
  82. }
  83. RCT_EXPORT_METHOD(injectJavaScript:(nonnull NSNumber *)reactTag script:(NSString *)script)
  84. {
  85. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWKWebView *> *viewRegistry) {
  86. RNCWKWebView *view = viewRegistry[reactTag];
  87. if (![view isKindOfClass:[RNCWKWebView class]]) {
  88. RCTLogError(@"Invalid view returned from registry, expecting RNCWKWebView, got: %@", view);
  89. } else {
  90. [view injectJavaScript:script];
  91. }
  92. }];
  93. }
  94. RCT_EXPORT_METHOD(goBack:(nonnull NSNumber *)reactTag)
  95. {
  96. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWKWebView *> *viewRegistry) {
  97. RNCWKWebView *view = viewRegistry[reactTag];
  98. if (![view isKindOfClass:[RNCWKWebView class]]) {
  99. RCTLogError(@"Invalid view returned from registry, expecting RNCWKWebView, got: %@", view);
  100. } else {
  101. [view goBack];
  102. }
  103. }];
  104. }
  105. RCT_EXPORT_METHOD(goForward:(nonnull NSNumber *)reactTag)
  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 goForward];
  113. }
  114. }];
  115. }
  116. RCT_EXPORT_METHOD(reload:(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 reload];
  124. }
  125. }];
  126. }
  127. RCT_EXPORT_METHOD(stopLoading:(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 stopLoading];
  135. }
  136. }];
  137. }
  138. #pragma mark - Exported synchronous methods
  139. - (BOOL) webView:(RNCWKWebView *)webView
  140. shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
  141. withCallback:(RCTDirectEventBlock)callback
  142. {
  143. _shouldStartLoadLock = [[NSConditionLock alloc] initWithCondition:arc4random()];
  144. _shouldStartLoad = YES;
  145. request[@"lockIdentifier"] = @(_shouldStartLoadLock.condition);
  146. callback(request);
  147. // Block the main thread for a maximum of 250ms until the JS thread returns
  148. if ([_shouldStartLoadLock lockWhenCondition:0 beforeDate:[NSDate dateWithTimeIntervalSinceNow:.25]]) {
  149. BOOL returnValue = _shouldStartLoad;
  150. [_shouldStartLoadLock unlock];
  151. _shouldStartLoadLock = nil;
  152. return returnValue;
  153. } else {
  154. RCTLogWarn(@"Did not receive response to shouldStartLoad in time, defaulting to YES");
  155. return YES;
  156. }
  157. }
  158. RCT_EXPORT_METHOD(startLoadWithResult:(BOOL)result lockIdentifier:(NSInteger)lockIdentifier)
  159. {
  160. if ([_shouldStartLoadLock tryLockWhenCondition:lockIdentifier]) {
  161. _shouldStartLoad = result;
  162. [_shouldStartLoadLock unlockWithCondition:0];
  163. } else {
  164. RCTLogWarn(@"startLoadWithResult invoked with invalid lockIdentifier: "
  165. "got %lld, expected %lld", (long long)lockIdentifier, (long long)_shouldStartLoadLock.condition);
  166. }
  167. }
  168. @end