jcarioti 5 年前
父节点
当前提交
90e0bc445d
没有帐户链接到提交者的电子邮件
共有 3 个文件被更改,包括 18 次插入1 次删除
  1. 1
    0
      ios/RNCWebView.h
  2. 16
    1
      ios/RNCWebView.m
  3. 1
    0
      ios/RNCWebViewManager.m

+ 1
- 0
ios/RNCWebView.h 查看文件

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 查看文件

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 查看文件

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