webView中使用内嵌html的总结

    在开发项目时,用到了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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值