UIWebView Javascript
UIWebView Javascript
我有三个 UITextfields 和一个提交按钮。我已经完成了文本字段的验证。但现在困难的部分来了。单击登录后,我必须将我的文本字段文本发送到 javascript,验证凭据,验证后,我的视图必须移至下一个视图。
- (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;
}
这甚至没有被调用。我什至做了一个简单的函数来生成警报,但即使那样也不起作用..
我在 viewDidLoad() 中设置了 webview 委托,并在 viewDidAppear() 中尝试了它
也是。
注意:我的 Obj C 类 在一个文件夹中,我的 html 和 js 文件在 2 个单独的文件夹中。我无法更改文件夹结构。
一旦 "enableLogin"
变为真,我将重新加载网络视图。
我有三个 UITextfields 和一个提交按钮。我已经完成了文本字段的验证。但现在困难的部分来了。单击登录后,我必须将我的文本字段文本发送到 javascript,验证凭据,验证后,我的视图必须移至下一个视图。
- (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;
}
这甚至没有被调用。我什至做了一个简单的函数来生成警报,但即使那样也不起作用.. 我在 viewDidLoad() 中设置了 webview 委托,并在 viewDidAppear() 中尝试了它 也是。
注意:我的 Obj C 类 在一个文件夹中,我的 html 和 js 文件在 2 个单独的文件夹中。我无法更改文件夹结构。
一旦 "enableLogin"
变为真,我将重新加载网络视图。