清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
// 方法一:
- (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的值