IOS 版本是 9,Xcode 7 和以下代码不起作用。但它适用于 IOS 版本 8、Xcode 6。
我正在使用以下代码从服务器下载文件:
NSURLSessionConfiguration *configuration;
if ([[[UIDevice currentDevice] systemVersion] compare:@"8.0" options:NSNumericSearch] != NSOrderedAscending) {
configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"Test"];
} else {
configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"Test"];
}
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://hw13.asset.aparat.com/aparat-video/e361693675a47fa4ae758756b40a11653215108-360p__71758.mp4"]];
NSProgress *progress;
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:&progress destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
NSString *fileAddress = [[appDelegate GetDocumentDirectory] stringByAppendingPathComponent:@"Myfile.mp4"];
return [NSURL fileURLWithPath:fileAddress];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
if(error != nil){
//ERROR
[[NSFileManager defaultManager] removeItemAtPath:[filePath absoluteString]
error:nil];
[progress removeObserver:self forKeyPath:@"fractionCompleted" context:NULL];
[downloadTask cancel];
return ;
}
//SUCCESS
[progress removeObserver:self forKeyPath:@"fractionCompleted" context:NULL];
}];
[downloadTask resume];
[progress addObserver:self
forKeyPath:@"fractionCompleted"
options:NSKeyValueObservingOptionNew
context:NULL];
不断给出错误:
Error Domain=NSURLErrorDomain Code=-1 "unknown error" UserInfo={NSErrorFailingURLKey=http://hw13.asset.aparat.com/aparat-video/e361693675a47fa4ae758756b40a11653215108-360p__71758.mp4, NSLocalizedDescription=unknown error, NSErrorFailingURLStringKey=http://hw13.asset.aparat.com/aparat-video/e361693675a47fa4ae758756b40a11653215108-360p__71758.mp4}
这是GetDocuemntsD
-(NSString *)GetDocumentDirectory{
self.fileMgr = [NSFileManager defaultManager];
self.homeDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
return self.homeDir;
}
*** 我可以将图像保存到文档目录,但我想在后台下载视频文件并向用户显示进度,但它一直失败。
我的 info.plist 有:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>