─━ IT ━─

[iOS] MessageUI, SwiftUI and UIKit 통합

DKel 2021. 1. 11. 19:49
반응형

만약 당신이 MFMailComposeViewController 또는 MFMessageComposeViewController를 이용하고 있다면

그건 적절하지 않을 것입니다. 어렵게 코딩하게 되는데. 이러한 조언을 잘 이해한다면 어렵지 않을 것입니다.

 

MessageUI란?

MessageUI는 UIKit 환경에서 외부 사용자 인터페이스를 통합하기 위해 iOS 3.0과 함께 제공되는 시스템 프레임워크입니다.

 

MessageUI에서는 MFMailComposureViewController를 사용하여 메일을 보내고 MFMessageComposeViewController를 사용하여 메시지를 보낼 수 있습니다.그리고 각 클래스에서는 결과에 따라 행동을 취해야 할 delegate 액션이 있습니다.

 

//
//  ContentView.swift
//  MailDemo
//
//  Created by Florent Morin on 29/06/2019.
//  Copyright © 2019 Morin Innovation. All rights reserved.
//
// Medium link: https://medium.com/@florentmorin/messageui-swiftui-and-uikit-integration-82d91159b0bd
// Sample source code: https://github.com/florentmorin/SwiftUIAndMessageUI
import SwiftUI
import MessageUI

/// Main View
struct ContentView: View {

    /// The delegate required by `MFMailComposeViewController`
    private let mailComposeDelegate = MailDelegate()

    /// The delegate required by `MFMessageComposeViewController`
    private let messageComposeDelegate = MessageDelegate()


    var body: some View {
        VStack {
            Spacer()
            Button(action: {
                self.presentMailCompose()
            }) {
                Text("Send email")
            }
            Spacer()
            Button(action: {
                self.presentMessageCompose()
            }) {
                Text("Send message")
            }
            Spacer()
        }
    }

}

// MARK: The mail part
extension ContentView {

    /// Delegate for view controller as `MFMailComposeViewControllerDelegate`
    private class MailDelegate: NSObject, MFMailComposeViewControllerDelegate {

        func mailComposeController(_ controller: MFMailComposeViewController,
                                   didFinishWith result: MFMailComposeResult,
                                   error: Error?) {
            <# Customize here #>
            controller.dismiss(animated: true)
        }

    }

    /// Present an mail compose view controller modally in UIKit environment
    private func presentMailCompose() {
        guard MFMailComposeViewController.canSendMail() else {
            return
        }
        let vc = UIApplication.shared.keyWindow?.rootViewController

        let composeVC = MFMailComposeViewController()
        composeVC.mailComposeDelegate = mailComposeDelegate

        <# Customize here #>

        vc?.present(composeVC, animated: true)
    }
}

// MARK: The message part
extension ContentView {

    /// Delegate for view controller as `MFMessageComposeViewControllerDelegate`
    private class MessageDelegate: NSObject, MFMessageComposeViewControllerDelegate {
        func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
            // Customize here
            controller.dismiss(animated: true)
        }

    }

    /// Present an message compose view controller modally in UIKit environment
    private func presentMessageCompose() {
        guard MFMessageComposeViewController.canSendText() else {
            return
        }
        let vc = UIApplication.shared.keyWindow?.rootViewController

        let composeVC = MFMessageComposeViewController()
        composeVC.messageComposeDelegate = messageComposeDelegate

        <# Customize here #>

        vc?.present(composeVC, animated: true)
    }
}

#if DEBUG
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif

 

샘플 코드는 다음 링크에서 받을 수 있습니다.github.com/florentmorin/SwiftUIAndMessageUI

 

florentmorin/SwiftUIAndMessageUI

How to use SwiftUI and MessageUI together. Contribute to florentmorin/SwiftUIAndMessageUI development by creating an account on GitHub.

github.com

원문: medium.com/@florentmorin/messageui-swiftui-and-uikit-integration-82d91159b0bd

 

MessageUI, SwiftUI and UIKit integration

If you want to use MFMailComposeViewController or MFMessageComposeViewController, it looks painful. But, with these tips, it’s painless. 😃

medium.com

 

반응형