Je veux utiliser swift pour implémenter le courrier électronique embedded à l'application. Quand je clique sur le button, la window d'email apparaît. Cependant, je ne peux pas envoyer mon email. De plus, après avoir cliqué sur annuler-supprimer le brouillon, je ne peux pas revenir à l'écran d'origine.
import UIkit import MessageUI class Information : UIViewController, MFMailComposeViewControllerDelegate{ var myMail: MFMailComposeViewController! @IBAction func sendReport(sender : AnyObject) { if(MFMailComposeViewController.canSendMail()){ myMail = MFMailComposeViewController() //myMail.mailComposeDelegate // set the subject myMail.setSubject("My report") //To recipients var toRecipients = ["[email protected]"] myMail.setToRecipients(toRecipients) //CC recipients var ccRecipients = ["[email protected]"] myMail.setCcRecipients(ccRecipients) //CC recipients var bccRecipients = ["[email protected]"] myMail.setBccRecipients(ccRecipients) //Add some text to the message body var sentfrom = "Email sent from my app" myMail.setMessageBody(sentfrom, isHTML: true) //Include an attachment var image = UIImage(named: "Gimme.png") var imageData = UIImageJPEGRepresentation(image, 1.0) myMail.addAttachmentData(imageData, mimeType: "image/jped", fileName: "image") //Display the view controller self.presentViewController(myMail, animated: true, completion: nil) } else{ var alert = UIAlertController(title: "Alert", message: "Your device cannot send emails", preferredStyle: UIAlertControllerStyle.Alert) alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) self.presentViewController(alert, animated: true, completion: nil) } } func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!){ switch(result.value){ case MFMailComposeResultSent.value: println("Email sent") default: println("Whoops") } self.dismissViewControllerAnimated(true, completion: nil) } }
Comme vous n'avez pas défini le controller de vue actuel comme mailComposeDelegate
de myMail
, la méthode mailComposeController:didFinishWithResult
n'est pas appelée. Après avoir init myMail
, assurez-vous d'append:
myMail.mailComposeDelegate = self
et vous serez prêt à partir
Au cas où quelqu'un chercherait une option non MFMailCompose, voici ce que j'ai fait pour l'utiliser en utilisant les servers SMTP de Gmail.
MyApp-Briding-Header.h
#import "Base64Transcoder.h" #import "HSK_CFUtilities.h" #import "NSData+Base64Additions.h" #import "NSStream+SKPSMTPExtensions.h" #import "SKPSMTPMessage.h"
Objective-C Briding Header
-> Debug
(par exemple MyApp/MyApp-Bridging-Header.h
Sélectionnez tous les files .m et click entrer. Tapez -fno-objc-arc
et appuyez sur Entrée.
Utilisez ce code pour envoyer un email:
var mail = SKPSMTPMessage() mail.fromEmail = "[email protected]" mail.toEmail = "[email protected]" mail.requiresAuth = true mail.login = "[email protected]" mail.pass = "password" mail.subject = "test subject" mail.wantsSecure = true mail.relayHost = "smtp.gmail.com" mail.relayPorts = [587] var parts: NSDictionary = [ "kSKPSMTPPartContentTypeKey": "text/plain; charset=UTF-8", "kSKPSMTPPartMessageKey": "test message", ] mail.parts = [parts] mail.send()
J'espère que ça aide quelqu'un. Je ne voulais pas utiliser l'option MFMailCompose car je ne voulais pas avoir à inviter l'user.
C'est ainsi que j'ai composé mon email avec le document attaché au file PDF .
Juste pour tester cet exemple, vous devez glisser et déposer un exemple de file PDF nommé "All_about_tax.pdf"
@IBAction func sendEmail(sender: UIButton) { //Check to see the device can send email. if( MFMailComposeViewController.canSendMail() ) { print("Can send email.") let mailComposer = MFMailComposeViewController() mailComposer.mailComposeDelegate = self //Set to recipients mailComposer.setToRecipients(["your email id here"]) //Set the subject mailComposer.setSubject("Tax info document pdf") //set mail body mailComposer.setMessageBody("This is what they sound like.", isHTML: true) if let filePath = NSBundle.mainBundle().pathForResource("All_about_tax", ofType: "pdf") { print("File path loaded.") if let fileData = NSData(contentsOfFile: filePath) { print("File data loaded.") mailComposer.addAttachmentData(fileData, mimeType: "application/pdf", fileName: "All_about_tax.pdf") } } //this will compose and present mail to user self.presentViewController(mailComposer, animated: true, completion: nil) } else { print("email is not supported") } } func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) { self.dismissViewControllerAnimated(true, completion: nil) }