- (void)loadData{
// 获得网络管理者
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// 要下载文件的url
NSURL *url = [NSURL URLWithString:@"http://app.fxingw.com/app/index/list.json"];
// 创建请求对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 异步
[[manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
// 告诉服务器下载的文本保存的位置在那里
NSString *homeDictionary = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];//获取根目录
//添加储存的文件名
NSURL *documentsDirectoryURL = [NSURL URLWithString:homeDictionary];
NSLog(@"file = %@",targetPath);
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"response = %@,filePath = %@",response,filePath);
[self loadInfo:filePath.absoluteString];
}] resume];
}
- (void)loadInfo:(NSString *)path{
// 加载json文件路径
// 读取json文件到一个NSData对象
NSData *data = [NSData dataWithContentsOfFile:path];
// 将data转换成一个NSDictionary
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
// 全局 数组
_huandeng = [NSArray arrayWithArray:dict[@"huandeng"]];
_news = [NSArray arrayWithArray:dict[@"newslist"]];
}