用于测试 在本地搭建了Apache 服务器
成功实现 从服务器下载文件
#pragma mark -文件下载-
-(void)downloadFile:(NSString *)urlStr{
AFURLSessionManager *manage = [[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
//存在中文 无法转成URL 需要转码
NSString *url = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//创建一个请求
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
__autoreleasing NSProgress *progress = nil;
NSURLSessionDownloadTask *task = [manage downloadTaskWithRequest:request progress:&progress destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSLog(@"targetPath%@",targetPath);
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/代码.zip"];
return [NSURL fileURLWithPath:path];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
if (error) {
NSLog(@"error%@",error);
}else{
NSLog(@"%@",filePath);
}
}];
[task resume];
_progress = progress;
//kvo
[_progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:nil];
}