2017-03-11 68 views
-1

我想從我的應用程序發送郵件。我正在做Swift的第一步,而且我堅持了一點。我想按下按鈕並直接發送電子郵件。我想從我的TextField獲取Subject和MessageBody的數據。包括來自我的TextField的發件人數據(請查看截圖) 這是怎麼回事? 這是我的代碼:直接從應用程序發送電子郵件 - Swift

@IBAction func sendEmailButtonTapped(sender: AnyObject) { 
    let mailComposeViewController = configuredMailComposeViewController() 
    if MFMailComposeViewController.canSendMail() { 
     self.present(mailComposeViewController, animated: true, completion: nil) 
    } else { 
     self.showSendMailErrorAlert() 
    } 
} 

func configuredMailComposeViewController() -> MFMailComposeViewController { 
    let mailComposerVC = MFMailComposeViewController() 
    mailComposerVC.mailComposeDelegate = self // Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property 

    mailComposerVC.setToRecipients(["[email protected]"]) 
    mailComposerVC.setSubject("Sending you an in-app e-mail...") 
    mailComposerVC.setMessageBody("Sending e-mail in-app is not so bad!", isHTML: false) 

    return mailComposerVC 
} 

func showSendMailErrorAlert() { 
    let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send e-mail. Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "OK") 
    sendMailErrorAlert.show() 
} 

// MARK: MFMailComposeViewControllerDelegate Method 
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { 
    controller.dismiss(animated: true, completion: nil) 
} 

enter image description here

+0

您的第一步不包括運行搜索嗎?屏幕截圖在哪裏?你的問題是什麼? –

+0

@ElTomato搜索什麼?我添加了一個問題 –

+0

類似主題...你不是第一個問這個問題的人。甚至沒有第十... –

回答

1

你是說你要發送的電子郵件在幕後,而無需用戶看到的郵件視圖控制器來呢?如果這就是你想要的,你不能從你的(前端)代碼中完成。 Apple不希望應用程序能夠以自動方式發送電子郵件,而無需用戶點擊發送按鈕。如果你想,你可以編程功能從應用程序的服務器端發送電子郵件。

+0

我希望用戶留在應用程序,即使按下按鈕後SEND –

+0

當電子郵件視圖控制器啓動時,用戶仍然在您的應用程序,即使電子郵件視圖控制器看起來與iPhone附帶的電子郵件應用程序完全相同。 – ClayJ

+0

我可以從我的TextField中爲主題和MessageBody獲取電子郵件的數據嗎? –

相關問題