iOS加载并显示bundle的.txt文件的方法

本文档介绍如何在iOS应用中加载并显示.bundle资源目录下的.txt文件,通过使用UIWebView和NSStringEncoding解决中文乱码问题,并对加载的内容进行处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

- (void)jiazaitxt{

    self.myWebview = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height+60)];

//    myWebView.delegate = self;

    //NSString   *path = [NSString stringWithFormat:@"%@/Documents/附件/%@",NSHomeDirectory(),self.wendangnaem];

    NSString *txtPath=[[NSBundle mainBundle]pathForResource:@"city_list" ofType:@"txt"];

    NSLog(@"txtPath:%@",txtPath);

    

    ///编码可以解决 .txt 中文显示乱码问题

    NSStringEncoding *useEncodeing = nil;

    //带编码头的如utf-8等,这里会识别出来

    NSString *body = [NSString stringWithContentsOfFile:txtPath usedEncoding:useEncodeing error:nil];

    

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

    if (!body) {

        body = [NSString stringWithContentsOfFile:txtPath encoding:0x80000632 error:nil];

        NSLog(@"%@",body);

    }

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

    if (!body) {

        body = [NSString stringWithContentsOfFile:txtPath encoding:0x80000631 error:nil];

        NSLog(@"%@",body);

    }

    //展现

    if (body) {

        NSLog(@"%@",body);

        

        NSData *data=[body dataUsingEncoding:NSUTF8StringEncoding];

        NSLog(@"data:%@",data);

        

        id userData=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

        if (userData) {

            if ([userData isKindOfClass:[NSDictionary class]])

            {

                /*

                 {"code":"000000","memo":"成功",

                 "result":

                 {"domestic":

                 [{"cityId":"110100","cityName":"北京市","region":[{"regionId":"110101","regionName":"北京市-东城区"},{"regionId":"110102","regionName":"北京市-西城区"},{"regionId":"110103","regionName":"北京市-崇文区"},{"regionId":"110105","regionName":"北京市-朝阳区"},{"regionId":"110106","regionName":"北京市-丰台区"},

                 */

                NSArray *domesticArray=[(NSDictionary *)userData objectForKey:@"domestic"];

                for (NSDictionary *dict in domesticArray)

                {

                    QSSMyMessageNowCityModel *model=[[QSSMyMessageNowCityModel alloc]init];

                    

                }

            }

        }

        

        

        [self.myWebview loadHTMLString:body baseURL: nil];

    }else {

        NSString *urlString = [[NSBundle mainBundle] pathForAuxiliaryExecutable:txtPath];

        urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        NSURL *requestUrl = [NSURL URLWithString:urlString];

        NSLog(@"%@",urlString);

        NSURLRequest *request = [NSURLRequest requestWithURL:requestUrl];

        [self.myWebview loadRequest:request];

    }

    [self addSubview:self.myWebview];

    

    

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值