2014-06-17 62 views
10

Uygulama içi e-postayı uygulamak için hızlıca kullanmak istiyorum. Düğmeyi tıkladığımda, e-posta penceresi açılır. Ancak e-postamı gönderemiyorum. Ayrıca, iptal silme taslağını tıkladıktan sonra, orijinal ekrana geri dönemem. Eğer myMail arasında mailComposeDelegate olarak mevcut görünüm denetleyicisi ayarlanmamış beriUygulama içi e-postayı uygulamak için Swift'i kullanma başarısız oldu

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

"All_about_tax.pdf" adlı bir örnek PDF sürükleyip bırakmak gerekir bu örneği test etmek. – rmaddy

cevap

10

, mailComposeController:didFinishWithResult yöntem çağrılan değildir. Eğer myMail init sonra emin eklemeyi kolaylaştırır:

myMail.mailComposeDelegate = self 

ve olmayan bir MFMailCompose seçenek arayan durumda herkes yılında

+0

Bu işe yarıyor! Çok teşekkür ederim! Ancak "gönder" i tıkladıktan sonra e-posta gönderilmiyor gibi görünüyor. E-postamı nasıl gönderebilirim? –

+0

Bunu cihazda veya simülatörde mi yapıyorsunuz? –

+0

Oh Ben bunu simülatörde yapıyorum ki farkındayım ... Çok teşekkür ederim! –

1

gitmek için iyi olacak, burada ben Gmail'in SMTP sunucuları kullanarak göndermek için yaptığı şey .

  1. İndir bir bu repo fermuar: https://github.com/jetseven/skpsmtpmessage
  2. Drag ve XCode projeye SMTPLibrary altında dosyalar açılır
  3. yeni başlık dosyası oluşturun - MyApp-Briding-Header.h
  4. bu yeni başlık dosyasını değiştirin:
#import "Base64Transcoder.h" 
#import "HSK_CFUtilities.h" 
#import "NSData+Base64Additions.h" 
#import "NSStream+SKPSMTPExtensions.h" 
#import "SKPSMTPMessage.h" 
  • Git Ayarları İnşa/(solda Hedefler> Uygulamam) Project
      /Swift Derleyici - Kod Üretimi
    1. Objective-C Briding Header altında dosyayı başlık yolunu ekleyin ->Debug (yani MyApp/MyApp-Bridging-Header.h
    2. Project/Build Phases/Derleme Kaynaklarına Git
    3. Tüm .m dosyalarını seçin ve Enter'a tıklayın. -fno-objc-arc yazın ve enter tuşuna basın.

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

    Birini yardımcı Umut:

  • kullanın bu kod e-posta göndermek. MFMailCompose seçeneğini kullanmak istemedim çünkü kullanıcıyı sormak istemedim.

  • 0

    Bu, e-postamı, ekli PDF dosyası belgesini kullanarak oluşturduğum yöntemdir.

    Sadece Sen `mailComposeDelegate` atmadı

    @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) 
        }