Swift Swift - 1 year ago 182
Objective-C Question

UIWebView Javascript

I have three UITextfields and a submit button. I have done the validations of the textfields. But the hard part comes now. After clicking on login I have to send my textfields text to javascript, validate the credentials and after validating, my view has to move 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;

This is not even getting called. I even made a simple function which generates an alert but even that doesn't work..
I have set the webview delegate in viewDidLoad() and tried it in viewDidAppear()

Note: My Obj C classes are in one folder and my html and js files are in 2 separate folders.. I can't change the folder structure.

Answer Source

I am reloading the webview once "enableLogin" becomes true.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download