下面是编程之家 jb51.cc 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
webView加载网页的时候,有的时候会有广告,去除网页中的广告// 方法一: - (void)showNews:(NSDictionary *)news { // 1.取出网页内容 NSString *body = news[@"body"]; // 2.取出图片 NSDictionary *img = [news[@"img"] lastObject]; NSString *imgHTML = [NSString stringWithFormat:@"<img src=\"%@\" width=\"300\" height=\"171\">",img[@"src"]]; // 2.创建一个webView,显示网页 UIWebView *webView = [[UIWebView alloc] init]; webView.frame = self.view.bounds; [self.view addSubview:webView]; // 3.拼接网页内容 NSString *html = [body stringByReplacingOccurrencesOfString:img[@"ref"] withString:imgHTML]; // 4.取出新闻标题 NSString *title = news[@"title"]; // 5.取出新闻的时间 NSString *time = news[@"ptime"]; // 头部内容 NSString *header = [NSString stringWithFormat:@"<div class=\"title\">%@</div><div class=\"time\">%@</div>",title,time]; html = [NSString stringWithFormat:@"%@%@",header,html]; // 链接mainBundle中的CSS文件 NSURL *cssURL = [[NSBundle mainBundle] URLForResource:@"news" withExtension:@"css"]; html = [NSString stringWithFormat:@"%@<link rel=\"stylesheet\" href=\"%@\">",html,cssURL]; // 5.加载网页内容 [webView loadHTMLString:html baseURL:nil]; } // 方法二:火狐浏览器:在要去掉的广告上,右键”查看元素“,将代码粘贴到要替换的字符串处 NSString *url = @"http://ibaby.ipadown.com/api/food/food.show.detail.php?id="; NSString *webSite = [url stringByAppendingString:self.Idtext]; NSURL *siteURL= [NSURL URLWithString:webSite]; NSURLRequest *request = [NSURLRequest requestWithURL:siteURL]; // 加载请求 // [self.webView.web loadRequest:request]; dispatch_async(dispatch_get_global_queue(0,0),^{ NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data,NSURLResponse * _Nullable response,NSError * _Nullable error) { self.htmlAbosluteString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; self.htmlAbosluteString = [self.htmlAbosluteString stringByReplacingOccurrencesOfString:@"<img width=\"0\" height=\"0\" style=\"width: 803px; height: 121px;\" src=\"http://ubmcmm.baidustatic.com/media/v1/0f0007y3CMO4YsUwoxXbB6.jpg\"></img>" withString:@""]; // 这是要去掉广告的第二个地方 self.htmlAbosluteString = [self.htmlAbosluteString stringByReplacingOccurrencesOfString:str9 withString:@""]; dispatch_async(dispatch_get_main_queue(),^{ [self.webView.web loadHTMLString:self.htmlAbosluteString baseURL:nil]; }); }]; [dataTask resume]; CGRect tmpFrame = self.webView.web.frame; CGFloat h = self.navigationController.navigationBar.frame.size.height + 20; tmpFrame.origin.y -= h; self.webView.web.frame = tmpFrame; }); // 方法三: NSString *str = @".jpg\" height=\"200\" width=\"340\"/>"; - (NSString *)transformMessage:(NSString *)message withString:(NSString *)string { NSString *messageCopy = message; NSScanner *theScanner; NSString *text = nil; theScanner = [NSScanner scannerWithString:messageCopy]; while ([theScanner isAtEnd] == NO) { // find start of tag [theScanner scanUpToString:@".jpg" intoString:NULL] ; // find end of tag if ([theScanner scanUpToString:@">" intoString:&text] || [theScanner scanUpToString:@"/>" intoString:&text]) { // .jpg" height="200" width="340"/> // 如果不需要第二个参数 : // 可以将string替换为 [NSString stringWithFormat:@".jpg\" height=\\\"200\" width=\"340\"/>"] if (text != nil) { messageCopy = [messageCopy stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:string]; NSLog(@"%@",messageCopy); break; } } } return messageCopy; } 作用是把start tag和end tag 之间替换成string的值
以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。