How to use UISegmented control to keep the user logged in - iphone

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.

Related

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

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.

Alert password field that stores password into the keychain and checks it against original login view controller

This is what I would like to happen I have screwed this up 60 times over now.
Ok, I have a login View Controller that handles the login using the username and password and a remember me switch. The login view controller verifies against an API, checks the credentials then proceeds. If the user has selected the remember me switch the password is stored. Next I have a separate deposit view controller. Once the user clicks the deposit button within this deposit controller it pops up an alert with a password field and once the password is entered it checks against the original login screen password to see if they match if they do. It pushes on another view controller and then life goes on. Otherwise if it doesnt match the stored pass and alert with and error is presented.
This just doesnt seem that hard but I'm having the hardest time with it I hope I made myself clear and questions let me know.
-Justin
In order to save text entered in a textField in an alertView, you have to store it in another variable before the alertView is dismissed. I use -(void)textFieldDidEndEditing:(UITextField *)textField to store the variable, since that is called after editing is complete, but before the alertView is dismissed.
Hope this helps!

Presenting Modal View Controller login screen

In my app there is authentication required, so when you launch one of the tabs on tab bar, "class A" checks are there credentials saved if not, "class B" modal view controller with fields to login launches.
So my question is : in which method in class A (loadView, viewWillAppear or maybe in another one) should be implemented checking if there are credentials saved and other stuff described above.
And my additional second question is:
is pushing modalviewcontroller correct way to show login screen, or i should do that differently?
Thank you for reply guys.
OH ! One More Thinh
And one more thing. I've done implementing LoginView by adding delegate and presenting ModalVC (Harkonian the Piquant's method). But in my tab bar app i have got very confusing problem. I mean when user taps login button (assume that everything was correct and he's able to secured data) how PROPERLY switch to tab where is secured info. I mean previously selected tab.
i did it by adding in
-(IBAction) login {
//some code
self.tabBarController.selectedIndex =1;
And it seem to work good but is it correct ?
I have a very similar use case in my app -- it requires a passcode to authenticate. After a lot of testing and tweaking I found the following design to be the best approach:
Don't use class A to launch your credentials VC -- use the app delegate instead.
For security purposes, typically you'll want the credentials VC to show before the user can view the underlying view. It's much easier to handle this in the app delegate than in a VC. In addition, you need to consider what happens when your app is backgrounded -- a screen shot is taken of the current state of the app. If you are using viewController A to show the credentials view, when the app relaunches the user will be able to see whatever sensitive information was visible on app close until the app finishes launching and VC A presents the credentials VC.
Don't insert your credentials view into an existing ViewController -- use a new UIWindow instead.
You don't ever want any other view to be able to sit on top of your credentials view. Ever. Even views that would normally always be on top, like UIAlertView. The easiest way to achieve this is to have a special UIWindow just for your credentials view. Show this window and hide the primary app window whenever you need to display the credentials view.
How does this approach look in practice?
If you are at all interested in how well this design works, you can check out the passcode feature in Audiotorium Notes for iPad. I spent a lot of time with this design to make sure it was as secure as possible.
If you have any specific implementation quests feel free to ask and I'll try to answer them.

How to reset an iOS application state after a user clicks sign out?

So I posted a question a while back which can be seen at: How to reset an iOS application when a user clicks the "sign out button"?
Following the advice I made a sign out button where a user where by is taken to the main screen where they can register or sign in again. What I am finding out is that when a new user signs in, I am seeing certain value from the old user in pickers, UITextView ETC
Is there a way to reset application state or do I have to go the long route of making sure that each outlet is set to default values? Is this a sign of bad programming practice somewhere?
The Cocoa way = KVO (key value observing). Controllers interested in being informed about login state change register themselves as observers on login component/controller/whatever instance does the login.
After login/logout this component notifies all observers about state change. Those then do all necessary actions: populating UI with user data after log in or resetting them after log out.
Very flexible pattern that avoids unnecessary dependencies between components.
Ok first thing you have to do is make your main view implement the delegate for signout successful. In this you got to reset all the data and views which will be recreated/repopulated when a new user signs in.
Inorder to achieve this you can analyse your code/logic as to what is created on a new user sign in and reset all these in the delegate method for sign out. This is way to generic an answer but resetting the data is the way to do it.
Alternatively you can recreate your main view when sign in is successful i.e. remove the main view and on sign in success create it afresh for the new user.
The cleanest way would be to create a new set of view controllers and set them to the viewControllers of the UITabBarController object but it needn't be the cheapest always. This will be something that you need to check if it's viable or not.
Otherwise, you'll have to to consider adding reset methods to the view controllers. If the tab is a navigation controller, popToRootViewControllerAnimated: and reset the first view controller. This one is a bit of effort to implement compared to the former approach.
When ever you go to new controller just allocate the whole controller again. and dont forget to release once the navigation is done.
Heres the sample code example to do it
-(void)goToFormController
{
FormViewController *objFormViewController = [[FormViewController alloc]initWithNibName:#"FormViewController" bundle:nil];
[self.navigationController pushViewController:objFormViewController animated:YES];
[objFormViewController release];
}
Happy iCoding...

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.