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

+ 16
- 1
ios/RNCWebView.m View File

230
   [wkWebViewConfig.userContentController addUserScript:script];
230
   [wkWebViewConfig.userContentController addUserScript:script];
231
 
231
 
232
   if (_messagingEnabled) {
232
   if (_messagingEnabled) {
233
+    for (NSString *handler in _messageHandlers)
234
+    {
235
+        [wkWebViewConfig.userContentController addScriptMessageHandler:[[RNCWeakScriptMessageDelegate alloc] initWithDelegate:self]
236
+        name:handler];
237
+    }
238
+      
233
     [wkWebViewConfig.userContentController addScriptMessageHandler:[[RNCWeakScriptMessageDelegate alloc] initWithDelegate:self]
239
     [wkWebViewConfig.userContentController addScriptMessageHandler:[[RNCWeakScriptMessageDelegate alloc] initWithDelegate:self]
234
                                                               name:MessageHandlerName];
240
                                                               name:MessageHandlerName];
235
 
241
 
523
       [event addEntriesFromDictionary: @{@"data": message.body}];
529
       [event addEntriesFromDictionary: @{@"data": message.body}];
524
       _onMessage(event);
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
         _onHttpError(event);
1091
         _onHttpError(event);
1077
       }
1092
       }
1078
     }
1093
     }
1079
-  }  
1094
+  }
1080
 
1095
 
1081
   decisionHandler(WKNavigationResponsePolicyAllow);
1096
   decisionHandler(WKNavigationResponsePolicyAllow);
1082
 }
1097
 }

+ 1
- 0
ios/RNCWebViewManager.m View File

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