2015-11-13 62 views
5

你好,我想從我的應用程序打開電子郵件程序,身體應該已經定義。我可以打開電子郵件,但不知道如何將電子郵件正文定義爲給定參數以顯示給定的標準文本。任何人都可以幫忙使用MFMailComposeViewController從應用程序打開電子郵件與iOS中的預定義文本

//EMAIL 
let email = "[email protected]" 
let urlEMail = NSURL(string: "mailto:\(email)") 

if UIApplication.sharedApplication().canOpenURL(urlEMail!) { 
       UIApplication.sharedApplication().openURL(urlEMail!) 
} else { 
print("Ups") 
} 
+0

請在'郵寄地址一些研究:'URL方案。您可以提供「收件人」地址,「抄送」地址,主題和郵件正文。但當然最好的選擇是做下面的答案。 – rmaddy

回答

12

你可以這樣做::繼承人我使用的代碼,打開電子郵件

let mailComposerVC = MFMailComposeViewController() 
mailComposerVC.mailComposeDelegate = self 
mailComposerVC.setToRecipients(["[email protected]"]) 
mailComposerVC.setSubject("Subject") 
mailComposerVC.setMessageBody("Body", isHTML: false) 
self.presentViewController(mailComposerVC, animated: true, completion: nil) 

此外,您還需要從MFMailComposeViewControllerDelegate實現mailComposeController:didFinishWithResult:error:,你應該解僱MFMailComposeViewController

+3

在你創建你的mailComposerVC之前,檢查你是否真的可以使用'MFMailComposeViewController.canSendMail()'發送一封電子郵件並不是一個壞主意' –

4

使用MFMailComposeViewController是這樣的:

  1. 導入MessageUI

    import MessageUI 
    
  2. 委託添加到您的類:

    class myClass: UIViewController, MFMailComposeViewControllerDelegate {} 
    
  3. 配置電子郵件預設你想有

    let mail = MFMailComposeViewController() 
    mail.mailComposeDelegate = self 
    mail.setSubject("Subject") 
    mail.setMessageBody("Body", isHTML: true) 
    mail.setToRecipients(["[email protected]"]) 
    presentViewController(mail, animated: true, completion: nil) 
    
  4. 將這個方法在你的代碼:

    func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) { 
        dismissViewControllerAnimated(true, completion: nil) 
    } 
    

現在你去了,現在工作。

+2

不要忘記檢查:如果MFMailComposeViewController.canSendMail(){... – djdance

0

我建議使用Apple的方式,您可以在官方文檔中找到有關MFMailComposeViewController的信息。它會打開一個帶有電子郵件的模式視圖控制器,發送後會被解僱。因此用戶留在您的應用程序。

+0

OP問Swift代碼,您的參考將顯示Obj-C。 – LinusGeffarth

+1

鏈接導致蘋果的文檔,其中可能需要Jan可能需要的所有信息,而不僅僅是特定的部分(方法/屬性)。在這種情況下,Apple僅在ObjC中提供了示例代碼,但它可以很容易地轉換爲Swift。 –

11

如果你想開,而不是顯示MFMailComposeViewController由他人已經提到了內置電子郵件應用程序,你可以構建一個mailto:鏈接是這樣的:

let subject = "My subject" 
let body = "The awesome body of my email." 
let encodedParams = "subject=\(subject)&body=\(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet()) 
let url = "mailto:[email protected]?\(encodedParams)" 

if let emailURL = NSURL(url) { 
    if UIApplication.sharedApplication().canOpenURL(emailURL) { 
     UIApplication.sharedApplication().openURL(emailURL) 
    } 
} 

剛拯救任何人打字,2016年的語法已略有改變:

let subject = "Some subject" 
let body = "Plenty of email body." 
let coded = "mailto:[email protected]?subject=\(subject)&body=\(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet()) 

if let emailURL:NSURL = NSURL(string: coded!) 
    { 
    if UIApplication.sharedApplication().canOpenURL(emailURL) 
     { 
     UIApplication.sharedApplication().openURL(emailURL) 
     } 
+0

頁面上問題的實際正確答案!謝啦! :) – Fattie

11

斯威夫特3版

let subject = "Some subject" 
let body = "Plenty of email body." 
let coded = "mailto:[email protected]?subject=\(subject)&body=\(body)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) 

if let emailURL: NSURL = NSURL(string: coded!) { 
    if UIApplication.shared.canOpenURL(emailURL as URL) { 
     UIApplication.shared.openURL(emailURL as URL) 
    } 
} 
0

雨燕4.0

let email = "[email protected]" 
    let subject = "subject" 
    let bodyText = "Please provide information that will help us to serve you better" 
    if MFMailComposeViewController.canSendMail() { 
     let mailComposerVC = MFMailComposeViewController() 
     mailComposerVC.mailComposeDelegate = self 
     mailComposerVC.setToRecipients([email]) 
     mailComposerVC.setSubject(subject) 
     mailComposerVC.setMessageBody(bodyText, isHTML: true) 
     self.present(mailComposerVC, animated: true, completion: nil) 
    } else { 
     let coded = "mailto:\(email)?subject=\(subject)&body=\(bodyText)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) 
     if let emailURL = URL(string: coded!) 
     { 
      if UIApplication.shared.canOpenURL(emailURL) 
      { 
       UIApplication.shared.open(emailURL, options: [:], completionHandler: { (result) in 
        if !result { 
         // show some Toast or error alert 
         //("Your device is not currently configured to send mail.") 
        } 
       }) 
      } 
     } 
    } 
相關問題