MessageUI는 메시지를 작성할 수 있는 UIViewController를 앱을 떠나지 않고, 띄워줌

문자 보내기가 완료되었는지 확인 가능

MFMessagingComposeViewController 사용

SwiftUI에서 쓸 수 있게 MessageComposer 정의하기

struct MessageComposer: UIViewControllerRepresentable {
  
  typealias UIViewControllerType = MFMessageComposeViewController
  
  @Binding var recipients: [String]
  var body: String
  var completion: (MessageComposeResult) -> Void
  
  func makeUIViewController(context: Context) -> MFMessageComposeViewController {
    let composeVC = MFMessageComposeViewController()
    composeVC.messageComposeDelegate = context.coordinator
    composeVC.body = body
    composeVC.recipients = recipients
    
    return composeVC
  }
  
  func updateUIViewController(_ uiViewController: MFMessageComposeViewController, context: Context) {
    // 
  }
  
  func makeCoordinator() -> MessageComposer.Coordinator {
    Coordinator(completion: completion)
  }
  
  class Coordinator: NSObject, MFMessageComposeViewControllerDelegate {
    
    var completion: (MessageComposeResult) -> Void
    
    init(completion: @escaping (MessageComposeResult) -> Void) {
      self.completion = completion
    }
    
    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
      completion(result)
      controller.dismiss(animated: true)
    }
  }
}

SwiftUI에서 사용

.popover(isPresented: $showMessageComposer) {
  MessageComposer(recipients: $smsRecipient, body: vm.smsSendText()) { result in
    switch result {
    case .cancelled:
      break
    case .failed:
      break
    case .sent:
      print("message sent")
    @unknown default:
      break
    }
  }
}

참조

https://stackoverflow.com/questions/26350220/sending-sms-in-ios-with-swift

https://developer.apple.com/documentation/messageui/messagecomposeresult?changes=_4

https://www.twilio.com/blog/2018/07/sending-text-messages-from-your-ios-app-in-swift-using-mfmessagecomposeviewcontroller.html