
앱 안에서 메일을 보내는 방법은 다음과 같아요!!
canSendMail을 통해, 메일을 보낼 수 있는 상태인지를 확인합니다.
만약에!! Mail 앱이 안깔려있거나 하면 메일을 못보내겠죠?? 그럼 canSendMail이 False로 들어가서 메일 전송 실패를 내뱉습니다.
MFMailComposeViewController 은 이미 그 자체로 메일을 보내는 틀을 완성해 놓고 있어서,
우리는 메일 보낼 주소, 제목, 내용 정도만 미리 적어놓고,
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?)
이 함수를 통해 메일 보내는걸 성공하고 나면 뭘 할건지!! 만 봐주면 됩니다ㅎㅎ
전체 코드는 다음과 같아요!
if MFMailComposeViewController.canSendMail() {
let compseVC = MFMailComposeViewController()
compseVC.mailComposeDelegate = self
compseVC.setToRecipients(["hasensprung42@gmail.com"])
compseVC.setSubject("[Canvas] ")
compseVC.setMessageBody("", isHTML: false)
self.present(compseVC, animated: true, completion: nil)
} else {
self.showSendMailErrorAlert()
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
func showSendMailErrorAlert() {
let sendMailErrorAlert = UIAlertController(title: "메일을 전송 실패", message: "mail앱을 확인하고 다시 시도해주세요.", preferredStyle: .alert)
let confirmAction = UIAlertAction(title: "확인", style: .default) {
(action) in
}
sendMailErrorAlert.addAction(confirmAction)
self.present(sendMailErrorAlert, animated: true, completion: nil)
}
'iOS 개발 > UIKit' 카테고리의 다른 글
[UITableView] SwipeAction - cell 삭제 (확인 Alert와 함께) (0) | 2021.11.29 |
---|---|
[UITextView] 줄 수, 글자 수 제한 (0) | 2021.11.22 |
[UIView] 정말 예쁜 blur Effect (0) | 2021.11.21 |
[Swift] App 첫 로딩을 감지하는 법 (first loading detection) (0) | 2021.11.20 |
[GestureRecognizer] Gesture 잘 사용하기 - 동시에 여러 gesture 사용하기, 주의점 (0) | 2021.11.17 |
[UICollectionView] View를 하나씩 넘기기, 여러개씩 넘기기 (0) | 2021.11.17 |
[UIImageView] image 색 변경하기 (0) | 2021.11.09 |
[UITableVIew] automaticDimension 이 되지 않을때 (0) | 2021.10.18 |