简体   繁体   中英

MessageComposeViewController only presents once

I'm using Xcode 8 to build an application for iPhone.
My simple app has an button. When user tap on this button, messageComposeViewController is called and with phone number and message content filled.
The message went through successfully when I clicked on Send button.

The problem is MessageComposeViewController is showing only once.
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 ..

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

In Your 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()
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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