How do I manage a login view in a navigation-based app? - iphone

I am designing an iphone application in objective C.
As of now, I have a UINavigationController in the beginning of my application
and rest of the navigation was being handled from it.
Now I want to insert a login screen when the application is loaded.
How do I make it independent of my rest of the application ?
i.e. As of now, I created a LoginViewController and added it to the NavigationViewController
of my app. When the user successfully logs in, the application continues with the next screen being pushed into the Navigation Controller.
But the problem with this approach is that, I can still go back to the initial login screen from the navigation item.
I have tried to hide the navigation bar from the first screen after login, but it removes the navigation bar from each of the subsequent screens.
The only working solution I can think of is that, I should manually hide the navigation bar in the start screen and make it visible in the subsequent screens.
Is there any other sane approach ?

I'd probably do your nav controller disregarding the login screen, and then present the login screen using presentModalViewController:animated:

present your LoginViewController in viewDidLoad for the the navigationController once the user login you dismiss it and continue your screen's flow as you want. also you can store that user login your app in the NSUserDefault so you can check this value and present the loginView in case he didn't login otherwise you show the navigation screen as normal

If the user have not login in the app show LoginViewController and don't allow the user to go to navigation controller till then. Once login don't show LoginViewController directly show navigation controller so that user dont have to sign again and again.. To store user login information use NSUserDefaults. Retrieve the info at loading of the app and display controllers accordingly.
Cheers

You need to store in UserDefaults some value - auto login / did login and when app is start you check this value if user isn`t in system u need to create login view controller and present it modaly.
Before your first view appear u need something like this -
BOOL didLogin = [[NSUserDefaults standardUserDefaults]boolForKey:#"isLogin"];
if (!didLogin) {
LoginViewController *loginVC = [[LoginViewController alloc]init];
[self presentModalViewController:loginVC animated:NO];
}
and when user is login you need to save value in user defaults
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"isLogin"];
if you vant auto login feature you must store password in keychain

I would suggest to hide back button at your firstviewcontroller and unhide in rest all viewcontrollers.
-(void)viewWillAppear:(BOOL)animated {
self.navigationItem.hidesBackButton=YES;
}
Otherwise another solution is that present the loginviewcontroller in the viewDidLoad method of your firstviewcontroller and as login completes dismiss the presented logincontroller on login button.
Even its the standard method to implement, all apps having login facility would have this type of flow.
Hope this will help you out.

Related

How to use UISegmented control to keep the user logged in

I'm creating a simple app that loads a modal view in the root view controller, and that modal view has a segmented control for a user to choose if he/she wants to stay logged in. The modal view has a username and password field with a submit button below it. After the app verifies the user, he/she will be taken to the root view controller that has buttons to take the user to another view i.e. Feature 1, Feature 2, etc. If the user taps the back button (driven by a navigation controller), I want the user not to be asked again for his/her credentials when the app loads the root view controller again, thus the use of the segmented control.
How can use and implement the segmented control in this case? Thanks!
I assume you've realised this, but you need to store the login credentials, I assume your question is 'How!?'
If you want to store the login credentials for just the lifetime of the app (they are logged out when you close the app), I'd store the variables in Globals class so that they can be referenced from anywhere in the app.
If you want to keep the login credentials stored longer (i.e. they may still be logged in if they close and reopen the app), you should use NSUserDefaults:
Writing:
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"UserIsLoggedIn"];
[[NSUserDefaults standardUserDefaults] synchronize];
Reading:
BOOL loggedIn = [[NSUserDefaults standardUserDefaults] boolForKey:#"UserIsLoggedIn"];
Please also note the following considerations though:
UserDefaults aren't secure. Please don't store passwords there without encrypting them.
boolForKey: returns NO if the key you provide doesn't exist
Why are u going for UISegmentedController? Just use simple UIBarButtonItem as Logout button on RootViewControllers navigation bar.
In RootViewControllers viewWillAppear/viewDidAppear method check wheather user is already Logged in or not. Based on that loads modal view.

iPhone dev - Navigation-based app with a start-up log-in screen?

I'm making a navigation-based app that I want to start out with a screen that let's the user enter a username/password before continuing on to the normal navigation hierarchy of views. I'm trying to think about the best way to go about doing this. Should I create a new viewController for the log-in screen and start out with that and then use presentModalViewController to go to the RootViewController of the navigation hierarchy? Or would it be better to start out in the RootViewController (a tableViewController) and immediately push the log-in view onto the screen, and then pop it if they enter a correct username/pw combo? I'm also curious as to HOW you would start your application with a different view from the RootViewController, because right now that's always the first one that shows up. Thanks!
If the user suspends/resumes your app or locks/unlocks the phone, are you going to make them login again? If so, then I'd choose your second option, pushing the LoginView above the NavigationView. Otherwise I think starting with the LoginView and replacing it with the NavigationView is fine.
Your ApplicationDelegate class specifies which controller will serve as the root controller for the application.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window.rootViewController = self.navigationController;
...
}
In this example, an instance of UINavigationController is set as the root view controller once the application has completed launching (e.g. all resources are loaded and the transition is made from the splash image).
If you want a different controller to serve as the root of your application, then this is where you'd do it.
To answer your question about the design, it depends on the desired workflow. Based on what you've described, I'd start with a barebones view controller that loads up a login view/controller. Upon authenticating the user, this view controller can switch to the main application view. With a design like this, you can easily switch back to the LoginViewController if you need to re-authenticate the user at any point.
ApplicationViewController (root)
- LoginViewController
- MainViewController (this could be a UINavigationController or whatever you need it to be)
Hope that helps!

Proper viewController setup for iPhone with login and logout functionality

In developing my current iPhone application, I'm having issues handling the login, logout functionality of presenting views.
I'd like to have my application have the following flow, but I cannot seem to figure out the proper viewcontroller setup:
When a user is not logged in, a login screen is immediately presented. Upon a successful login, the main application is displayed. The main application is a TabBarController. When returning back to the application, if you have already logged in, the login window will not display, but immediately go into the main tab bar. Once in the app, you can "logout" and it will take you back to the login scren.
Please let me know if I need to go into further detail. There are a few other questions on here that are similar but not exactly what I'm looking for.
Thanks!
I would consider two ways of doing this:
have the login screen be a modal view controller that pops over the main UI.
e.g.
if (currentCredentials == nil) [self presentModalViewController:loginView animated:YES];
or alternately, handle switching between views using your app delegate.

Best way to accomplish this scenario with viewControllers - iPhone

I'm writing an app that will allow iPhone users to login to their accounts. Once logged in the user can pull information, change passwords, change other types of data via webservices. I'm new to iPhone programming and I'm a bit confused on my viewController setup to handle this. What I would like to do is the following;
1) if the user is NOT logged in - display a view which takes up the entire screen displaying my login/password text fields etc. ( I will have some conditionals set in place checking to see if the user has u/p saved in a plist)
2) if user is logged in or once the user logs in, remove the current full size view and load the tabBar view. If the user logs out then the main login view will load.
I will need two viewControllers for this particular scenario? I've read a great deal on view controllers. Read all of the Apple documentation but I still get confused since there are so many options/methods to use.
As always I thank you in advance.
T
Just one of many possible options:
Create LoginViewController and UITabBarViewController. Application delegate will check on start-up whether it already has user's credentials and show the appropriate controller
if ([dataModel hasUserCredentials])
[window addSubview:[tabControlle view]];
else
[window addSubview:[loginController view]];
LoginViewController performs log in and notifies app delegate:
- (void) loginComplete
{
// XXX animation?
// XXX view(Will/Did)(Appear/Dosappear) and all the stuff,
[[loginContoller view] removeFromSuperview];
[window addSubview:[tabController view]];
}
Here is how I would do it:
The tab bar controller is your main view controller. You initialize it first (in the main NIB file) and add it to the window in your app delegate.
In application:didFinishLaunchingWithOptions:, you check whether the user is already logged in or not. If not, you immediately present your login screen as a modal view controller. If you do this without animation, the user will not notice that the tab bar controller is already present underneath.
When the user logs in, you dismiss the modal view controller and your tab bar UI becomes visible.
You should look at creating a Navigation-Based application. This will have a UINavigationController built in. You can use that to control your windows.

Is it possible to change the root view of a tabBarItem so tapping takes you to the second view in the hierarchy?

In Obj-C / iOS. Our UI design calls for an initial screen with options to register or log on. After doing either of these, the user data is saved to the phone and a the tabBarController is shown. However, tapping on the tabBar at the bottom takes the view back to the "log on or register" screen. Is it possible to change the root view controller in a tabBarItem's hiearchy?
What you have to do is have a UIViewController called "LoginViewController" as your Root Controller but this controller does not have a UITabBar it's just a controller with the Login UIView. And also have your UITabBarController with all of its content set but don't include the Login just have your basic content.
After the user logs in you show the UITabBarController with its content.
-(void) LoginUser {
// Load UITabBarController
YourAppDelegate *app = (YourAppDelegate*)[[UIApplication sharedApplication] delegate];
[app.window addSubview:aTabBarController.view];
}
where app is your *app Delegate.
You can't change the root, but you can solve this a few ways. Perhaps the simplest is in your viewWillAppear method check to see if the user is logged in, if so, immediately load (using navigation controller, modally or just add a subView depending on your app's structure), the regular logged in view without animation, then the user will never see that this happened. (You might choose to do it the other way, only load the login screen if the user is NOT logged in.)
I am using a navigation controller instead of a tab bar controller, but the concept I think is similar or the same. I did not want the user being able to "track back" to the log in screen once they had logged in.
I built my log in view as a modal view that is called in the ViewDidLoad of the main app view controller if the user is not logged in:
if (isUserLoggedIn == NO) {
[self performSegueWithIdentifier:#"logInUserModalSegue" sender:self];
}
The main view loads briefly and then the LogIn modal view animates up, covering everything.
When the user hits log in, I check the credentials and then dismiss (or not) the modal controller, like this:
if (logInStatus == YES) {
[[self presentingViewController] dismissViewControllerAnimated:YES
completion:NO];
} else {
self.errorDisplay.text = #"Sorry, there was an error logging in.";
}