[Swift] mailto와 MFMailComposeViewController
2020. 4. 8. 13:25ㆍiOS/Swift
일하다가 mailto와 MFMail~의 차이점이 뭔지 궁금해져서 찾아봤다.
mailto는 메일 앱을 통해메일을 보내는 것이고,
MFMailComposeViewController는앱 내에서메일을 보내는 것이다.
메일 계정(설정 - 암호 및 계정)이 없을 경우 MFMailComposeViewController에서 false를 반환한다.
class mailTest: MFMailComposeViewControllerDelegate {
func sendMail() {
let email = "ksmini0212@gmail.com"
let subject = "Hello."
let bodyText = "It's me"
if MFMailComposeViewController.canSendMail() {
// 메일 계정이 있을 경우
let composeVC = MFMailComposeViewController()
composeVC.delegate = self as? UINavigationControllerDelegate
composeVC.setToRecipients([email])
composeVC.setMessageBody(bodyText, isHTML: false)
self.present(composeVC, animated: true, completion: nil)
} else {
let coded = "mailto:\(email)?subject=\(subject)&body=\(bodyText)"
.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
if let url = URL(string: coded!) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
}
}
계정이 없다면 메일 앱으로 이동하여 메일을 등록하라는 화면이 뜬다.
+) iOS12는 오버팝 후 > 누르면 파일을 첨부할 수 있음
iOS13은 하단 액세서리 뷰로 파일 첨부 가능
'iOS > Swift' 카테고리의 다른 글
[Swift] Check Appstore receipts validation (1) | 2021.02.19 |
---|---|
[Swift] 배열 index 조회 및 삭제하기 (0) | 2020.08.13 |
[Swift] 센트리(Sentry)를 사용해보자! (0) | 2020.04.06 |
[Swift] textField / textView 끝의 공백 없애기 (0) | 2019.03.19 |
[Swift] 한글 종성받침 유무에 따라 '을/를' 반환하기 (0) | 2018.12.13 |