MFMailComposeViewController in Swift

前端 未结 5 742
没有蜡笔的小新
没有蜡笔的小新 2021-02-03 21:36

This is sample code:

import UIKit
import MessageUI

class ViewController: UIViewController, MFMailComposeViewControllerDelegate {

    @IBAction func showEmail(s         


        
相关标签:
5条回答
  • 2021-02-03 21:55

    In addition to using .value, don't forget to add to break to each case statement, see Apple's official version: https://developer.apple.com/library/iOS/samplecode/MessageComposer/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010161

    0 讨论(0)
  • 2021-02-03 21:58

    In swift 3, you can use this clear code:

     @IBAction func sendMail(_ sender: Any) {
    
            print(MFMailComposeViewController.canSendMail())
            if MFMailComposeViewController.canSendMail() {
                let mail = MFMailComposeViewController()
                mail.mailComposeDelegate = self
                mail.setToRecipients(["test@test.com"])
                mail.setMessageBody("<p>This is test Mail!</p>", isHTML: true)
    
                present(mail, animated: true)
            } else {
                 let email = "test@test.com"
                 if let url = URL(string: "mailto:\(email)") {
                 UIApplication.shared.open(url)
                 }
    
            }
    
    
        }
    
        func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
            controller.dismiss(animated: true)
            switch result {
            case .cancelled:
                print("Mail cancelled")
            case .saved:
                print("Mail saved")
            case .sent:
                self.allertInfo(_title: "Mail Info", _message: "Mail is sent successfuly", _actionTitle: "OK")
                print("Mail sent")
            case .failed:
                self.allertInfo(_title: "Mail Info", _message: "Mail isn't sent.",
    _actionTitle: "OK")
                print("Mail sent failure: \(error?.localizedDescription)")
            default:
                break
            }
    
        }
    
        func allertInfo(_title:String, _message:String, _actionTitle:String) {
    
            let alert = UIAlertController(title: _title, message: _message, preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: _actionTitle, style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
    
        }
    
    0 讨论(0)
  • 2021-02-03 22:11

    I compared MFMailComposeResult documentation on both Xcode 5 and Xcode 6. In Swift, MFMailComposeResult is a struct

    struct MFMailComposeResult {
        init(_ value: CUnsignedInt) // available in iPhone 3.0
        var value: CUnsignedInt
    }
    

    with MFMailComposeResultCancelled as a constant of type MFMailComposeResult:

    var MFMailComposeResultCancelled: MFMailComposeResult { get }
    

    while it's an enum in Objective-C:

     enum MFMailComposeResult {
        MFMailComposeResultCancelled,
        MFMailComposeResultSaved,
        MFMailComposeResultSent,
        MFMailComposeResultFailed
    };
    typedef enum MFMailComposeResult MFMailComposeResult;   // available in iPhone 3.0
    

    In order to make your code work, you will have to compare their values which are CUnsignedInt.

    So you will have to type the following code:

    func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) {
        switch result.value {
        case MFMailComposeResultCancelled.value:
            println("Mail cancelled")
        case MFMailComposeResultSaved.value:
            println("Mail saved")
        case MFMailComposeResultSent.value:
            println("Mail sent")
        case MFMailComposeResultFailed.value:
            println("Mail sent failure: \(error.localizedDescription)")
        default:
            break
        }
        self.dismissViewControllerAnimated(false, completion: nil)
    }
    
    0 讨论(0)
  • 2021-02-03 22:18

    in Swift 2.0 do this:

    func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
            switch result.rawValue {
            case MFMailComposeResultCancelled.rawValue:
                print("Mail cancelled")
            case MFMailComposeResultSaved.rawValue:
                print("Mail saved")
            case MFMailComposeResultSent.rawValue:
                print("Mail sent")
            case MFMailComposeResultFailed.rawValue:
                print("Mail sent failure: \(error!.localizedDescription)")
            default:
                break
            }
            controller.dismissViewControllerAnimated(true, completion: nil)
        }
    
    0 讨论(0)
  • 2021-02-03 22:19

    In swift 3.0 -> Syntax Change

    func mailComposeController(controller: MFMailComposeViewController,
                               didFinishWithResult result: MFMailComposeResult, error: NSError?) {
    
        switch result.rawValue {
        case MFMailComposeResult.Cancelled.rawValue:
            print("Mail cancelled")
        case MFMailComposeResult.Saved.rawValue:
            print("Mail saved")
        case MFMailComposeResult.Sent.rawValue:
            print("Mail sent")
        case MFMailComposeResult.Failed.rawValue:
            print("Mail sent failure: %@", [error!.localizedDescription])
        default:
            break
        }
        // Dismiss the mail compose view controller.
        controller.dismissViewControllerAnimated(true, completion: nil)
    }
    
    0 讨论(0)
提交回复
热议问题