jcarioti 4 years ago
parent
commit
90e0bc445d
No account linked to committer's email address
3 changed files with 18 additions and 1 deletions
  1. 1
    0
      ios/RNCWebView.h
  2. 16
    1
      ios/RNCWebView.m
  3. 1
    0
      ios/RNCWebViewManager.m

+ 1
- 0
ios/RNCWebView.h View File

@@ -29,6 +29,7 @@
29 29
 @property (nonatomic, weak) id<RNCWebViewDelegate> _Nullable delegate;
30 30
 @property (nonatomic, copy) NSDictionary * _Nullable source;
31 31
 @property (nonatomic, assign) BOOL messagingEnabled;
32
+@property (nonatomic, copy) NSArray  * _Nullable messageHandlers;
32 33
 @property (nonatomic, copy) NSString * _Nullable injectedJavaScript;
33 34
 @property (nonatomic, copy) NSString * _Nullable injectedJavaScriptBeforeContentLoaded;
34 35
 @property (nonatomic, assign) BOOL scrollEnabled;

+ 16
- 1
ios/RNCWebView.m View File

@@ -230,6 +230,12 @@ static NSDictionary* customCertificatesForHost;
230 230
   [wkWebViewConfig.userContentController addUserScript:script];
231 231
 
232 232
   if (_messagingEnabled) {
233
+    for (NSString *handler in _messageHandlers)
234
+    {
235
+        [wkWebViewConfig.userContentController addScriptMessageHandler:[[RNCWeakScriptMessageDelegate alloc] initWithDelegate:self]
236
+        name:handler];
237
+    }
238
+      
233 239
     [wkWebViewConfig.userContentController addScriptMessageHandler:[[RNCWeakScriptMessageDelegate alloc] initWithDelegate:self]
234 240
                                                               name:MessageHandlerName];
235 241
 
@@ -523,6 +529,15 @@ static NSDictionary* customCertificatesForHost;
523 529
       [event addEntriesFromDictionary: @{@"data": message.body}];
524 530
       _onMessage(event);
525 531
     }
532
+  } else {
533
+    if (_onMessage) {
534
+      NSMutableDictionary<NSString *, id> *event = [self baseEvent];
535
+      if (message.body) {
536
+        [event addEntriesFromDictionary: @{@"data":  message.body}];
537
+      }
538
+      [event addEntriesFromDictionary: @{@"handler": message.name}];
539
+      _onMessage(event);
540
+    }
526 541
   }
527 542
 }
528 543
 
@@ -1076,7 +1091,7 @@ static NSDictionary* customCertificatesForHost;
1076 1091
         _onHttpError(event);
1077 1092
       }
1078 1093
     }
1079
-  }  
1094
+  }
1080 1095
 
1081 1096
   decisionHandler(WKNavigationResponsePolicyAllow);
1082 1097
 }

+ 1
- 0
ios/RNCWebViewManager.m View File

@@ -70,6 +70,7 @@ RCT_EXPORT_VIEW_PROPERTY(contentInsetAdjustmentBehavior, UIScrollViewContentInse
70 70
  * Expose methods to enable messaging the webview.
71 71
  */
72 72
 RCT_EXPORT_VIEW_PROPERTY(messagingEnabled, BOOL)
73
+RCT_EXPORT_VIEW_PROPERTY(messageHandlers, NSArray)
73 74
 RCT_EXPORT_VIEW_PROPERTY(onMessage, RCTDirectEventBlock)
74 75
 RCT_EXPORT_VIEW_PROPERTY(onScroll, RCTDirectEventBlock)
75 76