iOS Finished
This commit is contained in:
parent
44851e8632
commit
04383b7179
@ -4,8 +4,7 @@ import MessageUI
|
|||||||
|
|
||||||
public class SwiftFlutterSmsPlugin: NSObject, FlutterPlugin, UINavigationControllerDelegate, MFMessageComposeViewControllerDelegate {
|
public class SwiftFlutterSmsPlugin: NSObject, FlutterPlugin, UINavigationControllerDelegate, MFMessageComposeViewControllerDelegate {
|
||||||
var message = "Please Send Message"
|
var message = "Please Send Message"
|
||||||
// let flutterViewController: FlutterViewController
|
var _arguments = [String: Any]()
|
||||||
// let channel: FlutterMethodChannel
|
|
||||||
|
|
||||||
public static func register(with registrar: FlutterPluginRegistrar) {
|
public static func register(with registrar: FlutterPluginRegistrar) {
|
||||||
let channel = FlutterMethodChannel(name: "flutter_sms", binaryMessenger: registrar.messenger())
|
let channel = FlutterMethodChannel(name: "flutter_sms", binaryMessenger: registrar.messenger())
|
||||||
@ -14,53 +13,24 @@ public class SwiftFlutterSmsPlugin: NSObject, FlutterPlugin, UINavigationControl
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
|
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
|
||||||
|
_arguments = call.arguments as! [String : Any];
|
||||||
switch call.method {
|
switch call.method {
|
||||||
case "sendSMS":
|
case "sendSMS":
|
||||||
// let sourceType: UIImagePickerControllerSourceType = "camera" == (call.arguments as? String) ? .camera : .photoLibrary
|
|
||||||
let controller = MFMessageComposeViewController()
|
let controller = MFMessageComposeViewController()
|
||||||
controller.body = "call.arguments"
|
controller.body = _arguments["message"] as? String
|
||||||
controller.recipients = ["1234567890"]
|
controller.recipients = _arguments["recipients"] as? [String]
|
||||||
controller.messageComposeDelegate = self
|
controller.messageComposeDelegate = self
|
||||||
// let messagePicker = self.buildMessageUI( message: "Test", recipients: ["2051234567"], completion: result)
|
|
||||||
// self.flutterViewController.present(controller, animated: true, completion: nil)
|
|
||||||
UIApplication.shared.keyWindow?.rootViewController?.present(controller, animated: true, completion: nil)
|
UIApplication.shared.keyWindow?.rootViewController?.present(controller, animated: true, completion: nil)
|
||||||
default:
|
default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// init(flutterViewController: FlutterViewController) {
|
|
||||||
//
|
|
||||||
// self.flutterViewController = flutterViewController
|
|
||||||
//// channel = FlutterMethodChannel(name: "flutter_sms", binaryMessenger: flutterViewController)
|
|
||||||
//// let instance = SwiftFlutterSmsPlugin(flutterViewController: flutterViewController)
|
|
||||||
// }
|
|
||||||
|
|
||||||
public func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
|
public func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
|
||||||
UIApplication.shared.keyWindow?.rootViewController?.dismiss(animated: true, completion: nil)
|
UIApplication.shared.keyWindow?.rootViewController?.dismiss(animated: true, completion: nil)
|
||||||
message = "Sent!"
|
message = "Sent!"
|
||||||
}
|
}
|
||||||
|
|
||||||
// func buildMessageUI(message: String, recipients: [String], completion: @escaping (_ result: Any?) -> Void) -> UIViewController {
|
|
||||||
// if message.isEmpty {
|
|
||||||
// let alert = UIAlertController(title: "Error", message: "Message Required", preferredStyle: .alert)
|
|
||||||
// alert.addAction(UIAlertAction(title: "OK", style: .default) { action in
|
|
||||||
// completion(FlutterError(code: "message_error", message: "message not available", details: nil))
|
|
||||||
// })
|
|
||||||
// return alert
|
|
||||||
// } else {
|
|
||||||
//
|
|
||||||
//// return MFMessageComposeViewController() {
|
|
||||||
//// self.flutterViewController.dismiss(animated: true, completion: nil)
|
|
||||||
//// if let image = image {
|
|
||||||
//// completion(self.saveToFile(image: image))
|
|
||||||
//// } else {
|
|
||||||
//// completion(FlutterError(code: "user_cancelled", message: "User did cancel", details: nil))
|
|
||||||
//// }
|
|
||||||
//// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
private func saveToFile(image: UIImage) -> Any {
|
private func saveToFile(image: UIImage) -> Any {
|
||||||
guard let data = UIImageJPEGRepresentation(image, 1.0) else {
|
guard let data = UIImageJPEGRepresentation(image, 1.0) else {
|
||||||
return FlutterError(code: "image_encoding_error", message: "Could not read image", details: nil)
|
return FlutterError(code: "image_encoding_error", message: "Could not read image", details: nil)
|
||||||
@ -75,69 +45,3 @@ public class SwiftFlutterSmsPlugin: NSObject, FlutterPlugin, UINavigationControl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ECMMessageComposerBuilder: NSObject {
|
|
||||||
|
|
||||||
@objc private dynamic var customWindow: UIWindow?
|
|
||||||
private var body: String?
|
|
||||||
private var phoneNumber: String?
|
|
||||||
fileprivate var messageController: MFMessageComposeViewController?
|
|
||||||
|
|
||||||
var canCompose: Bool {
|
|
||||||
return MFMessageComposeViewController.canSendText()
|
|
||||||
}
|
|
||||||
|
|
||||||
func body(_ body: String?) -> ECMMessageComposerBuilder {
|
|
||||||
self.body = body
|
|
||||||
return self
|
|
||||||
}
|
|
||||||
|
|
||||||
func phoneNumber(_ phone: String?) -> ECMMessageComposerBuilder {
|
|
||||||
self.phoneNumber = phone
|
|
||||||
return self
|
|
||||||
}
|
|
||||||
|
|
||||||
func build() -> UIViewController? {
|
|
||||||
guard canCompose else { return nil }
|
|
||||||
|
|
||||||
messageController = MFMessageComposeViewController()
|
|
||||||
messageController?.body = body
|
|
||||||
if let phone = phoneNumber {
|
|
||||||
messageController?.recipients = [phone]
|
|
||||||
}
|
|
||||||
messageController?.messageComposeDelegate = self
|
|
||||||
|
|
||||||
return messageController
|
|
||||||
}
|
|
||||||
|
|
||||||
func show() {
|
|
||||||
customWindow = UIWindow(frame: UIScreen.main.bounds)
|
|
||||||
customWindow?.rootViewController = UIViewController()
|
|
||||||
|
|
||||||
// Move it to the top
|
|
||||||
let topWindow = UIApplication.shared.windows.last
|
|
||||||
customWindow?.windowLevel = (topWindow?.windowLevel ?? 0) + 1
|
|
||||||
|
|
||||||
// and present it
|
|
||||||
customWindow?.makeKeyAndVisible()
|
|
||||||
|
|
||||||
if let messageController = build() {
|
|
||||||
customWindow?.rootViewController?.present(messageController, animated: true, completion: nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func hide(animated: Bool = true) {
|
|
||||||
messageController?.dismiss(animated: animated, completion: nil)
|
|
||||||
messageController = nil
|
|
||||||
customWindow?.isHidden = true
|
|
||||||
customWindow = nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension ECMMessageComposerBuilder: MFMessageComposeViewControllerDelegate {
|
|
||||||
|
|
||||||
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
|
|
||||||
controller.dismiss(animated: true, completion: nil)
|
|
||||||
hide()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user