[swift] 파일을 이메일로 전송 또는 공유하는 방법

먼저, 파일을 이메일로 전송하려면 MFMailComposeViewController를 사용하여 이메일 작성 인터페이스를 표시하고 파일을 첨부해야 합니다.

다음은 이메일로 파일을 전송하는 예시 코드입니다:

import MessageUI

// ...

func sendEmailWithAttachment(at filePath: URL, presentingViewController: UIViewController) {
    if MFMailComposeViewController.canSendMail() {
        let emailController = MFMailComposeViewController()
        emailController.setSubject("File Attachment")
        emailController.addAttachmentData(try! Data(contentsOf: filePath), mimeType: "application/octet-stream", fileName: filePath.lastPathComponent)
        emailController.mailComposeDelegate = presentingViewController
        presentingViewController.present(emailController, animated: true, completion: nil)
    } else {
        // Handle the case where the device is unable to send email
    }
}

또한, 파일을 다른 앱과 공유하려면 UIActivityViewController를 사용하여 파일을 다른 앱으로 전송할 수 있습니다.

다음은 파일을 다른 앱과 공유하는 예시 코드입니다:

// ...

func shareFile(at filePath: URL, presentingViewController: UIViewController) {
    let activityViewController = UIActivityViewController(activityItems: [filePath], applicationActivities: nil)
    activityViewController.popoverPresentationController?.sourceView = presentingViewController.view
    presentingViewController.present(activityViewController, animated: true, completion: nil)
}

이제 이메일로 파일을 전송하거나 공유하는 기능을 구현할 수 있을 것입니다.