3

在同一個視圖控制器上,我們可以發送電子郵件或短信將信息發送給朋友。 應用中的短信完全有效。但是對於電子郵件,電子郵件應用程序會在我的應用程序中打開我要求寫入的所有信息,但不可能通過推送取消來解除它,沒有任何反應。 我試過mc.mailComposeDelegate = self或mc.delegate = self,MFMailComposeViewControllerDelegate也在頂部。 我在網上查找了一切,我沒有找到任何解釋。 mailComposeController永遠不會被調用! 你有什麼想法嗎?SWIFT - 未調用mailComposeDelegate:取消MFMailComposeViewController不起作用

class inviteAFriendViewController: UIViewController, MFMessageComposeViewControllerDelegate, MFMailComposeViewControllerDelegate { 

@IBAction func emailButtonDidTouch(sender: AnyObject) { 
    sendEmail() 
} 

func sendEmail() { 
    let emailTitle = "text" 
    let messageBody = "text" 
    let toRecipents = [""] 

    let mc = MFMailComposeViewController() 

    //mc.mailComposeDelegate = self 

    mc.delegate = self 

    mc.setSubject(emailTitle) 
    mc.setMessageBody(messageBody, isHTML: false) 
    mc.setToRecipients(toRecipents) 

    presentViewController(mc, animated: true, completion: nil) 
} 

func mailComposeController(controller2: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) { 
    switch result.rawValue { 
    case MFMailComposeResultCancelled.rawValue: 
     print("Mail cancelled") 
     controller2.dismissViewControllerAnimated(true, completion: nil) 
    case MFMailComposeResultSaved.rawValue: 
     print("Mail saved") 
     controller2.dismissViewControllerAnimated(true, completion: nil) 
    case MFMailComposeResultSent.rawValue: 
     print("Mail sent") 
     controller2.dismissViewControllerAnimated(true, completion: nil) 
    case MFMailComposeResultFailed.rawValue: 
     print("Mail sent failure.") 
     controller2.dismissViewControllerAnimated(true, completion: nil) 
    default: 
     break 
    } 
    controller2.dismissViewControllerAnimated(true, completion: nil) 
} 

回答

13

我得到它的工作沒有任何問題,但我的委託方法看起來你有點不同:

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 
    } 
    self.dismissViewControllerAnimated(true, completion: nil) 
} 

,你可以嘗試這一個。

而且你需要設置 mc.mailComposeDelegate = self 而不是 mc.delegate = self

+0

哦,我的上帝,它的作品,我不知道你是誰,但感謝你1000倍!祝你今天愉快! – Oscar

2

首先使用

mc.mailComposeDelegate = self 

代替

mc.delegate = self 

此外,在情況下,斯威夫特3 ,委託方法有點upd這是:

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?){ 
    controller.dismiss(animated: true, completion: nil) 
} 
相關問題