Search code examples
iosimessage

How does iMessage preserve viewController state after view disappears?


I observed that iMessage preserves last typed message in a chat even if I move to another chat and come back to it.

For eg. I am in Chat1 and typing. Then without sending the message I move to Chat2 and then come back to Chat1. Last typed message is still there.

If I understand correctly, when I press back button in Chat1, it should execute viewWillDisappear which should destroy all local elements.

Then how is last typed message still there?


Solution

  • iMessage is not the only app which does it. Whatsapp also does it. Even you open a chat in whatsapp scroll to older message->go back->and come again on the same chat. You will see the old state of chat.

    The Recommended way to do it is App State Restoration if you want to store the whole state of your controller with more UI elements and values.

    You can check following tutorial available for this..

    https://www.raywenderlich.com/117471/state-restoration-tutorial

    http://useyourloaf.com/blog/state-preservation-and-restoration/