Нема описа

RNCWKWebViewManager.m 6.0KB

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