JavaScriptChannelHandler.m 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #import "JavaScriptChannelHandler.h"
  5. @implementation FLTJavaScriptChannel {
  6. FlutterMethodChannel* _methodChannel;
  7. NSString* _javaScriptChannelName;
  8. }
  9. - (instancetype)initWithMethodChannel:(FlutterMethodChannel*)methodChannel
  10. javaScriptChannelName:(NSString*)javaScriptChannelName {
  11. self = [super init];
  12. NSAssert(methodChannel != nil, @"methodChannel must not be null.");
  13. NSAssert(javaScriptChannelName != nil, @"javaScriptChannelName must not be null.");
  14. if (self) {
  15. _methodChannel = methodChannel;
  16. _javaScriptChannelName = javaScriptChannelName;
  17. }
  18. return self;
  19. }
  20. - (void)userContentController:(WKUserContentController*)userContentController
  21. didReceiveScriptMessage:(WKScriptMessage*)message {
  22. NSAssert(_methodChannel != nil, @"Can't send a message to an unitialized JavaScript channel.");
  23. NSAssert(_javaScriptChannelName != nil,
  24. @"Can't send a message to an unitialized JavaScript channel.");
  25. NSDictionary* arguments = @{
  26. @"channel" : _javaScriptChannelName,
  27. @"message" : [NSString stringWithFormat:@"%@", message.body]
  28. };
  29. [_methodChannel invokeMethod:@"javascriptChannelMessage" arguments:arguments];
  30. }
  31. @end