Açıklama Yok

RNCWKWebViewManager.m 7.3KB

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