How to present login screen only when a userdefaults key doesn't exist?

Hi all


I am trying to develop an application (still learning) where i present a logon screen which takes a username and password - this then goes off to a web service to authenticate and returns an access token.


The access token is then stored in userdefaults and then presents a new view controller which gives access to the secured data.


My problem is that when i close my app - force close, it then asks to login again.


Because my login view controller is the initial view controller then i added a check to see if access token exists in userdefaults and present the new view controller which gives access to the secured data. Now my problem is that the login screen is always open behind my secured view controller so when opening the app from scratch you can briefly see the login view controller before it then presents the secured view controller.


How would i ideally handle this, is it the case the initial view controller is set to the secured view controller when the user defaults key exists - but doing this how would i handle the logout function as i would need to 'pop' to root view controller and clear user defaults, but since the login screen isnt in the view hierarchy then i cannot return to this? If it presented the login view controller on logout then the secured view controller still exists under the login view controller.


Sorry if this is a little long winded but just trying to describe the problem i am having.


Thanks

Answered by mds1256 in 208738022

I eventually sorted this by using the rootviewcontroller property from the appdelegate, this swaps the view controllers and clears down the view heirarchy as i need.

Open with a neutral viewController that contains neither the login nor the secured data. In that viewController check to see if the user has logged in. If they have - go to the secure data viewController. If they have not then present the login view controller.


But you have a different problem. An app can't require a log in to function. It can require a log in to provide secure data but it must do something even if the user refuses to log in. So you must offer that 'lite' function on that first screen and the 'option' but not 'requirement' that the user log in.

I see where you are coming from but i still seem to see the underlying view whilst its deciding on if the user has logged in or not (its quick - like a flicker) but rather have a smooth user interface.


The app is no different to the like of my banking app or facebook, spotify etc where you need to log into use the app - surely this is allowed as there are loads of apps that require accounts?


I read that when you submit the app you will need to provide Apple a test account so they can log in.

If you have the information stored locally then you can access that within the app delegate's 'didFinishLaunchingWithOptions' and set up the screen from there. However if the information is out on the web somewhere you can't do that quickly enough. And again, your logic is wrong and you will be rejected at the App Store. Then you will complain about "my banking app or facebook, spotify etc" and the App Store will say "appeal denied".

Accepted Answer

I eventually sorted this by using the rootviewcontroller property from the appdelegate, this swaps the view controllers and clears down the view heirarchy as i need.

How to present login screen only when a userdefaults key doesn't exist?
 
 
Q