Managing three UITextfields along with a submit button can be challenging when dealing with validations. However, the real challenge arises when trying to send the text from these fields to JavaScript for credential validation before transitioning to the next view.
- (IBAction) login: (id) sender
{
if (enableLogin == YES) {
// [self loadNextView];
}
}
-(void)loadNextView {
appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
if (webViewDidFinishLoadBool == YES) {
[appDelegate loginUser: txtfield1.text];
NSLog(@"loadNextView called");
} else NSLog(@"loadNextView not called");
}
-(void)sendToJS {
// NSString* htmlPath = [[NSBundle mainBundle] pathForResource:@"pathtohtml" ofType:@"html"];
// NSString* appHtml = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
// NSURL *baseURL = [NSURL fileURLWithPath:htmlPath];
// [self.webview loadHTMLString:appHtml baseURL:baseURL];
NSURLRequest *myReq = [NSURLRequest requestWithURL:
[NSURL URLWithString:
[NSString stringWithFormat:@"pathtohtml.html"]]];
[self.webview loadRequest:myReq];
if (self.webview != nil) {
jsCallBack = [NSString stringWithFormat: @"loginUser.login('%@', '%@', '%@');", txtfield1.text, txtfield2.text, txtfield3.text];
}
}
-(void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(@"js loaded");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.webview stringByEvaluatingJavaScriptFromString:jsCallBack];
}
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSString *requestString = [[request URL] absoluteString];
NSArray *components = [requestString componentsSeparatedByString:@":"];
if ([components count] > 1 &&
[(NSString *)[components objectAtIndex:0] isEqualToString:@"jscall"]) {
if([(NSString *)[components objectAtIndex:1] isEqualToString:@"myapp"])
{
NSString *urlString = (NSString *)[components objectAtIndex:2];
NSLog(@"URL STRING %@", urlString);
if ([urlString isEqual: @"OK"]) {
globalValue = YES;
webViewDidFinishLoadBool = YES;
[self loadNextView];
NSLog(@"is called");
} else NSLog("is not called");
}
return NO;
}
return YES;
}
Despite thorough testing and setting delegates correctly, the functionality seems to be unreachable. Even a simple alert generating function failed to work as expected.
Please note that the folder structure cannot be altered; Obj C classes are in one folder while HTML and JS files reside in separate locations.