Geen omschrijving

RNCUIWebViewManager.m 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #import "RNCUIWebViewManager.h"
  2. #import <React/RCTBridge.h>
  3. #import <React/RCTUIManager.h>
  4. #import <React/UIView+React.h>
  5. #import "RNCUIWebView.h"
  6. @interface RNCUIWebViewManager () <RNCUIWebViewDelegate>
  7. @end
  8. @implementation RNCUIWebViewManager
  9. {
  10. NSConditionLock *_shouldStartLoadLock;
  11. BOOL _shouldStartLoad;
  12. }
  13. RCT_EXPORT_MODULE()
  14. - (UIView *)view
  15. {
  16. RNCUIWebView *webView = [RNCUIWebView 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_REMAP_VIEW_PROPERTY(showsHorizontalScrollIndicator, _webView.scrollView.showsHorizontalScrollIndicator, BOOL)
  38. RCT_REMAP_VIEW_PROPERTY(showsVerticalScrollIndicator, _webView.scrollView.showsVerticalScrollIndicator, BOOL)
  39. RCT_REMAP_VIEW_PROPERTY(directionalLockEnabled, _webView.scrollView.directionalLockEnabled, BOOL)
  40. RCT_EXPORT_METHOD(goBack:(nonnull NSNumber *)reactTag)
  41. {
  42. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCUIWebView *> *viewRegistry) {
  43. RNCUIWebView *view = viewRegistry[reactTag];
  44. if (![view isKindOfClass:[RNCUIWebView class]]) {
  45. RCTLogError(@"Invalid view returned from registry, expecting RNCUIWebView, got: %@", view);
  46. } else {
  47. [view goBack];
  48. }
  49. }];
  50. }
  51. RCT_EXPORT_METHOD(goForward:(nonnull NSNumber *)reactTag)
  52. {
  53. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
  54. RNCUIWebView *view = viewRegistry[reactTag];
  55. if (![view isKindOfClass:[RNCUIWebView class]]) {
  56. RCTLogError(@"Invalid view returned from registry, expecting RNCUIWebView, got: %@", view);
  57. } else {
  58. [view goForward];
  59. }
  60. }];
  61. }
  62. RCT_EXPORT_METHOD(reload:(nonnull NSNumber *)reactTag)
  63. {
  64. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCUIWebView *> *viewRegistry) {
  65. RNCUIWebView *view = viewRegistry[reactTag];
  66. if (![view isKindOfClass:[RNCUIWebView class]]) {
  67. RCTLogError(@"Invalid view returned from registry, expecting RNCUIWebView, got: %@", view);
  68. } else {
  69. [view reload];
  70. }
  71. }];
  72. }
  73. RCT_EXPORT_METHOD(stopLoading:(nonnull NSNumber *)reactTag)
  74. {
  75. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCUIWebView *> *viewRegistry) {
  76. RNCUIWebView *view = viewRegistry[reactTag];
  77. if (![view isKindOfClass:[RNCUIWebView class]]) {
  78. RCTLogError(@"Invalid view returned from registry, expecting RNCUIWebView, got: %@", view);
  79. } else {
  80. [view stopLoading];
  81. }
  82. }];
  83. }
  84. RCT_EXPORT_METHOD(postMessage:(nonnull NSNumber *)reactTag message:(NSString *)message)
  85. {
  86. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCUIWebView *> *viewRegistry) {
  87. RNCUIWebView *view = viewRegistry[reactTag];
  88. if (![view isKindOfClass:[RNCUIWebView class]]) {
  89. RCTLogError(@"Invalid view returned from registry, expecting RNCUIWebView, got: %@", view);
  90. } else {
  91. [view postMessage:message];
  92. }
  93. }];
  94. }
  95. RCT_EXPORT_METHOD(injectJavaScript:(nonnull NSNumber *)reactTag script:(NSString *)script)
  96. {
  97. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCUIWebView *> *viewRegistry) {
  98. RNCUIWebView *view = viewRegistry[reactTag];
  99. if (![view isKindOfClass:[RNCUIWebView class]]) {
  100. RCTLogError(@"Invalid view returned from registry, expecting RNCUIWebView, got: %@", view);
  101. } else {
  102. [view injectJavaScript:script];
  103. }
  104. }];
  105. }
  106. #pragma mark - Exported synchronous methods
  107. - (BOOL)webView:(__unused RNCUIWebView *)webView
  108. shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
  109. withCallback:(RCTDirectEventBlock)callback
  110. {
  111. _shouldStartLoadLock = [[NSConditionLock alloc] initWithCondition:arc4random()];
  112. _shouldStartLoad = YES;
  113. request[@"lockIdentifier"] = @(_shouldStartLoadLock.condition);
  114. callback(request);
  115. // Block the main thread for a maximum of 250ms until the JS thread returns
  116. if ([_shouldStartLoadLock lockWhenCondition:0 beforeDate:[NSDate dateWithTimeIntervalSinceNow:.25]]) {
  117. BOOL returnValue = _shouldStartLoad;
  118. [_shouldStartLoadLock unlock];
  119. _shouldStartLoadLock = nil;
  120. return returnValue;
  121. } else {
  122. RCTLogWarn(@"Did not receive response to shouldStartLoad in time, defaulting to YES");
  123. return YES;
  124. }
  125. }
  126. RCT_EXPORT_METHOD(startLoadWithResult:(BOOL)result lockIdentifier:(NSInteger)lockIdentifier)
  127. {
  128. if ([_shouldStartLoadLock tryLockWhenCondition:lockIdentifier]) {
  129. _shouldStartLoad = result;
  130. [_shouldStartLoadLock unlockWithCondition:0];
  131. } else {
  132. RCTLogWarn(@"startLoadWithResult invoked with invalid lockIdentifier: "
  133. "got %lld, expected %lld", (long long)lockIdentifier, (long long)_shouldStartLoadLock.condition);
  134. }
  135. }
  136. @end