I have set my app’s initial view with storyboard to the main one. I have this code in the app delegate that should load the login VC if the user is not signed in already:
if (currentUser) {
// do stuff with the user
//use storybaord nibs
} else {
// show the signup or login screen
UIStoryboard *storyboard = self.window.rootViewController.storyboard;
self.window.rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"loginVC"];
}
The problem is I don’t have the logic for currentUser. That is just suedo code. How can I set this up properly?
This is the code I use in the actual login VC to log a user in. I save username/pass to NSUserDefaults.
-(IBAction)loginButtonPressed:(id)sender{
ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://myapp.com/20111115/60b88126/login_user/"]]];
[request setPostValue:self.usernameTextField.text forKey:@"username"];
[request setPostValue:self.passwordTextField.text forKey:@"password"];
[request startSynchronous];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:self.usernameTextField.text forKey:@"login"];
[userDefaults setObject:self.passwordTextField.text forKey:@"password"];
NSError *error = [request error];
NSString *responseString = nil;
if (!error) {
responseString = [request responseString];
} else {
responseString = @"Failed to connect to server, please check your WiFi or 3G connection.";
}
NSDictionary *responseDictionary = [responseString JSONValue];
NSString *loginProblems = [responseDictionary valueForKey:@"login_errors"];
if (![loginProblems isEqualToString:@""]) {
//Invalid login
} else {
[self performSegueWithIdentifier:@"login" sender:self];
}
}
can you just check to see if the NSUserDefaults have values for the username/password and proceed from there? you would just need to make sure to clear those values when he logs out.
So if the username/password field for NSUserDefaults is empty, send him to the login. if it has values, “do stuff”. when he logs out, clear the values?
or make an instant variable in the AppDelegate stating he is logged in or not?
create the variable in the app delegate just like normal a normal instance variable then use:
to access it in a different view controller (need to declare it in any method you use it in).