update flutter_sms to the latest version

This commit is contained in:
2026-01-14 00:23:07 -08:00
parent 33258c580c
commit 6250a11afd
38 changed files with 1192 additions and 480 deletions
@@ -2,60 +2,50 @@ import Flutter
import UIKit
import MessageUI
public class SwiftFlutterSmsPlugin: NSObject, FlutterPlugin, UINavigationControllerDelegate, MFMessageComposeViewControllerDelegate {
var result: FlutterResult?
public class SwiftFlutterSmsPlugin: NSObject, FlutterPlugin, SmsHostApi, UINavigationControllerDelegate, MFMessageComposeViewControllerDelegate {
var result: ((Result<String, Error>) -> Void)?
var _arguments = [String: Any]()
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "flutter_sms", binaryMessenger: registrar.messenger())
let instance = SwiftFlutterSmsPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
SmsHostApiSetup.setUp(binaryMessenger: registrar.messenger(), api: instance)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "sendSMS":
_arguments = call.arguments as! [String : Any];
#if targetEnvironment(simulator)
result(FlutterError(
code: "message_not_sent",
message: "Cannot send message on this device!",
details: "Cannot send SMS and MMS on a Simulator. Test on a real device."
)
)
#else
if (MFMessageComposeViewController.canSendText()) {
self.result = result
let controller = MFMessageComposeViewController()
controller.body = _arguments["message"] as? String
controller.recipients = _arguments["recipients"] as? [String]
controller.messageComposeDelegate = self
UIApplication.shared.keyWindow?.rootViewController?.present(controller, animated: true, completion: nil)
} else {
result(FlutterError(
code: "device_not_capable",
message: "The current device is not capable of sending text messages.",
details: "A device may be unable to send messages if it does not support messaging or if it is not currently configured to send messages. This only applies to the ability to send text messages via iMessage, SMS, and MMS."
)
)
}
#endif
public func sendSms(message: String, recipients: [String], completion: @escaping (Result<String, Error>) -> Void) {
#if targetEnvironment(simulator)
completion(.failure(FlutterError(
code: "message_not_sent",
message: "Cannot send message on this device!",
details: "Cannot send SMS and MMS on a Simulator. Test on a real device."
)))
#else
if (MFMessageComposeViewController.canSendText()) {
self.result = completion
let controller = MFMessageComposeViewController()
controller.body = message
controller.recipients = recipients
controller.messageComposeDelegate = self
UIApplication.shared.keyWindow?.rootViewController?.present(controller, animated: true, completion: nil)
} else {
completion(.failure(FlutterError(
code: "device_not_capable",
message: "The current device is not capable of sending text messages.",
details: "A device may be unable to send messages if it does not support messaging or if it is not currently configured to send messages. This only applies to the ability to send text messages via iMessage, SMS, and MMS."
)))
}
#endif
}
case "canSendSMS":
#if targetEnvironment(simulator)
result(false)
#else
if (MFMessageComposeViewController.canSendText()) {
result(true)
} else {
result(false)
}
#endif
default:
result(FlutterMethodNotImplemented)
break
}
public func canSendSms() -> Bool {
#if targetEnvironment(simulator)
return false
#else
if (MFMessageComposeViewController.canSendText()) {
return true
} else {
return false
}
#endif
}
public func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
@@ -65,7 +55,7 @@ public class SwiftFlutterSmsPlugin: NSObject, FlutterPlugin, UINavigationControl
MessageComposeResult.failed: "failed",
]
if let callback = self.result {
callback(map[result])
callback(.success(map[result] ?? "unknown"))
}
UIApplication.shared.keyWindow?.rootViewController?.dismiss(animated: true, completion: nil)
}