// 方法一:
- (void)shownews:(nsdictionary *)news
{
// 1.取出网页内容
nsstring *body = news[@"body"];
// 2.取出图片
nsdictionary *img = [news[@"img"] lastobject];
nsstring *imghtml = [nsstring stringwithformat:@"", 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:@"%@%@", title, time];
html = [nsstring stringwithformat:@"%@%@", header, html];
// 链接mainbundle中的css文件
nsurl *cssurl = [[nsbundle mainbundle] urlforresource:@"news" withextension:@"css"];
html = [nsstring stringwithformat:@"%@", 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:@"" 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的值
用户登录
还没有账号?立即注册
用户注册
投稿取消
| 文章分类: |
|
还能输入300字
上传中....
电脑城出口基地