简体   繁体   English

MessageComposeViewController只呈现一次

[英]MessageComposeViewController only presents once

I'm using Xcode 8 to build an application for iPhone. 我正在使用Xcode 8为iPhone构建应用程序。
My simple app has an button. 我的简单应用程序有一个按钮。 When user tap on this button, messageComposeViewController is called and with phone number and message content filled. 当用户点击此按钮时,将调用messageComposeViewController ,并填写电话号码和消息内容。
The message went through successfully when I clicked on Send button. 单击“发送”按钮后,该消息成功完成。

The problem is MessageComposeViewController is showing only once. 问题是MessageComposeViewController只显示一次。
After the message sent, when I tapped on the button to call it, a black screen showed up instead of the message composer. 发送消息后,当我点击按钮调用它时,会出现一个黑屏而不是消息编写器。

My code is attached below. 我的代码附在下面。
I appreciate any help. 我感谢任何帮助。

import UIKit
import MessageUI

class ViewController: UIViewController, MFMessageComposeViewControllerDelegate   {

    let msg = MFMessageComposeViewController()

    @IBOutlet weak var coordinate_label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    @IBAction func sendMessage(_ sender: AnyObject) {

        self.msg.body = "Message Content"
        self.msg.recipients = ["xxx-xxx-xxxx"]
        self.msg.messageComposeDelegate = self

        self.present(msg, animated: false, completion: nil)
    }

    func messageComposeViewController(_ controller:   MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
        switch result.rawValue {
            case MessageComposeResult.cancelled.rawValue:
                dismiss(animated: true, completion: nil)
            case MessageComposeResult.failed.rawValue:
                dismiss(animated: true, completion: nil)
            case MessageComposeResult.sent.rawValue:
                dismiss(animated: true, completion: nil)
            default:
                break;
        }
    }   
}

Try this code 试试这个代码

import Foundation
import MessageUI

let textMessageRecipients = ["1-800-867-5309"] // for pre-populating the recipients list (optional, depending on your needs)

class MessageComposer: NSObject, MFMessageComposeViewControllerDelegate {

    // A wrapper function to indicate whether or not a text message can be sent from the user's device
    func canSendText() -> Bool {
        return MFMessageComposeViewController.canSendText()
    }

    // Configures and returns a MFMessageComposeViewController instance
    func configuredMessageComposeViewController() -> MFMessageComposeViewController {
        let messageComposeVC = MFMessageComposeViewController()
        messageComposeVC.messageComposeDelegate = self  //  Make sure to set this property to self, so that the controller can be dismissed!
        messageComposeVC.recipients = textMessageRecipients
        messageComposeVC.body = "Hey friend - Just sending a text message in-app using Swift!"
        return messageComposeVC
    }

    // MFMessageComposeViewControllerDelegate callback - dismisses the view controller when the user is finished with it
    func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
        controller.dismissViewControllerAnimated(true, completion: nil)
    }
}

Note, latest syntax 2017 .. 注意,最新语法2017 ..

func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
    controller.dismiss(animated: true, completion: nil)
}

In Your viewController .. 在你的viewController ..

import UIKit

class ViewController: UIViewController {
    // Create a MessageComposer
    let messageComposer = MessageComposer()

    @IBAction func sendTextMessageButtonTapped(sender: UIButton) {
        // Make sure the device can send text messages
        if (messageComposer.canSendText()) {
            // Obtain a configured MFMessageComposeViewController
            let messageComposeVC = messageComposer.configuredMessageComposeViewController()

            // Present the configured MFMessageComposeViewController instance
            // Note that the dismissal of the VC will be handled by the messageComposer instance,
            // since it implements the appropriate delegate call-back
            presentViewController(messageComposeVC, animated: true, completion: nil)
        } else {
            // Let the user know if his/her device isn't able to send text messages
            let errorAlert = UIAlertView(title: "Cannot Send Text Message", message: "Your device is not able to send text messages.", delegate: self, cancelButtonTitle: "OK")
            errorAlert.show()
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM