티스토리 뷰

728x90
반응형

내용

  • ✅ 아래와 같이 인앱에서 mail 을 연결해보자.
  • 🔥 기기 종류 / iOS 버전 / App 버전 을 가져와보자.
// 🔥 App Version.
guard let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String else { return }

// ✅ mail 을 연동해서 보낼 수 있는가를 체크.
if MFMailComposeViewController.canSendMail() {
    let mailComposeVC = MFMailComposeViewController()
    mailComposeVC.mailComposeDelegate = self

    mailComposeVC.setToRecipients(["teamsparker66@gmail.com"])
    mailComposeVC.setSubject("스파크 문의 사항")
    mailComposeVC.setMessageBody("문의 사항을 상세히 입력해주세요.",
                                             isHTML: false)
    mailComposeVC.modalPresentationStyle = .overFullScreen
    present(mailComposeVC, animated: true, completion: nil)
} else {
    // ✅ mail 이 계정과 연동되지 않은 경우.
        let mailErrorAlert = UIAlertController(title: "메일 전송 실패", message: "이메일 설정을 확인하고 다시 시도해주세요.", preferredStyle: .alert)
        let confirmAction = UIAlertAction(title: "확인", style: .default) { _ in }
        mailErrorAlert.addAction(confirmAction)
        present(mailErrorAlert, animated: true, completion: nil)
}

// MARK: - MFMailComposeViewControllerDelegate
extension MypageVC: MFMailComposeViewControllerDelegate {
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        // ✅ MFMailComposeResult 가지고 메일 작성 인터페이스가 닫힐때의 결과에 대응할 수있다.
        switch result {
        case .cancelled:
// ✅ The user canceled the operation.
            print("cancelled")
        case .saved:
// ✅ The email message was saved in the user’s drafts folder.
            print("saved")
        case .sent:
// ✅ The email message was queued in the user’s outbox.
            print("sent")
        case .failed:
// ✅ The email message was not saved or queued, possibly due to an error.
            print("failed")
        }
// ✅ Dismiss the mail compose view controller.
        controller.dismiss(animated: true, completion: nil)
    }
}

// MARK: - UIDevice Extension.
extension UIDevice {
// 🔥 iOS Version
    static let iOSVersion = "\(current.systemName) \(current.systemVersion)"

// 🔥 iPhone Model
    private static var hardwareString: String {
        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let model = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }
        return model
    }

    /// Referenced the following URL.
    /// [List of iOS and iPadOS devices](https://en.wikipedia.org/wiki/List_of_iOS_and_iPadOS_devices)
    private static var modelDictionary: [String: String] {
        return [
            "i386": "Simulator",   // 32 bit
            "x86_64": "Simulator", // 64 bit
            "iPhone8,1": "iPhone 6S",
            "iPhone8,2": "iPhone 6S Plus",
            "iPhone8,4": "iPhone SE 1st generation",
            "iPhone9,1": "iPhone 7",
            "iPhone9,3": "iPhone 7",
            "iPhone9,2": "iPhone 7 Plus",
            "iPhone9,4": "iPhone 7 Plus",
            "iPhone10,1": "iPhone 8",
            "iPhone10,4": "iPhone 8",
            "iPhone10,2": "iPhone 8 Plus",
            "iPhone10,5": "iPhone 8 Plus",
            "iPhone10,3": "iPhone X",
            "iPhone10,6": "iPhone X",
            "iPhone11,2": "iPhone XS",
            "iPhone11,4": "iPhone XS Max",
            "iPhone11,6": "iPhone XS Max",
            "iPhone11,8": "iPhone XR",
            "iPhone12,1": "iPhone 11",
            "iPhone12,3": "iPhone 11 Pro",
            "iPhone12,5": "iPhone 11 Pro Max",
            "iPhone12,8": "iPhone SE 2nd generation",
            "iPhone13,1": "iPhone 12 Mini",
            "iPhone13,2": "iPhone 12",
            "iPhone13,3": "iPhone 12 Pro",
            "iPhone13,4": "iPhone 12 Pro Max",
            "iPhone14,4": "iPhone 13 Mini",
            "iPhone14,5": "iPhone 13",
            "iPhone14,2": "iPhone 13 Pro",
            "iPhone14,3": "iPhone 13 Pro Max",
            "iPhone14,6": "iPhone SE 3nd generation"
        ]
    }

    static var iPhoneModel: String {
        return modelDictionary[hardwareString] ?? "Unknown iPhone - \(hardwareString)"
    }
}

출처:

[[iOS] Swift를 이용해서 iOS 디바이스에서 Email 보내기 📧](https://borabong.tistory.com/6)

Apple Developer Documentation

[[iOS] UIDevice](https://jinnify.tistory.com/35)

List of iOS and iPadOS devices - Wikipedia

728x90
반응형
댓글
최근에 올라온 글
최근에 달린 댓글
글 보관함
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
링크
Total
Today
Yesterday