- (void)downFileFromServerWithUrlString:(NSString *)urlString{數據庫
NSURL *URL = [NSURL URLWithString:urlString];app
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];async
//AFN3.0+基於封住URLSession的句柄ui
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];atom
//請求url
NSURLRequest *request = [NSURLRequest requestWithURL:URL];spa
//下載Task操做.net
_downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {component
// @property int64_t totalUnitCount; 須要下載文件的總大小orm
// @property int64_t completedUnitCount; 當前已經下載的大小
// 回到主隊列刷新UI
dispatch_async(dispatch_get_main_queue(), ^{
[HUD showAnimated:YES whileExecutingBlock:^{
HUD.progress = 1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount;
} completionBlock:^{
}];
});
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
//- block的返回值, 要求返回一個URL, 返回的這個URL就是文件的位置的路徑
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [cachesPath stringByAppendingPathComponent:response.suggestedFilename];
return [NSURL fileURLWithPath:path];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
// filePath就是你下載文件的位置,你能夠解壓,也能夠直接拿來使用
//下載完成走這個block
if (!error)
{
[HUD removeFromSuperview];
HUD = nil;
//若是請求沒有錯誤(請求成功), 則打印地址
// NSLog(@"%@", filePath);
// NSLog(@"%@",filePath.path);
NSData *data = [NSData dataWithContentsOfURL:filePath];
NSArray *arr = [_downloadTask.response.suggestedFilename componentsSeparatedByString:@"."];
NSString *behindStr = [NSString stringWithFormat:@"%@.%@",STR_OBJ([_wordArr objectAtIndex:_index][@"name"]),STR_OBJ([arr lastObject])];
NSLog(@"%@",_downloadTask.response.suggestedFilename);
//獲取數據寫入文件 寫入文件須要注意每次app啓動沙盒路徑都是動態變化的 若是要作數據庫存儲不能直接存儲完整的文件路徑 只須要存儲本身設定的表示字段 再每次獲取時再動態獲取完整路徑
[self writeToFileWithData:data WithFileName:behindStr];
}else{
[GlobelModel showTipToView:self.view andTipText:error.localizedDescription];
}
// NSString *imgFilePath = [filePath path];// 將NSURL轉成NSString
//
// NSLog(@"%@",imgFilePath);
}];
}
#pragma mark 將數據寫入文件
- (void)writeToFileWithData:(NSData *)data WithFileName:(NSString *)fileName{
//此處首先指定了圖片存取路徑(默認寫到應用程序沙盒 中)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
//並給文件起個文件名
NSString *path=[[paths objectAtIndex:0] stringByAppendingPathComponent:fileName];
NSLog(@"%@",path);
BOOL isSuccess = [data writeToFile:path atomically:NO];
if (isSuccess) {
//寫入成功 存入數據庫
NSDictionary *dic = [_wordArr objectAtIndex:_index];//對於的數據源
NSInteger idIndex = [dic[@"id"] integerValue];
//插入數據
BOOL isSuccess = [[MySqlite sharedInstance].database executeUpdate:@"INSERT INTO WordList (uid, path) VALUES (?,?);",@(idIndex),fileName];
if (isSuccess) {
//插入成功 刷新tableview
[_ruleSearchTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:_index inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
}
}
}