首页 > 移动平台 > 详细

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

时间:2015-01-27 11:05:51      阅读:408      评论:0      收藏:0      [点我收藏+]

- (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];

    

    

}

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

原文:http://blog.csdn.net/darongzi1314/article/details/43192119

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!