설명 없음

RCTUIWebViewManager.m 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #import "RCTUIWebViewManager.h"
  2. #import <React/RCTBridge.h>
  3. #import <React/RCTUIManager.h>
  4. #import <React/UIView+React.h>
  5. #import "RCTUIWebView.h"
  6. @interface RCTUIWebViewManager () <RCTUIWebViewDelegate>
  7. @end
  8. @implementation RCTUIWebViewManager
  9. {
  10. NSConditionLock *_shouldStartLoadLock;
  11. BOOL _shouldStartLoad;
  12. }
  13. RCT_EXPORT_MODULE()
  14. - (UIView *)view
  15. {
  16. RCTUIWebView *webView = [RCTUIWebView new];
  17. webView.delegate = self;
  18. return webView;
  19. }
  20. RCT_EXPORT_VIEW_PROPERTY(source, NSDictionary)
  21. RCT_REMAP_VIEW_PROPERTY(bounces, _webView.scrollView.bounces, BOOL)
  22. RCT_REMAP_VIEW_PROPERTY(scrollEnabled, _webView.scrollView.scrollEnabled, BOOL)
  23. RCT_REMAP_VIEW_PROPERTY(decelerationRate, _webView.scrollView.decelerationRate, CGFloat)
  24. RCT_EXPORT_VIEW_PROPERTY(scalesPageToFit, BOOL)
  25. RCT_EXPORT_VIEW_PROPERTY(messagingEnabled, BOOL)
  26. RCT_EXPORT_VIEW_PROPERTY(injectedJavaScript, NSString)
  27. RCT_EXPORT_VIEW_PROPERTY(contentInset, UIEdgeInsets)
  28. RCT_EXPORT_VIEW_PROPERTY(automaticallyAdjustContentInsets, BOOL)
  29. RCT_EXPORT_VIEW_PROPERTY(onLoadingStart, RCTDirectEventBlock)
  30. RCT_EXPORT_VIEW_PROPERTY(onLoadingFinish, RCTDirectEventBlock)
  31. RCT_EXPORT_VIEW_PROPERTY(onLoadingError, RCTDirectEventBlock)
  32. RCT_EXPORT_VIEW_PROPERTY(onMessage, RCTDirectEventBlock)
  33. RCT_EXPORT_VIEW_PROPERTY(onShouldStartLoadWithRequest, RCTDirectEventBlock)
  34. RCT_REMAP_VIEW_PROPERTY(allowsInlineMediaPlayback, _webView.allowsInlineMediaPlayback, BOOL)
  35. RCT_REMAP_VIEW_PROPERTY(mediaPlaybackRequiresUserAction, _webView.mediaPlaybackRequiresUserAction, BOOL)
  36. RCT_REMAP_VIEW_PROPERTY(dataDetectorTypes, _webView.dataDetectorTypes, UIDataDetectorTypes)
  37. RCT_EXPORT_METHOD(goBack:(nonnull NSNumber *)reactTag)
  38. {
  39. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTUIWebView *> *viewRegistry) {
  40. RCTUIWebView *view = viewRegistry[reactTag];
  41. if (![view isKindOfClass:[RCTUIWebView class]]) {
  42. RCTLogError(@"Invalid view returned from registry, expecting RCTUIWebView, got: %@", view);
  43. } else {
  44. [view goBack];
  45. }
  46. }];
  47. }
  48. RCT_EXPORT_METHOD(goForward:(nonnull NSNumber *)reactTag)
  49. {
  50. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
  51. id view = viewRegistry[reactTag];
  52. if (![view isKindOfClass:[RCTUIWebView class]]) {
  53. RCTLogError(@"Invalid view returned from registry, expecting RCTUIWebView, got: %@", view);
  54. } else {
  55. [view goForward];
  56. }
  57. }];
  58. }
  59. RCT_EXPORT_METHOD(reload:(nonnull NSNumber *)reactTag)
  60. {
  61. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTUIWebView *> *viewRegistry) {
  62. RCTUIWebView *view = viewRegistry[reactTag];
  63. if (![view isKindOfClass:[RCTUIWebView class]]) {
  64. RCTLogError(@"Invalid view returned from registry, expecting RCTUIWebView, got: %@", view);
  65. } else {
  66. [view reload];
  67. }
  68. }];
  69. }
  70. RCT_EXPORT_METHOD(stopLoading:(nonnull NSNumber *)reactTag)
  71. {
  72. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTUIWebView *> *viewRegistry) {
  73. RCTUIWebView *view = viewRegistry[reactTag];
  74. if (![view isKindOfClass:[RCTUIWebView class]]) {
  75. RCTLogError(@"Invalid view returned from registry, expecting RCTUIWebView, got: %@", view);
  76. } else {
  77. [view stopLoading];
  78. }
  79. }];
  80. }
  81. RCT_EXPORT_METHOD(postMessage:(nonnull NSNumber *)reactTag message:(NSString *)message)
  82. {
  83. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTUIWebView *> *viewRegistry) {
  84. RCTUIWebView *view = viewRegistry[reactTag];
  85. if (![view isKindOfClass:[RCTUIWebView class]]) {
  86. RCTLogError(@"Invalid view returned from registry, expecting RCTUIWebView, got: %@", view);
  87. } else {
  88. [view postMessage:message];
  89. }
  90. }];
  91. }
  92. RCT_EXPORT_METHOD(injectJavaScript:(nonnull NSNumber *)reactTag script:(NSString *)script)
  93. {
  94. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTUIWebView *> *viewRegistry) {
  95. RCTUIWebView *view = viewRegistry[reactTag];
  96. if (![view isKindOfClass:[RCTUIWebView class]]) {
  97. RCTLogError(@"Invalid view returned from registry, expecting RCTUIWebView, got: %@", view);
  98. } else {
  99. [view injectJavaScript:script];
  100. }
  101. }];
  102. }
  103. #pragma mark - Exported synchronous methods
  104. - (BOOL)webView:(__unused RCTUIWebView *)webView
  105. shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
  106. withCallback:(RCTDirectEventBlock)callback
  107. {
  108. _shouldStartLoadLock = [[NSConditionLock alloc] initWithCondition:arc4random()];
  109. _shouldStartLoad = YES;
  110. request[@"lockIdentifier"] = @(_shouldStartLoadLock.condition);
  111. callback(request);
  112. // Block the main thread for a maximum of 250ms until the JS thread returns
  113. if ([_shouldStartLoadLock lockWhenCondition:0 beforeDate:[NSDate dateWithTimeIntervalSinceNow:.25]]) {
  114. BOOL returnValue = _shouldStartLoad;
  115. [_shouldStartLoadLock unlock];
  116. _shouldStartLoadLock = nil;
  117. return returnValue;
  118. } else {
  119. RCTLogWarn(@"Did not receive response to shouldStartLoad in time, defaulting to YES");
  120. return YES;
  121. }
  122. }
  123. RCT_EXPORT_METHOD(startLoadWithResult:(BOOL)result lockIdentifier:(NSInteger)lockIdentifier)
  124. {
  125. if ([_shouldStartLoadLock tryLockWhenCondition:lockIdentifier]) {
  126. _shouldStartLoad = result;
  127. [_shouldStartLoadLock unlockWithCondition:0];
  128. } else {
  129. RCTLogWarn(@"startLoadWithResult invoked with invalid lockIdentifier: "
  130. "got %lld, expected %lld", (long long)lockIdentifier, (long long)_shouldStartLoadLock.condition);
  131. }
  132. }
  133. @end