I'm trying to make a log in system for the app I am developing for a class project. This log in button would display on the navigation bar of most views so I was thinking of a separate class to initialize in the various view controllers to create the UIAlertView and handle the app to server communication. The issue I'm having is changing the Log In text to Log Out after the username and password has been checked and saved. I have been able to show the UIAlertView and talk with the server but am stuck on changing the text. I initially put a method in the view controller to change the text, with no success, but within the user file would be preferable. It is my first time using a singleton so I'm not interely sure on the correctness. Am I going about this the wrong way, is there a better way? Thanks for any help.
HomeViewController.m
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
self.navigationController.navigationBar.topItem.title = @"Home";
UIBarButtonItem *login = [[UIBarButtonItem alloc] initWithTitle:@"Log In" style:UIBarButtonItemStylePlain target:self action:@selector(log)];
self.navigationController.navigationBar.topItem.rightBarButtonItem = login;
}
-(void)log {
User* singleton = [User getInstance];
[singleton sign];
}
/*
-(void)logfinish {
if ([[NSUserDefaults standardUserDefaults] stringForKey:@"Username"] && [[NSUserDefaults standardUserDefaults] stringForKey:@"Password"]) {
UIBarButtonItem *logout = [[UIBarButtonItem alloc] initWithTitle:@"Log Out" style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationController.navigationBar.topItem.rightBarButtonItem = logout;
}
}*/
User.m
@implementation User
static User *singletonInstance;
+ (User*)getInstance{
if (singletonInstance == nil) {
singletonInstance = [[super alloc] init];
}
return singletonInstance;
}
-(void)sign {
if (![[NSUserDefaults standardUserDefaults] stringForKey:@"Username"] && ![[NSUserDefaults standardUserDefaults] stringForKey:@"Password"]) {
signAlert = [[UIAlertView alloc] initWithTitle:@"Log In" message:@"Please sign into your account" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Log In", nil];
signAlert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[signAlert show];
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *person = [signAlert textFieldAtIndex:0].text;
NSString *pass = [signAlert textFieldAtIndex:1].text;
NSDictionary *params = @ {@"User": person, @"Password" :pass};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:signIn parameters:params success:^(AFHTTPRequestOperation *operation, NSData* response) {
NSString *responsef = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
//NSLog(@"%@", responsef);
[self check:responsef user:params];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
-(void)check:(NSString*) pass user:(NSDictionary*)nandp {
if ([pass isEqualToString:@"Success"]) {
[[NSUserDefaults standardUserDefaults] setValue:[nandp objectForKey:@"User"] forKey:@"Username"];
[[NSUserDefaults standardUserDefaults] setValue:[nandp objectForKey:@"Password"] forKey:@"Password"];
//NSLog(@"%@ logged in", [[NSUserDefaults standardUserDefaults] stringForKey:@"Username"]);
//HomeViewController *view = [[HomeViewController alloc] init];
//[view logfinish];
}
else NSLog(@"Failure");
}