ios WebView 打开txt文件乱码问题

      

 UIWebView *web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0,WIDTH , HEIGHT)];

    web.backgroundColor = [UIColor clearColor];

    web.scalesPageToFit = YES;

    web.scrollView.delegate = self;

    web.delegate = self;

    web.userInteractionEnabled = YES;



    

    ///编码可以解决 .txt 中文显示乱码问题
    NSStringEncoding *useEncodeing = nil;

    //带编码头的如utf-8等,这里会识别出来 self.path是文件地址(Users/a1/Library/Developer/CoreSimulator/Devices/62525235-2FB8-40D6-8B16-1F94704B7EEA/data/Containers/Data/Application/635003EB-D704-4EC4-8333-6CF80B5E3FCB/Documents/22237.text)

    NSString *body = [NSString stringWithContentsOfFile:self.path usedEncoding:useEncodeing error:nil];

    //识别不到,按GBK编码再解码一次.这里不能先按GB18030解码,否则会出现整个文档无换行bug。

    if (!body) {

        body = [NSString stringWithContentsOfFile:self.path  encoding:0x80000632 error:nil];

    }

    //还是识别不到,按GB18030编码再解码一次.

    if (!body) {

        body = [NSString stringWithContentsOfFile:self.path encoding:0x80000631 error:nil];

    }

    

    //展现

    if (body) {

        [web loadHTMLString:body baseURL: nil];

    }else {

        NSURL *url = [NSURL fileURLWithPath:self.path];

        NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];

        [web loadRequest:request];

    }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值