swiftxcodeimessageimessage-extensionmsmessage

Xcode: didTransition to Won't Run in iMessage Extension


I am making an iMessage extension that uses the didTransition(to:). However, the function won't run when I resize the iMessage extension in the simulator. Am I doing something wrong?

This is the code I have:

import UIKit
import Messages

class EditorViewController: MSMessagesAppViewController {
    @IBOutlet weak var input: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        input.text = "not changed"
    }

    // This part isn't working:

    override func didTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
        input.text = "changed"
    }
}

When I resize it in the simulator, nothing happens. The input.text changes the UITextView's text in the viewDidLoad() function, but not in the didTransition(to) function because it never runs.

Am I doing something wrong?

The EditorViewController is a view controller presented by the show (e.g. Push) segue, and has a NavigationController attached to it.

Here is a gif of it not changing:

A gif of the input not changing when the iMessage extension is resized
The input's text never changes

How can I fix this?

EDIT: The willTransition and didTransition functions don't run when the View Controller is embedded in a Navigation Controller. Is there a way to fix this? (It works without the Navigation Controller, but I need the Navigation Controller for this project).


Solution

  • As pointed out in this answer, the entry point of a iMessage App need to be a subclass of MSMessagesAppViewController, so you can not use a NavigationViewController directly as root controller, until Apple adds support for this behavior.

    But as suggested, you could solve this with a workaround like this:

    import UIKit
    import Messages
    
    class MyRootVC: MSMessagesAppViewController {
        var navVC: UINavigationViewController!
        var editorVC: EditorViewController!
        
        func viewDidLoad() {
            super.viewDidLoad()
    
            editorVC = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as! EditorViewController
        
            navVC = UINavigationController(rootViewController: editorVC)
            self.addChild(navVC)
            self.view.addSubview(navVC.view)
            navVC.didMove(toParent: self)
        }
    
        override func didTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
            editorVC.input.text = "changed"
        }
    }
    
    class EditorViewController: UIViewController {
        @IBOutlet weak var input: UITextView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
        
            input.text = "not changed"
        }
    }