From 2fccc8b0f18780b60adf6e6076e6ec6840160c1f Mon Sep 17 00:00:00 2001 From: "leojoseph.r" <58416454+rleojoseph@users.noreply.github.com> Date: Mon, 16 Oct 2023 20:46:00 +0900 Subject: [PATCH] Fix character escaping (#529) * Fix character escaping * Update MiniAppViewHandler.swift * Update MiniAppViewHandler.swift * Update MiniAppViewHandler.swift --- Sources/Classes/core/View/MiniAppViewHandler.swift | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Sources/Classes/core/View/MiniAppViewHandler.swift b/Sources/Classes/core/View/MiniAppViewHandler.swift index 7b652015..694e26f1 100644 --- a/Sources/Classes/core/View/MiniAppViewHandler.swift +++ b/Sources/Classes/core/View/MiniAppViewHandler.swift @@ -831,7 +831,11 @@ extension MiniAppViewHandler: MiniAppCallbackDelegate { } func didReceiveEvent(_ event: MiniAppEvent, message: String) { - let messageBody = Constants.JavaScript.eventCallback + "('\(event.rawValue)'," + "'\(message)')" + var encodedMessage = message + if event == .miniappReceiveJsonString { + encodedMessage = message.base64Encoded() ?? "" + } + let messageBody = Constants.JavaScript.eventCallback + "('\(event.rawValue)'," + "'\(encodedMessage)')" messageBodies.append(messageBody) MiniAppLogger.d(messageBody, "♨️️") webView?.evaluateJavaScript(messageBody) @@ -932,4 +936,11 @@ extension MiniAppViewHandler { self.didReceiveEvent(MiniAppEvent.miniappReceiveJsonString, message: jsonString ?? "") } } + +extension String { + func base64Encoded() -> String? { + guard let data = data(using: .nonLossyASCII) else { return "" } + return data.base64EncodedString() + } +} // swiftlint:enable file_length function_body_length