2014-06-17 7 views
10

मैं इन-ऐप ईमेल को लागू करने के लिए तेज़ी से उपयोग करना चाहता हूं। जब मैं बटन पर क्लिक करता हूं, तो ईमेल विंडो पॉप अप होती है। हालांकि, मैं अपना ईमेल भेजने में असमर्थ हूं। इसके अलावा, जब मैं रद्द-ड्राफ्ट ड्राफ्ट पर क्लिक करता हूं, तो मैं मूल स्क्रीन पर वापस नहीं जा सकता।इन-ऐप ईमेल को लागू करने के लिए स्विफ्ट का उपयोग करने में विफल

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) 

} 
} 
+0

आप सेट नहीं 'mailComposeDelegate' छोड़ने की ज़रूरत परीक्षण करने के लिए। – rmaddy

उत्तर

10

आप myMail की mailComposeDelegate के रूप में वर्तमान दृश्य नियंत्रक सेट नहीं किया है के बाद से, mailComposeController:didFinishWithResult विधि बुलाया जा रहा है नहीं है। आप myMail init के बाद, जोड़ना सुनिश्चित करें:

myMail.mailComposeDelegate = self 

और आप

+0

यह काम करता है! आपका बहुत बहुत धन्यवाद! हालांकि, जब मैं "भेजता हूं" पर क्लिक करता हूं, ऐसा लगता है कि ईमेल नहीं भेजा गया है। क्या मैं पूछ सकता हूं कि मेरा ईमेल कैसे भेजना है? –

+0

क्या आप इसे डिवाइस या सिम्युलेटर पर कर रहे हैं? –

+0

ओह मुझे एहसास है कि मैं सिम्युलेटर पर ऐसा कर रहा हूं ... बहुत बहुत धन्यवाद! –

1

जाना मामला किसी में एक गैर MFMailCompose विकल्प की तलाश में है अच्छा हो जाएगा, यहाँ क्या मैं Gmail के SMTP सर्वर का उपयोग कर भेजने के लिए किया है ।

  1. डाउनलोड इस रेपो की एक ज़िप: https://github.com/jetseven/skpsmtpmessage
  2. खींचें और अपने Xcode प्रोजेक्ट में SMTPLibrary अंतर्गत फ़ाइलें ड्रॉप
  3. एक नया हेडर फाइल बनाएं - MyApp-Briding-Header.h
  4. इस के साथ नए हेडर फाइल बदलें:
#import "Base64Transcoder.h" 
#import "HSK_CFUtilities.h" 
#import "NSData+Base64Additions.h" 
#import "NSStream+SKPSMTPExtensions.h" 
#import "SKPSMTPMessage.h" 
  1. जाओ (लक्ष्य> बाईं तरफ MyApp) परियोजना/सेटिंग्स का निर्माण करने के लिए/स्विफ्ट संकलक - कोड जनरेशन
  2. Objective-C Briding Header के तहत फ़ाइल शीर्षक के लिए पथ जोड़ें ->Debug (अर्थात MyApp/MyApp-Bridging-Header.h
  3. परियोजना/बिल्ड चरण/संकलन स्रोत
  4. सभी .m फ़ाइलों का चयन करें और दर्ज करें पर क्लिक करें। -fno-objc-arc टाइप करें और एंटर दबाएं।

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() 

आशा है कि यह किसी को मदद मिलती है:

  • इस कोड का प्रयोग करें ईमेल भेजने के लिए। मैं MFMailCompose विकल्प का उपयोग नहीं करना चाहता था क्योंकि मैं उपयोगकर्ता को संकेत नहीं देना चाहता था।

  • 0

    इस प्रकार मैंने संलग्न ईमेल पीडीएफ फ़ाइल दस्तावेज़ के साथ अपना ईमेल बनाया है।

    बस इस उदाहरण आप खींचें और एक नमूना पीडीएफ "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) 
        } 
    
    संबंधित मुद्दे

     संबंधित मुद्दे