diff --git a/shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h b/shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h index 07815c6494411ba083595c1f45afc56cfff99c32..2c35fd42599c4c10fe39130c8059b4b246c5d332 100644 --- a/shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h +++ b/shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h @@ -61,10 +61,7 @@ FLUTTER_EXPORT */ - (void)sendOnChannel:(NSString*)channel message:(NSData* _Nullable)message - binaryReply:(FlutterBinaryReply _Nullable)callback - // TODO: Add macOS support for replies once - // https://github.com/flutter/flutter/issues/18852 is fixed. - API_UNAVAILABLE(macos); + binaryReply:(FlutterBinaryReply _Nullable)callback; /** * Registers a message handler for incoming binary messages from the Flutter side diff --git a/shell/platform/darwin/common/framework/Headers/FlutterChannels.h b/shell/platform/darwin/common/framework/Headers/FlutterChannels.h index 9245d79a8ec6c9acf5fdf24494507bbf6bd920ae..83c9e989762e49e6a7567dfd3118ec056a557982 100644 --- a/shell/platform/darwin/common/framework/Headers/FlutterChannels.h +++ b/shell/platform/darwin/common/framework/Headers/FlutterChannels.h @@ -103,11 +103,7 @@ FLUTTER_EXPORT * @param message The message. Must be supported by the codec of this channel. * @param callback A callback to be invoked with the message reply from Flutter. */ -- (void)sendMessage:(id _Nullable)message - reply:(FlutterReply _Nullable)callback - // TODO: Add macOS support for replies once - // https://github.com/flutter/flutter/issues/18852 is fixed. - API_UNAVAILABLE(macos); +- (void)sendMessage:(id _Nullable)message reply:(FlutterReply _Nullable)callback; /** * Registers a message handler with this channel. diff --git a/shell/platform/darwin/common/framework/Source/FlutterCodecs.mm b/shell/platform/darwin/common/framework/Source/FlutterCodecs.mm index b06dab7b85577f0c9261473c95b478166aa2b1f0..b55cf5277516bb79040029fc73ad9dc15e6ec072 100644 --- a/shell/platform/darwin/common/framework/Source/FlutterCodecs.mm +++ b/shell/platform/darwin/common/framework/Source/FlutterCodecs.mm @@ -73,7 +73,7 @@ } - (id)decode:(NSData*)message { - if (message == nil) + if (message.length == 0) return nil; BOOL isSimpleValue = NO; id decoded = nil; diff --git a/shell/platform/darwin/macos/framework/Source/FLEEngine.mm b/shell/platform/darwin/macos/framework/Source/FLEEngine.mm index cfb6669cd74f7db801966701b66dab8c55fb4589..8d36532e098455dda648fcfde7d8dac8aabda164 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEEngine.mm +++ b/shell/platform/darwin/macos/framework/Source/FLEEngine.mm @@ -345,6 +345,49 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FLEEngine* } } +- (void)sendOnChannel:(NSString*)channel + message:(NSData* _Nullable)message + binaryReply:(FlutterBinaryReply _Nullable)callback { + struct Captures { + FlutterBinaryReply reply; + }; + auto captures = std::make_unique(); + captures->reply = callback; + auto message_reply = [](const uint8_t* data, size_t data_size, void* user_data) { + auto captures = reinterpret_cast(user_data); + NSData* reply_data = [NSData dataWithBytes:(void*)data length:data_size]; + captures->reply(reply_data); + delete captures; + }; + + FlutterPlatformMessageResponseHandle* response_handle = nullptr; + FlutterEngineResult result = FlutterPlatformMessageCreateResponseHandle( + _engine, message_reply, captures.get(), &response_handle); + if (result != kSuccess) { + NSLog(@"Failed to create a FlutterPlatformMessageResponseHandle"); + return; + } + captures.release(); + + FlutterPlatformMessage platformMessage = { + .struct_size = sizeof(FlutterPlatformMessage), + .channel = [channel UTF8String], + .message = static_cast(message.bytes), + .message_size = message.length, + .response_handle = response_handle, + }; + + result = FlutterEngineSendPlatformMessage(_engine, &platformMessage); + if (result != kSuccess) { + NSLog(@"Failed to send message to Flutter engine on channel '%@' (%d).", channel, result); + } + + result = FlutterPlatformMessageReleaseResponseHandle(_engine, response_handle); + if (result != kSuccess) { + NSLog(@"Failed to release the response handle"); + }; +} + - (void)setMessageHandlerOnChannel:(nonnull NSString*)channel binaryMessageHandler:(nullable FlutterBinaryMessageHandler)handler { _messageHandlers[channel] = [handler copy];