Search code examples
ioswatchkit

What is the best way to show a message on Apple Watch independently of the active interface controller?


I would like to display a local message (generated by WatchKit code) on the Apple Watch. My problem is that the message may be triggered by a code segment outside of the currently active interface controller.

What is the best way to modally present a new interface controller independent of the currently active (top-most) interface controller?

One way would be to get a pointer to the currently active interface controller. But how to do this in an easy way?

  • One possible solution is to use a global property that holds a pointer to the currently active interface controller. It gets updated in the willActivate methods of all interface controllers.

  • Another solution would be to always pass the pointer to the currently active interface controller to all methods that may want to show a message.

  • Is there an easier way to get a pointer to the top-most interface controller?

Well, I'm not even convinced that the above approach is a good one. What if the top-most controller is dismissed shortly after presenting the interface controller with the message? Then the message is probably not shown very long or not at all.

Is it possible to modally present the message interface controller on top of everything independent of the currently active (top-most) interface controller?

Should I use local notifications? Is there something like UIAlertView?

What is the best and easiest way to solve this problem?


Solution

  • To show a message independently of the active interface controller, it is possible to present an interface controller using presentControllerWithName:context: from the main interface controller (the one with the arrow in the storyboard). This works even if the main interface controller is not currently active.

    For this solution to work, it is necessary that the method that calls presentControllerWithName:context: has a pointer to the main interface controller. This can be achieved by passing self from the main interface controller to other interface controllers and methods.

    However, I sometimes want to show a message from an object deep down in the hierarchy. And I do not want to pass the pointer to the main interface controller to every method that possibly wants to display a message to the user.

    So, my current solution is to store a pointer to the main interface controller in a global variable (using extern) and then presenting from this main interface controller.

    Acknowledgements: I would like to thank "Schemetrical" and "Mike Swanson" for the comments that solved this problem.