Failed on using Swift to implement in-app email

前端 未结 3 2118
走了就别回头了
走了就别回头了 2021-02-14 08:52

I want to use swift to implement in-app email. When I click the button, the email window pops up. However, I am unable to send my email. Moreover, after I click cancel-delete dr

3条回答
  •  庸人自扰
    2021-02-14 09:42

    This is how I have composed my email with attached PDF file document.

    Just to test this example you need to drag and drop a sample PDF named "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)
        }
    

提交回复
热议问题