// // ViewController.m // headlessbrower_test1 // // Created by PLUC SeniorDesign on 2/23/15. // Copyright (c) 2015 PLUC SeniorDesign. All rights reserved. // #import "ViewController.h" #import "GlobalWebController.h" @interface ViewController () @property UIWebView *webview; @end @implementation ViewController bool webview_loaded; - (void)viewDidLoad { [super viewDidLoad]; GlobalWebController *globalWeb = [GlobalWebController getInstance]; if (!globalWeb.isInitialized) { self.webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)]; globalWeb.web = self.webview; globalWeb.isInitialized = true; //[self.webview // loadRequest:[NSURLRequest // requestWithURL: // [NSURL URLWithString:@"http://192.168.1.131/rti/"]]]; // Test collection page on mac mini desktop [self.webview loadRequest:[NSURLRequest requestWithURL: [NSURL URLWithString:@"file:///Users/plucseniordesign/Desktop/RTISetup.html"]]]; [self.webview setDelegate:self]; } } -(void) viewWillAppear:(BOOL)animated { [self.view addSubview:self.webview]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)webViewDidFinishLoad:(UIWebView *)webView { // Strictly for debugging purposes [webView stringByEvaluatingJavaScriptFromString:@"var test1 = 'Hello Pluto'"]; NSString *newHtml = [webView stringByEvaluatingJavaScriptFromString:@"test1"]; NSLog(@"%@\n", newHtml); NSString *path = [[NSBundle mainBundle] pathForResource:@"WhatPage" ofType:@"js"]; NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; NSString *page = [webView stringByEvaluatingJavaScriptFromString:content]; NSLog(@"PAGE NUMBER IS: %@\n", page); if ([page isEqual:@"1"]) { // Setup Page // Get collections available - Testing GetAvailableCollections NSString *getAvailablePath = [[NSBundle mainBundle] pathForResource:@"GetAvailableCollections" ofType:@"js"]; NSString *getAvailable = [NSString stringWithContentsOfFile:getAvailablePath encoding:NSUTF8StringEncoding error:nil]; NSString *available = [webView stringByEvaluatingJavaScriptFromString:getAvailable]; UIAlertView *collectionAlert = [[UIAlertView alloc] initWithTitle:@"Collections Available" message:available delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [collectionAlert show]; // Get selected collections NSString *selectedPath = [[NSBundle mainBundle] pathForResource:@"GetSelectedCollections" ofType:@"js"]; NSString *selectedFunc = [NSString stringWithContentsOfFile:selectedPath encoding:NSUTF8StringEncoding error:nil]; NSString *selected = [webView stringByEvaluatingJavaScriptFromString:selectedFunc]; UIAlertView *selectedAlert = [[UIAlertView alloc] initWithTitle:@"Collections Selected" message:selected delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [selectedAlert show]; } else if ([page isEqual:@"2"]) { // Scan Page // Get scanned codes - Testing GetScanned NSString *getScanPath = [[NSBundle mainBundle] pathForResource:@"GetScanned" ofType:@"js"]; NSString *getScan = [NSString stringWithContentsOfFile:getScanPath encoding:NSUTF8StringEncoding error:nil]; NSString *scanned = [webView stringByEvaluatingJavaScriptFromString:getScan]; self.bookLog.text = scanned; UIAlertView *previousScan = [[UIAlertView alloc] initWithTitle:@"Previous Scans" message:scanned delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [previousScan show]; } else if ([page isEqual:@"3"]) { // Error/Action Page // Get the error - Testing GetError NSString *getErrorPath = [[NSBundle mainBundle] pathForResource:@"GetError" ofType:@"js"]; NSString *getError = [NSString stringWithContentsOfFile:getErrorPath encoding:NSUTF8StringEncoding error:nil]; NSString *error = [webView stringByEvaluatingJavaScriptFromString:getError]; // Continue - Testing GoToScan NSString *goToScanPath = [[NSBundle mainBundle] pathForResource:@"GoToScan" ofType:@"js"]; NSString *goToScan = [NSString stringWithContentsOfFile:goToScanPath encoding:NSUTF8StringEncoding error:nil]; [self.webview stringByEvaluatingJavaScriptFromString:goToScan]; UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error Encountered" message:error delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [errorAlert show]; } } - (IBAction)loginBtnPressed:(id)sender { NSString *path = [[NSBundle mainBundle] pathForResource:@"WhatPage" ofType:@"js"]; NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; NSString *page = [self.webview stringByEvaluatingJavaScriptFromString:content]; NSLog(@"PAGE NUMBER IS: %@\n", page); if ([page isEqual:@"0"]) { // Login Page NSString *loginPath = [[NSBundle mainBundle] pathForResource:@"Login" ofType:@"js"]; NSString *login = [NSString stringWithContentsOfFile:loginPath encoding:NSUTF8StringEncoding error:nil]; NSString *success = [self.webview stringByEvaluatingJavaScriptFromString:login]; UIAlertView *loginAlert = [[UIAlertView alloc] initWithTitle:@"Logged In!" message:success delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [loginAlert show]; } } - (IBAction)scanBtnPressed:(id)sender { NSString *path = [[NSBundle mainBundle] pathForResource:@"WhatPage" ofType:@"js"]; NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; NSString *page = [self.webview stringByEvaluatingJavaScriptFromString:content]; NSLog(@"PAGE NUMBER IS: %@\n", page); if ([page isEqual:@"2"]) { // Scan Page // Get scanned codes - Testing GetScanned NSString *barcode = self.barcodeField.text; if (barcode.length != 14) { UIAlertView *noBarcode = [[UIAlertView alloc] initWithTitle:@"Invalid Barcode" message:@"Barcode ain't 14 characters" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [noBarcode show]; } else { NSString *stringStart = @"var code = '"; NSString *stringEnd = @"';"; NSString *newString = [stringStart stringByAppendingString:barcode]; NSString *inputCode = [newString stringByAppendingString:stringEnd]; NSString *scanPath = [[NSBundle mainBundle] pathForResource:@"ScanBarcode" ofType:@"js"]; NSString *scan = [NSString stringWithContentsOfFile:scanPath encoding:NSUTF8StringEncoding error:nil]; [self.webview stringByEvaluatingJavaScriptFromString:inputCode]; [self.webview stringByEvaluatingJavaScriptFromString:scan]; // Get scanned codes - Testing GetScanned NSString *getScanPath = [[NSBundle mainBundle] pathForResource:@"GetScanned" ofType:@"js"]; NSString *getScan = [NSString stringWithContentsOfFile:getScanPath encoding:NSUTF8StringEncoding error:nil]; NSString *scanned = [self.webview stringByEvaluatingJavaScriptFromString:getScan]; self.bookLog.text = scanned; UIAlertView *previousScan = [[UIAlertView alloc] initWithTitle:@"Previous Scans" message:scanned delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [previousScan show]; } } } - (IBAction)continueOnError:(id)sender { NSString *path = [[NSBundle mainBundle] pathForResource:@"WhatPage" ofType:@"js"]; NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; NSString *page = [self.webview stringByEvaluatingJavaScriptFromString:content]; NSLog(@"PAGE NUMBER IS: %@\n", page); if ([page isEqual:@"3"]) { // Error/Action Page // Continue - Testing GoToScan NSString *goToScanPath = [[NSBundle mainBundle] pathForResource:@"GoToScan" ofType:@"js"]; NSString *goToScan = [NSString stringWithContentsOfFile:goToScanPath encoding:NSUTF8StringEncoding error:nil]; [self.webview stringByEvaluatingJavaScriptFromString:goToScan]; } } - (IBAction)clickedFinish:(id)sender { NSString *path = [[NSBundle mainBundle] pathForResource:@"WhatPage" ofType:@"js"]; NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; NSString *page = [self.webview stringByEvaluatingJavaScriptFromString:content]; NSLog(@"PAGE NUMBER IS: %@\n", page); // Test FinishScan if ([page isEqual:@"2"]) { // Scan Page NSString *finishPath = [[NSBundle mainBundle] pathForResource:@"FinishScan" ofType:@"js"]; NSString *finish = [NSString stringWithContentsOfFile:finishPath encoding:NSUTF8StringEncoding error:nil]; NSString *isFinished = [self.webview stringByEvaluatingJavaScriptFromString:finish]; if ([isFinished isEqual:@"-1"]) { } else { NSLog(@"FINISHED NOT FAILED\n"); } } } - (IBAction)clickedSetup:(id)sender { NSString *path = [[NSBundle mainBundle] pathForResource:@"WhatPage" ofType:@"js"]; NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; NSString *page = [self.webview stringByEvaluatingJavaScriptFromString:content]; NSLog(@"PAGE NUMBER IS: %@\n", page); if ([page isEqual:@"1"]) { // Get selected collections NSString *selectedPath = [[NSBundle mainBundle] pathForResource:@"GetSelectedCollections" ofType:@"js"]; NSString *selectedFunc = [NSString stringWithContentsOfFile:selectedPath encoding:NSUTF8StringEncoding error:nil]; NSString *selected = [self.webview stringByEvaluatingJavaScriptFromString:selectedFunc]; UIAlertView *selectedAlert = [[UIAlertView alloc] initWithTitle:@"Collections Selected" message:selected delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [selectedAlert show]; // Testing AddCollection // NSString *addSelectedPath = // [[NSBundle mainBundle] pathForResource:@"AddCollection" // ofType:@"js"]; // NSString *addSelected = // [NSString stringWithContentsOfFile:addSelectedPath // encoding:NSUTF8StringEncoding // error:nil]; // // [self.webview // stringByEvaluatingJavaScriptFromString:@"var selectedAdd = 49;"]; // NSString *add = // [self.webview // stringByEvaluatingJavaScriptFromString:addSelected]; // // if ([add isEqual:nil]) { // NSLog(@"nil failed"); // } else { // NSLog(@"NOT NILL UNFAILED"); // } // Testing RemoveCollection // NSString *removeSelectedPath = // [[NSBundle mainBundle] pathForResource:@"RemoveCollection" // ofType:@"js"]; // NSString *removeSelected = // [NSString stringWithContentsOfFile:removeSelectedPath // encoding:NSUTF8StringEncoding // error:nil]; // // [self.webview // stringByEvaluatingJavaScriptFromString:@"var selectedRemove = // 0;"]; // NSString *remove = // [self.webview // stringByEvaluatingJavaScriptFromString:removeSelected]; // // Testing Save SetUp // NSString *saveSetupPath = // [[NSBundle mainBundle] pathForResource:@"SaveSetup" ofType:@"js"]; // NSString *saveSetup = // [NSString stringWithContentsOfFile:saveSetupPath // encoding:NSUTF8StringEncoding // error:nil]; // // [self.webview stringByEvaluatingJavaScriptFromString:saveSetup]; } } @end