[swift] 연락처(Contacts) 정보를 이용하여 이메일 보내는 기능 추가 방법

1. 연락처 정보에 접근하기

import Contacts

let store = CNContactStore()

store.requestAccess(for: .contacts, completionHandler: { (granted, error) in
    if granted {
        // 연락처 접근 권한이 허용된 경우
    } else {
        // 연락처 접근 권한이 거부된 경우
    }
})

2. 연락처에서 이메일 주소 가져오기

if granted {
    let contact = // 사용자가 선택한 연락처
    if let email = contact.emailAddresses.first {
        let emailAddress = email.value as String
        // 이메일 주소 사용
    } else {
        // 선택한 연락처에 이메일 주소가 없는 경우 처리
    }
}

3. 이메일 보내기

이메일 주소를 얻었다면, iOS의 MessageUI 프레임워크를 사용하여 이메일을 작성하고 보낼 수 있습니다. 해당 프레임워크를 활용하여 메일 작성 및 전송 기능을 추가할 수 있습니다.

아래는 MessageUI를 활용한 기본적인 이메일 보내기 코드입니다.

import MessageUI

if MFMailComposeViewController.canSendMail() {
    let mailController = MFMailComposeViewController()
    mailController.setToRecipients([emailAddress])
    mailController.mailComposeDelegate = self
    present(mailController, animated: true, completion: nil)
}

위와 같이 연락처(Contacts) 정보를 활용하여 이메일을 보내는 기능을 추가할 수 있습니다. 이를 통해 사용자가 앱 내에서 간편하게 연락처의 이메일 주소를 활용하여 이메일을 보낼 수 있게 됩니다.