2016-11-30 28 views
29

Próbuję skonfigurować aplikację z opcją wysyłania wiadomości e-mail.Wysyłanie wiadomości e-mail z firmy Swift 3

mam ten kod:

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. 
    } 
} 

Więc ten komunikat: „Poczta usługi nie są dostępne”. Teraz zalogowałem się na urządzeniu symulacyjnym w iCloud ... Myślę, że powinno się to zrobić, ale tak nie jest. Dlaczego to nie działa? Czy możesz mi powiedzieć, co jest nie tak i jak mogę iść do przodu?

+0

Czy skonfigurować urządzenie z adresem e-mail? jeśli nie, zrób to ... i może rozwiązać twój problem. –

+0

Używam symulatora. Jak mogę skonfigurować urządzenie za pomocą adresu e-mail? kiedy wchodzę do ustawień, nie widzę opcji "e-mail" .... –

+0

Coś, rozwiązałeś ten problem .. Tymczasem tutaj jest link do konfiguracji e-maila dla urządzenia iOS ... https: // support. apple.com/en-in/HT201320 –

Odpowiedz

29

Nie będzie działać z symulatorem. Przetestuj go na urządzeniu iPhone. Można odwołać Apple Developer Portal - MFMailComposeViewController

+0

Tak. Jestem w 100% pewien.Pracuję nad tym samym typem aplikacji i znalazłem to. –

+0

Ok dzięki :) ale kod napisałem powyżej. Czy to jest w porządku? –

+0

Tak. Kod wydaje się w porządku i powinien działać. Nie masz iPhone'a? –

4

Kod wydaje się być dobre i działa dobrze, jeśli aplikacja działa w rzeczywistym urządzeniu

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

nie można przetestować go na symulatorze, będzie można przetestować podstawowe rzeczy jak interfejs użytkownika, jak to się dzieje na przyciskach Anuluj/Wyślij.

Aby przetestować, musisz użyć urządzenia, aplikacja Mail w urządzeniu powinna być skonfigurowana z pewną pocztą (np. [email protected]).

Nadzieję, że pomaga.

-1

Problem z kodem jest, że

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

prezentuje się w sobie ;-)

Jeśli nie znam aktualnego kontrolera widoku, po prostu wyświetlić go na RootViewController, tj

UIApplication.shared.keyWindow? .rootViewController? .present (...

9

Oto jak to zrobiłem. wygląda na to, że po documentati bardzo dobrze, pomyślałem, że dodam moją odmianę na wypadek, gdyby pomogła komuś innemu. Dodatkowo jest to nieco bardziej zaktualizowane do obecnej składni (sierpień 2017).

Dostosuj się do protokołu MFMailComposeViewController Usuń protokół i sprawdź, czy urządzenie może wysyłać pocztę.

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

Moja aplikacja używa IBAction do inicjowania kompozycji poczty.

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

} 

O następującej funkcji mailComposeController dokumentacja mówi

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

Źródło Apple Documentation: MFMailComposeViewController