iOS_UIWebView加载Html乱码文件

现象:使用WKWebView加载乱码Html文件,可以显示出来,但是页面显示字体比较小,不符合预期的效果,换用UIWebView对乱码Html文件进行加载,但是由于是乱码文件,直接进行文件加载,显示出来的效果是乱码。
结果方案如下

UIWebView *view = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, BXScreenW, BXScreenH)];

// 此处,我是加载本地的文件
NSString *path = [[NSBundle mainBundle] pathForResource:@"隐私条款" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:path];

NSStringEncoding * usedEncoding = nil;
NSString *body = [NSString stringWithContentsOfURL:url usedEncoding:usedEncoding error:nil];
if (!body){//如果之前不能解码,现在使用GBK解码
    NSLog(@"GBK解码");
    body = [NSString stringWithContentsOfURL:url encoding:0x80000632 error:nil];
}
if (!body) {//再使用GB18030解码
    NSLog(@"GBK18030解码");
    body = [NSString stringWithContentsOfURL:url encoding:0x80000631 error:nil];
}
if (body) {
    [view loadHTMLString:body baseURL:nil];
}else {
    NSLog(@"没有合适的编码");
 }
 [self.view addSubview:view];

猜你喜欢

转载自blog.csdn.net/FlyingKuiKui/article/details/101031367