현재 피드백 버튼을 넣고 이메일로 받기위해 만들어보았다.
private func sendFeedbackEmail() {
guard MFMailComposeViewController.canSendMail() else {
// 이메일 기능이 사용 불가능할 때
let alert = UIAlertController(
title: "Email Not Available",
message: "Please configure your email account to send feedback.",
preferredStyle: .alert
)
alert.addAction(UIAlertAction(title: "OK", style: .default) { _ in
// 첫 화면으로 돌아가기
self.navigationController?.popToRootViewController(animated: true)
})
present(alert, animated: true, completion: nil)
return
}
let mailComposeVC = MFMailComposeViewController()
mailComposeVC.mailComposeDelegate = self
mailComposeVC.setToRecipients(["yourEmail@naver.com"])
mailComposeVC.setSubject("Feedback on Workout App")
mailComposeVC.setMessageBody("Please provide your feedback here.", isHTML: false)
present(mailComposeVC, animated: true, completion: nil)
}
// MFMailComposeViewControllerDelegate 메소드
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true) {
if let error = error {
// 이메일 전송 실패 시 처리
let alert = UIAlertController(
title: "Error",
message: "Failed to send email. Please try again. Error: \(error.localizedDescription)",
preferredStyle: .alert
)
alert.addAction(UIAlertAction(title: "OK", style: .default))
self.present(alert, animated: true, completion: nil)
} else {
switch result {
case .sent:
// 이메일 전송 성공 시 감사 메시지 표시
let alert = UIAlertController(
title: "Thank You!",
message: "Your feedback has been sent successfully. We appreciate your input!",
preferredStyle: .alert
)
alert.addAction(UIAlertAction(title: "Complete", style: .default, handler: { _ in
// 알림 창에서 완료를 누르면 메인 화면으로 돌아갑니다.
self.navigationController?.popToRootViewController(animated: true)
}))
self.present(alert, animated: true, completion: nil)
case .saved:
// 이메일 저장 시 알림
let alert = UIAlertController(
title: "Draft Saved",
message: "Your email draft has been saved.",
preferredStyle: .alert
)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
case .failed:
// 이메일 전송 실패 시 알림
let alert = UIAlertController(
title: "Send Failed",
message: "Failed to send your feedback. Please try again.",
preferredStyle: .alert
)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
case .cancelled:
// 이메일 전송 취소 시 알림 (필요에 따라 생략 가능)
break
@unknown default:
break
}
}
}
}
요렇게 해서 알럿창이 뛰어지도록 만들었다.
앞으로 피드백 버튼을 만들고 이메일로 받을때 알럿창으로 문구를 띄울때 잘 사용해야겠다
하자자