2016-11-30 24 views
29

Gönderme e-posta seçeneği ile bir uygulama kurmaya çalışıyorum. Hızlı e-posta ile gönderme 3

import Foundation 
import MessageUI 
import UIKit 

class emailClass: UIViewController, MFMailComposeViewControllerDelegate { 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     if !MFMailComposeViewController.canSendMail() { 
      print("Mail services are not available") 
      return 
     }   
     sendEmail() 
    } 

    func sendEmail() {  
     let composeVC = MFMailComposeViewController() 
     composeVC.mailComposeDelegate = self 
     // Configure the fields of the interface. 
     composeVC.setToRecipients(["[email protected]"]) 
     composeVC.setSubject("Hello!") 
     composeVC.setMessageBody("Hello this is my message body!", isHTML: false) 
     // Present the view controller modally. 
     self.present(composeVC, animated: true, completion: nil) 
    } 

    func mailComposeController(controller: MFMailComposeViewController, 
          didFinishWithResult result: MFMailComposeResult, error: NSError?) { 
     // Check the result or perform other tasks. 
     // Dismiss the mail compose view controller. 
     controller.dismiss(animated: true, completion: nil) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 

Yani bu mesajı alıyorum:: "Posta hizmetleri mevcut değildir"

Bu kodu vardır. Şimdi iCloud'da simülatör cihazına giriş yaptım ... Bu yüzden yapmalıyım ama yapmamalı. Neden bu çalışmıyor? Bana neyin yanlış olduğunu söyleyebilir misin ve nasıl ilerleyebilirim?

+0

bir e-posta adresi ile cihazınızı yapılandırmak mı

The mail compose view controller is not dismissed automatically. When the user taps the buttons to send the email or cancel the interface, the mail compose view controller calls the mailComposeController(_:didFinishWith:error:) method of its delegate. Your implementation of that method must dismiss the view controller explicitly, as shown in Listing 3. You can also use this method to check the result of the operation.

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { // Check the result or perform other tasks. // Dismiss the mail compose view controller. controller.dismiss(animated: true, completion: nil) } } 

Kaynak diyor? hayırsa, o zaman yapın ... ve sorununuzu çözebilir. –

+0

Simülatörü kullanıyorum. Cihazı e-posta adresiyle nasıl yapılandırabilirim? ayarlara gittiğimde seçenekler göremiyorum "email" .... –

+0

Ben bir şey, bu sorunu çözdünüz .. Bu arada bir iOS cihaz için bir e-posta yapılandırmak için bağlantı ... https: // support. apple.com/en-in/HT201320 –

cevap

29

Simülatör ile çalışmayacaktır. Lütfen iPhone cihazında test edin. Sen app

MFMailComposeViewController.canSendMail() // returns false for simulators. 

Sen simülatörde test edemez gerçek bir cihazda çalışıyorsa Apple Developer Portal - MFMailComposeViewController

+0

Evet. % 100 eminim.Aynı türde bir uygulama üzerinde çalışıyorum ve bunu buldum. –

+0

Tamam teşekkürler :) ama yukarıda yazdığım kod. Tamam mı? –

+0

Evet. Kod iyi görünüyor ve çalışmalı. IPhone'unuz yok mu? –

4

Kod iyi görünüyor ve gayet iyi çalışıyor başvurabilir, Temel şeyleri test etmek mümkün olacak UI gibi, İptal/Gönder düğmesine tıklandığında işler nasıl oluyor?

Sınamak için , bir aygıt kullanmanız gerekir, aygıttaki posta uygulaması, bazı posta ile yapılandırılmalıdır (örneğin: [email protected]).

Bu yardımcı olur umarım. Eğer geçerli görünüm denetleyicisi bilmiyorum sadece yani RootViewController, üzerinde gösterirseniz

// Present the view controller modally. self.present(composeVC, animated: true, completion: nil)

;-) kendi içinde kendini göstermektedir olduğunu

-1

sizin koduyla sorunudur,

UIApplication.shared.keyWindow? .rootViewController? .present (... nasıl yaptığımı

İşte
9

bu. Eğer documentati takip gibi görünüyor Çok iyi, bir başkasına yardımcı olması durumunda varyasyonumu ekleyeceğimi düşündüm. Artı, bu mevcut (Ağustos 2017) sözdizimi için biraz daha güncel.

MFMailComposeViewControllerDelegate protokolüne uyun ve aygıtın posta gönderip gönderemeyeceğini kontrol edin.

import Foundation 
import UIKit 
import MessageUI 

class WelcomeViewController: UIViewController, MFMailComposeViewControllerDelegate { 


override func viewDidLoad() { 
    super.viewDidLoad() 

    if !MFMailComposeViewController.canSendMail() { 
     print("Mail services are not available") 
     return 
    } 
} 

Uygulamam, posta bileşimini başlatmak için bir IBAction kullanır. Aşağıdaki mailComposeController işlevi hakkında

@IBAction func sendFeedbackButtonTapped(_ sender: Any) { 

    let composeVC = MFMailComposeViewController() 
    composeVC.mailComposeDelegate = self 

    // Configure the fields of the interface. 
    composeVC.setToRecipients(["[email protected]"]) 
    composeVC.setSubject("StoryBook Feedback") 
    composeVC.setMessageBody("Hey Josh! Here's my feedback.", isHTML: false) 

    // Present the view controller modally. 
    self.present(composeVC, animated: true, completion: nil) 

} 

, dokümantasyon Apple Documentation: MFMailComposeViewController