52

メール送信オプションを使用してアプリをセットアップしようとしています。

私はこのコードを持っています:

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(["address@example.com"])
        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.
    }
}

そのため、「メールサービスは利用できません」というメッセージが表示されます。これで、iCloud のシミュレーター デバイスにログインしました。なぜこれが機能しないのですか?何が問題なのか、どうすれば前進できるのか教えていただけますか?

4

5 に答える 5

38

これが私がやった方法です。ドキュメントに非常によく従ったようです。他の人に役立つ場合に備えて、バリエーションを追加すると思いました。さらに、これは現在 (2017 年 8 月) の構文に少し更新されています。

MFMailComposeViewControllerDelegate プロトコルに準拠し、デバイスがメールを送信できるかどうかを確認します。

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

私のアプリは IBAction を使用してメール作成を開始します。

@IBAction func sendFeedbackButtonTapped(_ sender: Any) {

    let composeVC = MFMailComposeViewController()
    composeVC.mailComposeDelegate = self

    // Configure the fields of the interface.
    composeVC.setToRecipients(["exampleEmail@email.com"])
    composeVC.setSubject("Message Subject")
    composeVC.setMessageBody("Message content.", isHTML: false)

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

}

次のmailComposeController関数について、ドキュメントは言う

メール作成ビュー コントローラーは自動的には閉じられません。ユーザーがボタンをタップして電子メールを送信するか、インターフェイスをキャンセルすると、メール作成ビュー コントローラーはデリゲートの mailComposeController(_:didFinishWith:error:) メソッドを呼び出します。リスト 3 に示すように、そのメソッドの実装では、View Controller を明示的に閉じる必要があります。このメソッドを使用して、操作の結果を確認することもできます。

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

ソースApple ドキュメント: MFMailComposeViewController

于 2017-08-23T15:55:46.787 に答える