1 頁 (共 1 頁)

send email in-app

發表於 : 週四 3月 24, 2016 3:53 pm
rusli
in-app.zip
(25.55 KiB) 已下載 28 次


代碼: 選擇全部

extension UIAlertController
{
    func AutoDismiss()
    {
        // Delay the dismissal by 2 seconds
        let delay = 2.0 * Double(NSEC_PER_SEC)
        let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
        dispatch_after(time, dispatch_get_main_queue(), {
            self.dismissViewControllerAnimated(true, completion: nil)
        })
    }
}


代碼: 選擇全部


extension UIViewController
{
    func ShowDismissMessage(title: String, message: String)
    {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
        self.presentViewController(alertController, animated: true, completion: nil)
       
        alertController.AutoDismiss()
    }
}



代碼: 選擇全部

import UIKit
import MessageUI
   
class ViewController: UIViewController, MFMailComposeViewControllerDelegate
{
    override func viewDidLoad() {
        super.viewDidLoad()

        self.sendMail()
    }
   
    func sendMail()
    {
        //Check to see the device can send email.
        if( MFMailComposeViewController.canSendMail() ) {
            print("Can send email.")
           
            let mailComposer = MFMailComposeViewController()
            mailComposer.mailComposeDelegate = self
           
            mailComposer.setToRecipients(["A@mail.com"])
           
            //Set the subject and message of the email
            mailComposer.setSubject("Hi!!")
            // mailComposer.setMessageBody(builder.htmlBody, isHTML: true)
            mailComposer.setMessageBody("<b> hi </b>", isHTML: true)
           
            let data = "hello in pdf".dataUsingEncoding(NSUTF8StringEncoding)
           
            mailComposer.addAttachmentData(data!, mimeType: "application/pdf", fileName: "A.pdf")
           
            self.presentViewController(mailComposer, animated: true, completion: nil)
        }
        else
        {
            self.ShowDismissMessage("Alert", message: "Your device cannot send emails")
        }

    }

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