Nessuna descrizione

RNCWKWebViewManager.m 6.0KB

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