From f1594ac5963dea0d79ffdadd03f82a2a6572ead5 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Sun, 24 Aug 2025 22:47:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E7=9F=AD=E4=BF=A1=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=8F=96=E6=B6=88=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat_api/SystemFunctionHandler.swift | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/local_plugins/chat_api/ios/chat_api/Sources/chat_api/SystemFunctionHandler.swift b/local_plugins/chat_api/ios/chat_api/Sources/chat_api/SystemFunctionHandler.swift index 76ca62770..6f2bcf72d 100644 --- a/local_plugins/chat_api/ios/chat_api/Sources/chat_api/SystemFunctionHandler.swift +++ b/local_plugins/chat_api/ios/chat_api/Sources/chat_api/SystemFunctionHandler.swift @@ -568,7 +568,9 @@ private class TranslationModeHandler: FunctionHandler { } /// 发送短信处理器 -private class SendTextMessageHandler: FunctionHandler { +private class SendTextMessageHandler: NSObject, FunctionHandler, MFMessageComposeViewControllerDelegate { + private var continuation: CheckedContinuation? + func handle(arguments: [String: Any]) async throws -> String { guard let contact = arguments["contact"] as? String, let message = arguments["message"] as? String else { @@ -581,17 +583,19 @@ private class SendTextMessageHandler: FunctionHandler { private func sendTextMessage(contact: String, message: String) async -> Result { return await withCheckedContinuation { continuation in + self.continuation = continuation DispatchQueue.main.async { if MFMessageComposeViewController.canSendText() { let messageVC = MFMessageComposeViewController() messageVC.recipients = [contact] messageVC.body = message + messageVC.messageComposeDelegate = self // 设置 delegate if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene, let window = windowScene.windows.first, let rootViewController = window.rootViewController { rootViewController.present(messageVC, animated: true) - continuation.resume(returning: Result(success: true, message: "短信界面已打开")) + // 不在这里立即返回结果,等待用户操作 } else { continuation.resume(returning: Result(success: false, message: "无法打开短信界面")) } @@ -601,6 +605,33 @@ private class SendTextMessageHandler: FunctionHandler { } } } + + // MARK: - MFMessageComposeViewControllerDelegate + func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) { + controller.dismiss(animated: true) { + // 在界面关闭后返回结果 + let message: String + let success: Bool + + switch result { + case .cancelled: + message = "用户取消发送短信" + success = false + case .sent: + message = "短信发送成功" + success = true + case .failed: + message = "短信发送失败" + success = false + @unknown default: + message = "未知结果" + success = false + } + + self.continuation?.resume(returning: Result(success: success, message: message)) + self.continuation = nil + } + } } /// 拨打电话处理器