在开发项目时,用到了UIWebView,还用到了内嵌的html页面,用到了很多的js方面的东西,oc与js进行交互,同时还用到了重定向原理,这里做一下总结:
(1)使用到的内嵌html页面,我们这里要导入html页面到工程中。由于返回的json数据里包含有网页格式,我们需要对关键字替换
//数据解析
-(void)analyzeJSONData:(NSDictionary *)jsonDic{
newsDetailInfo=[Tools parseForNewsDetailInfoJSONString:jsonDic];
[newsDetailInfo retain];
if (newsDetailInfo.appInfo!=nil) {
newsAppInfo=[Tools parseForNewsAppInfoJSONString:newsDetailInfo.appInfo];
[newsAppInfo retain];
if (IS_PHONE) {
[self replaceJSONDataWithHTML:STRATEGY_DETAIL_FILENAME];
}else{
[self replaceJSONDataWithHTML:STRATEGY_DETAIL_FILENAME_IPD];
}
}else{
if (IS_PHONE) {
[self replaceJSONDataWithHTML:NEWS_DETAIL_FILENAME];
}else{
[self replaceJSONDataWithHTML:NEWS_DETAIL_FILENAME_IPAD];
}
}
}
//替换
-(void)replaceJSONDataWithHTML:(NSString *)fileName{
NSError *error;
_fileContents=[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:@"html"] encoding:NSUTF8StringEncoding error:&error];
NSString *resultHTMLString;
resultHTMLString=[_fileContents stringByReplacingOccurrencesOfString:@"[title]" withString:newsDetailInfo.title];
resultHTMLString=[resultHTMLString stringByReplacingOccurrencesOfString:@"[modifyTime]" withString:newsDetailInfo.modifyTime];
resultHTMLString=[resultHTMLString stringByReplacingOccurrencesOfString:@"[commentCount]" withString:[NSString stringWithFormat:@"%d",newsDetailInfo.commentCount]];
resultHTMLString=[resultHTMLString stringByReplacingOccurrencesOfString:@"[author]" withString:newsDetailInfo.author];
if (pageNum==1) {
resultHTMLString=[resultHTMLString stringByReplacingOccurrencesOfString:@"[summary]" withString:newsDetailInfo.summary];
contentString=newsDetailInfo.summary;
}else{
resultHTMLString=[resultHTMLString stringByReplacingOccurrencesOfString:@"[summary]" withString:_remainResponseString];
}
if ([fileName isEqualToString:STRATEGY_DETAIL_FILENAME] || [fileName isEqualToString:STRATEGY_DETAIL_FILENAME_IPD]) {
resultHTMLString=[resultHTMLString stringByReplacingOccurrencesOfString:@"[icon]" withString:newsAppInfo.iconURL];
resultHTMLString=[resultHTMLString stringByReplacingOccurrencesOfString:@"[appName]" withString:newsAppInfo.name];
resultHTMLString=[resultHTMLString stringByReplacingOccurrencesOfString:@"[version]" withString:newsAppInfo.version];
resultHTMLString=[resultHTMLString stringByReplacingOccurrencesOfString:@"[size]" withString:newsAppInfo.size];
resultHTMLString=[resultHTMLString stringByReplacingOccurrencesOfString:@"[language]" withString:newsAppInfo.language];
}
resultHTMLString=[resultHTMLString stringByReplacingOccurrencesOfString:@" " withString:@""];
resultHTMLString=[resultHTMLString stringByReplacingOccurrencesOfString:@"<img" withString:@"<br><img"];
resultHTMLString=[resultHTMLString stringByReplacingOccurrencesOfString:@"<embed" withString:@"<br><embed"];
if (IS_PHONE) {
resultHTMLString=[resultHTMLString stringByReplacingOccurrencesOfString:@"width=" withString:@"width2="];
resultHTMLString=[resultHTMLString stringByReplacingOccurrencesOfString:@"height=" withString:@"height2="];
}
resultHTMLString=[resultHTMLString stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
resultHTMLString=[resultHTMLString stringByReplacingOccurrencesOfString:@"\t" withString:@" "];
resultHTMLString=[resultHTMLString stringByReplacingOccurrencesOfString:@"<br />" withString:@" "];
resultHTMLString=[resultHTMLString stringByReplacingOccurrencesOfString:@"<br>" withString:@" "];
for (NSString *str