FlutterBinaryMessenger.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2013 The Flutter 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. #ifndef FLUTTER_FLUTTERBINARYMESSENGER_H_
  5. #define FLUTTER_FLUTTERBINARYMESSENGER_H_
  6. #import <Foundation/Foundation.h>
  7. #include "FlutterMacros.h"
  8. NS_ASSUME_NONNULL_BEGIN
  9. /**
  10. * A message reply callback.
  11. *
  12. * Used for submitting a binary reply back to a Flutter message sender. Also used
  13. * in for handling a binary message reply received from Flutter.
  14. *
  15. * @param reply The reply.
  16. */
  17. typedef void (^FlutterBinaryReply)(NSData* _Nullable reply);
  18. /**
  19. * A strategy for handling incoming binary messages from Flutter and to send
  20. * asynchronous replies back to Flutter.
  21. *
  22. * @param message The message.
  23. * @param reply A callback for submitting an asynchronous reply to the sender.
  24. */
  25. typedef void (^FlutterBinaryMessageHandler)(NSData* _Nullable message, FlutterBinaryReply reply);
  26. typedef int64_t FlutterBinaryMessengerConnection;
  27. /**
  28. * A facility for communicating with the Flutter side using asynchronous message
  29. * passing with binary messages.
  30. *
  31. * Implementated by:
  32. * - `FlutterBasicMessageChannel`, which supports communication using structured
  33. * messages.
  34. * - `FlutterMethodChannel`, which supports communication using asynchronous
  35. * method calls.
  36. * - `FlutterEventChannel`, which supports commuication using event streams.
  37. */
  38. FLUTTER_EXPORT
  39. @protocol FlutterBinaryMessenger <NSObject>
  40. /**
  41. * Sends a binary message to the Flutter side on the specified channel, expecting
  42. * no reply.
  43. *
  44. * @param channel The channel name.
  45. * @param message The message.
  46. */
  47. - (void)sendOnChannel:(NSString*)channel message:(NSData* _Nullable)message;
  48. /**
  49. * Sends a binary message to the Flutter side on the specified channel, expecting
  50. * an asynchronous reply.
  51. *
  52. * @param channel The channel name.
  53. * @param message The message.
  54. * @param callback A callback for receiving a reply.
  55. */
  56. - (void)sendOnChannel:(NSString*)channel
  57. message:(NSData* _Nullable)message
  58. binaryReply:(FlutterBinaryReply _Nullable)callback;
  59. /**
  60. * Registers a message handler for incoming binary messages from the Flutter side
  61. * on the specified channel.
  62. *
  63. * Replaces any existing handler. Use a `nil` handler for unregistering the
  64. * existing handler.
  65. *
  66. * @param channel The channel name.
  67. * @param handler The message handler.
  68. * @return An identifier that represents the connection that was just created to the channel.
  69. */
  70. - (FlutterBinaryMessengerConnection)setMessageHandlerOnChannel:(NSString*)channel
  71. binaryMessageHandler:
  72. (FlutterBinaryMessageHandler _Nullable)handler;
  73. /**
  74. * Clears out a channel's message handler if that handler is still the one that
  75. * was created as a result of
  76. * `setMessageHandlerOnChannel:binaryMessageHandler:`.
  77. *
  78. * @param connection The result from `setMessageHandlerOnChannel:binaryMessageHandler:`.
  79. */
  80. - (void)cleanupConnection:(FlutterBinaryMessengerConnection)connection;
  81. @end
  82. NS_ASSUME_NONNULL_END
  83. #endif // FLUTTER_FLUTTERBINARYMESSENGER_H_