1

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

  • 1
    try using print and tell us what happens when you print – a1cd May 04 '21 at 21:15
  • @Evergreen I tried this, and nothing gets printed out when I resize it. –  May 05 '21 at 00:00
  • that is a predicament! Try using `willTransition()` and if that does not work, try using `self.presentationStyle` in `viewDidLoad()` to see if it changed and then if so, call a new function that does whatever you want. – a1cd May 05 '21 at 00:21
  • @Evergreen I tried `willTransition()`, it doesn't run either. Do you think this may be because it is not the primary view controller, it is presented using a `show` segue with the `performSegue` function? **Edit**: I created a blank Xcode project, and it seems to work in that one. Idk why it won't run in the other project though. –  May 05 '21 at 18:15
  • @Evergreen I created a new project and realized that for some reason, the `willTransition and `didTransition` functions don't run when the ViewController is embedded in a navigation controller. I can't find any way around this though, or any reason this may be happening. –  May 05 '21 at 20:35
  • @user14210589 did you do exactly the same in the fresh project? – Robin Schmidt May 05 '21 at 20:35
  • @Evergreen yes, but without the Navigation Controller. I seem to have found someone else with this similar problem too: https://stackoverflow.com/questions/44933647 –  May 05 '21 at 20:38
  • @user14210589 do you know how to solve your issue by the answer in the other thread, or do you need further help? – Robin Schmidt May 05 '21 at 21:03
  • @RobinSchmidt The answer in the other thread doesn't really give a solution, because navigation controllers are the problem. Until Apple adds support for this, the only solution would be to remove the Navigation Controller. –  May 05 '21 at 21:06
  • @user14210589 No, the author explained a workaround how to use a NavigationController anyway. – Robin Schmidt May 05 '21 at 21:10
  • @RobinSchmidt ok, I just saw that. That solves my issue. –  May 05 '21 at 21:12

1 Answers1

0

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"
    }
}
Robin Schmidt
  • 1,153
  • 8
  • 15