你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS加載並顯示bundle的.txt文件的方法

iOS加載並顯示bundle的.txt文件的方法

編輯:IOS開發綜合

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

}

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved