LogInViewController.m 7.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
//
//  LogInViewController.m
//  PLUCdraft
//
//  Created by Alexander W. Moore on 2/3/15.
//  Copyright (c) 2015 Alexander W. Moore. All rights reserved.
//

#import "LogInViewController.h"

@interface LogInViewController ()

@end

@implementation LogInViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self.view action:@selector(endEditing:)]];
    self.navigationItem.hidesBackButton = YES;
22 23
}

24 25 26 27 28
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

29 30
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
Alex Moore's avatar
Alex Moore committed
31
    [self resetRTI];
32
    [self checkAndSatisfyUserLoginPreferences];
33 34
}

35
- (void)webViewDidFinishLoad:(UIWebView *)webView {
36
    [self evaluateNextRTIAction:webView];
37 38 39
}


40 41 42 43 44 45 46
#pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 
47 48
}

49 50
- (IBAction)prepareForUnwindToLogin:(UIStoryboardSegue *)sender {
    
51 52
}

53
#pragma mark - Helper Functions
54

55
- (void)checkAndSatisfyUserLoginPreferences {
56
    
57 58 59 60 61
    self.txtPassword.text = @"";
    self.txtUsername.text = @"";
    
    //Set switches, username, password
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
Alex Moore's avatar
Alex Moore committed
62
    
63 64 65 66 67 68 69 70
    if ([prefs boolForKey:@"rememberUsername"]) {
        [self.rememberUsernameSwitch setOn:YES];
        self.txtUsername.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"usernameString"];
    }
    if ([prefs boolForKey:@"rememberPassword"]) {
        [self.rememberPasswordSwitch setOn:YES];
        self.txtPassword.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"passwordString"];
    }
Alex Moore's avatar
Alex Moore committed
71 72
}

73 74 75 76
- (void)resetRTI {
    //Remove delegate from webview
    [self.webview setDelegate:nil];
    
77
    //Reset global uiwebview
Alex Moore's avatar
Alex Moore committed
78
    GlobalWebController *globalWeb = [GlobalWebController resetInstance];
79
    //Create new uiwebview
Alex Moore's avatar
Alex Moore committed
80
    self.webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
81
    //Set global uiwebview = new uiwebview, mark as initialized
Alex Moore's avatar
Alex Moore committed
82 83
    globalWeb.web = self.webview;
    globalWeb.isInitialized = true;
84 85
    
    //Set current view as delegate of uiwebview
86
    [self.webview setDelegate:self];
87 88 89 90 91 92 93 94 95
    //Create URL request for RTI webpage
    NSURLRequest *rti;
#ifdef TESTING
    rti = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://162.223.19.75/rti/"]];
#else
    rti = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.1.131/rti/"]];
#endif
    
    //Eliminate cache and cookies
Alex Moore's avatar
Alex Moore committed
96 97 98 99 100 101 102
    [[NSURLCache sharedURLCache] removeCachedResponseForRequest:rti];
    //Clean the cookie jar
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (NSHTTPCookie *cookie in [storage cookies]) {
        [storage deleteCookie:cookie];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
103 104
    
    //Load URL in uiwebview
Alex Moore's avatar
Alex Moore committed
105
    [self.webview loadRequest:rti];
106 107

    //Add uiwebview as subview to current view
108
    [self.view addSubview:self.webview];
Alex Moore's avatar
Alex Moore committed
109 110
}

111 112 113 114 115 116 117 118
- (void)evaluateNextRTIAction:(UIWebView *)webView {
    NSString *url = [[webView request] URL].absoluteString;
    NSString *base_dest_url;
#ifdef TESTING
    base_dest_url = @"http://162.223.19.75";
#else
    base_dest_url = @"http://192.168.1.131";
#endif
119
    
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    if ([url isEqualToString:[base_dest_url stringByAppendingString:@"/rti/"]]) {
        NSLog(@"0: %@", url);
    }
    else if ([url isEqualToString:[base_dest_url stringByAppendingString:@"/rti/RTILogin.aspx"]]) {
        NSLog(@"1: %@", url);
    }
    else if ([url isEqualToString:[base_dest_url stringByAppendingString:@"/rti/RTISetup.aspx?location=1"]]) {
        NSLog(@"2: %@", url);
        
        //Remember username/password if desired
        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        if ([prefs boolForKey:@"rememberUsername"]) {
            [prefs setValue:self.txtUsername.text forKey:@"usernameString"];
            
        }
        else {
            [prefs setValue:@"" forKey:@"usernameString"];
        }
        [prefs synchronize];
        if ([prefs boolForKey:@"rememberPassword"]) {
            [prefs setValue:self.txtPassword.text forKey:@"passwordString"];
        }
        else {
            [prefs setValue:@"" forKey:@"passwordString"];
        }
        [prefs synchronize];
        
        //Segue to section page
        [self performSegueWithIdentifier:@"segueLoginToSection" sender:nil];
    }
    //Page handles username/password errors; now handle deeper errors
    else {
        NSLog(@"3: %@", url);
        /*
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"FATAL PAGE ERROR" message:@"Page not recognized, cannot proceed.  App will no longer work as expected." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
         [alert show];
         */
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"FATAL PAGE ERROR" message:@"Page not recognized, cannot proceed. App will no longer work as expected." preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [alert dismissViewControllerAnimated:YES completion:nil];
        }];
        [alert addAction: ok];
        [self presentViewController:alert animated:YES completion:nil];
    }
164
}
165 166 167


#pragma mark - UI Buttons
168 169

- (IBAction)btnLogInTouch:(id)sender {
170
    
171 172 173
    NSString *login1Path =
    [[NSBundle mainBundle] pathForResource:@"Login1" ofType:@"js"];
    NSString *login1 = [NSString stringWithContentsOfFile:login1Path
174 175 176
                                                encoding:NSUTF8StringEncoding
                                                   error:nil];
    
177 178 179 180 181 182
    NSString *username = @"user.value = \"";
    username = [username stringByAppendingString:[self.txtUsername.text stringByAppendingString:@"\";"]];
    
    NSString *password = @"pword.value = \"";
    password = [password stringByAppendingString:[self.txtPassword.text stringByAppendingString:@"\";"]];
    
183 184 185 186 187
    NSString *login2Path =
    [[NSBundle mainBundle] pathForResource:@"Login2" ofType:@"js"];
    NSString *login2 = [NSString stringWithContentsOfFile:login2Path
                                                encoding:NSUTF8StringEncoding
                                                   error:nil];
188
    
189
    NSString *login = [login1 stringByAppendingString:[username stringByAppendingString:[password stringByAppendingString:login2]]];
190 191
    
    [self.webview stringByEvaluatingJavaScriptFromString:login];
192 193 194 195 196 197 198 199 200 201
}

- (IBAction)rememberUsernameBtnChanged:(id)sender {
    [[NSUserDefaults standardUserDefaults] setBool:self.rememberUsernameSwitch.isOn forKey:@"rememberUsername"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (IBAction)rememberPasswordBtnChanged:(id)sender {
    [[NSUserDefaults standardUserDefaults] setBool:self.rememberPasswordSwitch.isOn forKey:@"rememberPassword"];
    [[NSUserDefaults standardUserDefaults] synchronize];
202 203
}
@end