Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - How to remove all ViewControllers in the applications window hierarchy?

My app uses a typical authentication scenario, where the user logs in with his credentials against the server, which is returning an authentication token which is then stored locally on the client (iPhone).

Now it is not likely but possible the stored authentication token gets invalid over time. As every request made with that invalid token would fail, I want to handle that by "logging the user out" of the application when that failure occurs for the first time. Note: that request may be made in a background thread, because the app is synchronizing with the server periodically.

Logging out means:

  1. Deleting the locally stored token
  2. Deleting all the other content stored by the user (in Sqlite)
  3. "Redirecting" the user to the login controller

So it is possible the user is viewing some content within the app, while the token gets invalid and the sign out process gets started.

As all content gets deleted (#2) it is not sufficient to present her the login controller, because after logging in and dismissing that controller the previously content isn't really there anymore.

To prevent these kind of side effects I want to completely remove all view controllers in the hierarchy, regardless how deep it is when the logout is triggered.

So my question is:

What's the best way to "kill" all active controllers before presenting the login controller?

like image 545
asp_net Avatar asked Mar 23 '23 04:03

asp_net


1 Answers

Assign your LoginViewController to your window's rootViewController. Here's the description of rootViewController from the documentation. It sounds exactly like what you are trying to accomplish.

The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller’s view as the content view of the window. If the window has an existing view hierarchy, the old views are removed before the new ones are installed.

like image 184
Jonathan Arbogast Avatar answered Apr 06 '23 10:04

Jonathan Arbogast