//www.greatytc.com/p/5b3d1933700f 格式有点乱,整理了下
JS代码:遍历网页源码中“img” 节点,并为每个节点添加一个事件。
function setImageClickFunction(){
var imgs = document.getElementsByTagName("img");
for(var i=0;i<imgs.length;i++) {
var src = imgs[i].src;
imgs[i].setAttribute("onClick","getImg(src)");
}
}
function getImg(src){
var url = src;
document.location = url;
}
viewDidLoad
- (void)viewDidLoad{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"//www.greatytc.com/p/944dbb35928e"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.myWebView loadRequest:request];
self.myWebView.delegate = self;
//加载js文件
NSString *jsStr = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"Findimg" withExtension:@"js"] encoding:NSUTF8StringEncoding error:nil];
[self.myWebView stringByEvaluatingJavaScriptFromString:jsStr];
}
delegate:
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSLog(@"webViewDidFinishLoad");
[webView stringByEvaluatingJavaScriptFromString:@"setImageClickFunction()"];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSString* path = [[request URL] absoluteString];
NSLog(@"path: %@",path);
//添加关键字进行是否跳转的筛选
if ([path rangeOfString:@"jpg"].location != NSNotFound){
ImgShowViewController* img = [[ImgShowViewController alloc] init];
[self presentViewController:img animated:YES completion:nil];
return NO;
}else{
return YES;
}
}
在完成的时候,stringByEvaluatingJavaScriptFromString
方法嵌入页面中
最后用于得到uiwebview所有的链接,yes no 决定是否跳转,判断条件可以用jpg,png后缀做判断
cell.selectionStyle=UITableViewCellSelectionStyleNone;