【问题标题】:UIWebView JavascriptUIWebView Javascript
【发布时间】:2016-11-06 02:18:38
【问题描述】:

我有三个 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 个单独的文件夹中。我无法更改文件夹结构。

【问题讨论】:

  • 你又在哪里使用jsCallBack?我看到你给了它价值,但现在再次使用它
  • @Ricardo Alves jsCallBack 仅用于加载页面,
  • 你有没有尝试在页面中添加js。这样你就可以在按钮上创建事件
  • @Ricardo Alves 你的意思是调用 .js 文件本身?是的,我试过了

标签: javascript ios objective-c uiwebview


【解决方案1】:

一旦"enableLogin" 变为真,我将重新加载 webview。

【讨论】:

    猜你喜欢
    • 2011-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    相关资源
    最近更新 更多