效果如下:
废话不多说,直接上代码
- (NSString *)jsFunction { return @"function sendCommand(cmd,param){var url='testapp:'+cmd+':'+param;document.location = url;}";}
-(void)parseData{ NSString *tempStrring =[self htmlEntityDecode:_info.content]; NSString *strring =[NSString stringWithFormat:@"%",tempStrring]; NSMutableString *contentstr=[NSMutableString stringWithString:strring]; NSString *puttimestr=[NSString stringWithFormat:@"%@ %@",_info.newsOrigin,_info.puttime]; [contentstr insertString:puttimestr atIndex:0]; NSString *titlestr=[NSString stringWithFormat:@"%@",_info.title]; [contentstr insertString:titlestr atIndex:0];
NSString *titlestr1 = [NSString stringWithFormat:@"猜你喜欢"];
[contentstr appendString:titlestr1];
for (NSDictionary *item in _info.newslab) {
NSString *titlestr2 = [NSString stringWithFormat:@"%@ · %@%@",[self jsFunction],[NSString stringWithFormat:@"sendCommand(\"wangzhan\",this.children[1].innerHTML)"],[item valueForKey:@"news_title"],[item valueForKey:@"news_id"]];
[contentstr appendString:titlestr2];
}
webView1=[[UIWebView alloc]init];
webView1.delegate = self;
webView1.backgroundColor=[UIColor whiteColor];
NSString *htmlpath=[[NSBundle mainBundle]pathForResource:@"Content" ofType:@"html"];
[webView1 loadHTMLString:contentstr baseURL:[NSURL fileURLWithPath:htmlpath]];
webView1.frame=CGRectMake(0,44, DEXx, DEXy - 44); [webView1 setDelegate:self];
// 取消滚动条和背景
for (UIView *aView in [webView1 subviews]) {
if ([aView isKindOfClass:[UIScrollView class]]) {
[(UIScrollView *)aView setShowsVerticalScrollIndicator:NO];
//右侧的滚动条 (水平的类似)
for (UIView *shadowView in aView.subviews) {
if ([shadowView isKindOfClass:[UIImageView class]]) { shadowView.hidden = YES;
//上下滚动出边界时的黑色的图片 也就是拖拽后的上下阴影
}
}
}
}
[self.view addSubview:webView1];}
// ********** 字符串转义 *************
-(NSString *)htmlEntityDecode:(NSString *)string{ string = [string stringByReplacingOccurrencesOfString:@"&" withString:@"&"]; //中文换行 string = [string stringByReplacingOccurrencesOfString:@" withString:@"
"];
//英文换行 string = [string stringByReplacingOccurrencesOfString:@"
" withString:@""];
//字体加粗 string = [string stringByReplacingOccurrencesOfString:@"" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@"" withString:@""];
//字体 斜体 string = [string stringByReplacingOccurrencesOfString:@"" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@"" withString:@""]; string = [string stringByReplacingOccurrencesOfString:@"<" withString:@"<"]; string = [string stringByReplacingOccurrencesOfString:@">" withString:@">"]; //双引号 string = [string stringByReplacingOccurrencesOfString:@""" withString:@"'"]; string = [string stringByReplacingOccurrencesOfString:@""" withString:@"'"]; string=[string stringByReplacingOccurrencesOfString:@"img" withString:@"img style='width:310px;'"]; string=[string stringByReplacingOccurrencesOfString:@"height" withString:@""]; string = [string stringByReplacingOccurrencesOfString:@" " withString:@" "]; return string;}#pragma mark -- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSString *requestString = [[request URL] absoluteString];//获取请求的绝对路径. NSArray *components = [requestString componentsSeparatedByString:@":"];//提交请求时候分割参数的分隔符 if ([components count] > 1 && [(NSString *)[components objectAtIndex:0] isEqualToString:@"testapp"]) {
//过滤请求是否是我们需要的.不需要的请求不进入条件
if([(NSString *)[components objectAtIndex:1] isEqualToString:@"wangzhan"]) {
NSString *message = (NSString *)[components objectAtIndex:2];
WZInfomationFileInfo *subFile = [[WZInfomationFileInfo alloc] init];
subFile.ID = message;
WZInfomationDetilVC *detilVC = [[WZInfomationDetilVC alloc] init]; detilVC.info = subFile;
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:detilVC];
[self presentViewController:nav animated:YES completion:nil];
}
return NO;
}
return YES;}
谢谢支持!!!
网友评论