Intermodulation source of oc and jsObjective-C
一、删除网页中对应的标签:OC调用JS代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
- ( void )webViewDidFinishLoad:(UIWebView *)webView
{
NSMutableString *JSStM = [ NSMutableString string];
[JSStM appendString: @"var headerTag = document.getElementsByTagName('header')[0];headerTag.parentNode.removeChild(headerTag);" ];
[JSStM appendString: @"var buyTag = document.getElementsByClassName('buy-now')[0];buyTag.parentNode.removeChild(buyTag);" ];
[JSStM appendString: @"var footerBtnTag = document.getElementsByClassName('footer-btn-fix')[0]; footerBtnTag.parentNode.removeChild(footerBtnTag);" ];
[JSStM appendString: @"var footerTag = document.getElementsByClassName('footer')[0]; footerTag.parentNode.removeChild(footerTag);" ];
[JSStM appendString: @"var figureTag = document.getElementsByTagName('figure')[0].children[0]; figureTag.onclick = function(){window.location.href = 'xg://www.hahah.com'};" ];
[webView stringByEvaluatingJavaScriptFromString:JSStM];
}
|
二、通过网页中的JS代码跳转到控制器界面:JS调用OC的代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
- ( BOOL )webView:(UIWebView *)webView shouldStartLoadWithRequest:( NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog ( @"%@" ,request.URL.absoluteString);
NSString *URLString = request.URL.absoluteString;
NSRange range = [URLString rangeOfString: @"xg://?src=" ];
if ([URLString isEqualToString: @"xg://www.hahah.com" ]){
NSLog ( @"你点击的是图片" );
[ self .navigationController popToRootViewControllerAnimated: YES ];
return NO ;
}
return YES ;
}
|
|