iOS - UIWebView和JavaScript互调

孔城
2023-12-01

一、UIWebView调用JavaScript代码

直接写JavaScript代码

1、只有在webView加载完毕之后在能够调用对应页面中的js方法。

JavaScript代码写在如下webView的代理方法中。

-(void)webViewDidFinishLoad:(UIWebView*)webView;//当网页视图结束加载一个请求之后,得到通知。 

2、与UIWebView进行交互,调用web页面中的需要传参的函数时,参数需要带单引号,或者双引号(双引号需要进行转义在转义字符前加\),在传递json字符串时不需要加单引号或双引号:

-(void)webViewDidFinishLoad:(UIWebView *)webView {

NSString *sendJsStr=[NSString stringWithFormat:@"openFile(\"%@\")",jsDocPathStr];
[webView stringByEvaluatingJavaScriptFromString:sendJsStr];
}

3、调用JavaScript函数只能在函数写完后调用,不能使用window.onload = ‘function()’,不能在页面加载的时候调用。

demo如下:

 - (void)webViewDidFinishLoad:(UIWebView *)webView {
    // 设置p标签字体颜色
    [webView stringByEvaluatingJavaScriptFromString:@"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function SetParaColor() {"
     "var i = 0;"
     "var allP = document.getElementsByTagName('p');"
     "for (i; i < allP.length; i++) {"
     "var p = allP[i];"
     "p.style.color = '#333';"
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"
     ];

    [webView stringByEvaluatingJavaScriptFromString:@"SetParaColor();"];
}

引入JavaScript文件

1、写个test.js放到项目中

2、引入test.js文件到html中

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"js"];  
NSString *jsString = [[NSString alloc] initWithContentsOfFile:filePath];  
[webView stringByEvaluatingJavaScriptFromString:jsString]; 

3、触发test.js中的函数

二、UIWebView调用JavaScript代码

1、在html里面写重定向请求

在JavaScript写上:

href = "$://......."

2、在以下代理方法中截获html里面的重定向请求

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

    NSString *str = [request.URL absoluteString];
    if ([str hasPrefix:@"http"]) {
        NSString *url = [Utils URLDecodedString:str];
        NSArray *params = [url componentsSeparatedByString:@"?"];
        if (params.count > 0) {
            if (params.count >=2 ) {
                NSArray *value = [params[1] componentsSeparatedByString:@"="];
                if ([value[0] isEqualToString:@"name"]) {
                    [HyperlinkClickManager hyperlinkClickWithParam:value[1] type:@"$" delegate:self.contentView.viewController titleStr:@""];
                }else if ([value[0] isEqualToString:@"topicId"]) {
                    [HyperlinkClickManager hyperlinkClickWithParam:value[1] type:@"#" delegate:self.contentView.viewController titleStr:@""];
                }else if ([value[0] isEqualToString:@"clientId"]) {
                    [HyperlinkClickManager hyperlinkClickWithParam:value[1] type:@"#_" delegate:self.contentView.viewController titleStr:@""];
                }else if ([value[0] isEqualToString:@"fid"]) {
                    [HyperlinkClickManager hyperlinkClickWithParam:value[1] type:@"@" delegate:self.contentView.viewController titleStr:@""];
                }else {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
                }
            }else {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
            }

        }else {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
        }
        return NO;
    }
    return YES;
}
 类似资料: