E. Rivera
E. Rivera

Reputation: 10938

Prevent dismissing modal WKInterfaceController

Is there a way to prevent the user from dismissing a modal controller?

I think it is pretty common to want to "block" the main Watch App interface while asking the user to open the iPhone counterpart or to perform some action there.

My current solution is to present again the controller when it gets dismissed but its clunky.

Upvotes: 1

Views: 1739

Answers (3)

Hackman
Hackman

Reputation: 2659

The trick is to make the modal screen fullscreen and change the inset top value for your main group.

enter image description here Preview of modal view

Upvotes: 1

Ben Lachman
Ben Lachman

Reputation: 3100

There's a somewhat kludgy way to get around this issue using reloadRootControllers. When you call reloadRootControllers(withNamesAndContexts:) with the name of a WKInterfaceController that you've named in your storyboard, it has a similar effect to presenting that controller modally. However, since it's now the root controller, it doesn't have a cancel button. I don't really like this, but it does get the job done.

Note this method is deprecated since watchOS 4. Apple Documentation on reloadRootControllers(withNamesAndContexts:)

Upvotes: 2

user4151918
user4151918

Reputation:

You can't prevent a modal interface controller from being dismissed, as the system automatically dismisses it when the title is tapped.

Since your code isn't asked if it should happen, but only knows that it is happening, there's no way to intercept or cancel that action. The WKInterfaceController documentation briefly touches on this.

When the user taps the title string, WatchKit automatically dismisses the modal interface without taking any further actions.

What can you do?

While you don't know when the Cancel title is tapped, there is a hack which "hides" the Cancel title.

This may confuse users who might wonder how to dismiss the modal, or mislead others into thinking the modal couldn't be dismissed.

What does the HIG recommend?

Circumventing a Human Interface Guideline would likely degrade the entire user experience.

The top-left corner of the modal sheet is reserved for the Close button, which dismisses the interface.

Some users might be frustrated or annoyed if

  • there is no apparent way to cancel, or

  • the modal presents itself again after repeatedly being cancelled.

Since the user expects to be able to dismiss the modal, perhaps you could allow them to do just that, then simply display some form of reminder in the presenting interface controller (to log in, or enable permissions).

Upvotes: 0

Related Questions