Search code examples
iosswiftmfmessagecomposeviewcontroller

MessageComposeViewController not calling delegate


I've been searching for solutions to my problem without any success...

The app I'm developing lets the user play a small quiz game and send the result as a text message. Everything works fine except when the MessageComposeViewController is suppose to dismiss (on send/cancel).

It seems like the MessageComposeViewController doesn't call the delegate since I don't get the print from the delegate function...

I have a separate class called SendMessage which handles the MessageComposeViewController, when the user click a button "Send" in a ViewController I create an instance of this class and present it.

Part of my ViewController with the send button:

    @IBAction func Send(_ sender: Any) {

    let sendResult = SendMessage()

    if sendResult.canSend() {
        let meddelande = sendResult.createMessage(result: 8, name: "Steve Jobs")
        present(meddelande, animated: true, completion: nil)
    } else {
        alert.addAction(alertButton)
        self.present(alert, animated: true, completion: nil)
    }

}

The class which handles the MessageComposeViewController called SendMessage (I left some irrelevant code out)

    func createMessage(result: Int, name: String) -> MFMessageComposeViewController {

    let meddelande = MFMessageComposeViewController()
    meddelande.messageComposeDelegate = self
    meddelande.recipients = ["PhoneNumber"]        
    meddelande.body = name + ": " + String(result)
    return meddelande

}


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

Grateful for any help!


Solution

  • I think you should hold a strong reference to it instead of a local variable

    let sendResult = SendMessage()
    

    declare it as instance variable

    var sendResult:SendMessage?