【发布时间】:2014-10-08 07:18:06
【问题描述】:
因此,在进入主屏幕之前,我确实在 iOS 应用程序中下载了多个视频文件,并且为此使用了 AFNetworking 2。如果我必须下载一个文件,一切都很好,但是当我必须下载多个文件时,一些文件会因此错误而失败,它并不总是相同的文件,它似乎是随机的.. :
错误:错误域=com.alamofire.error.serialization.response Code=-1011 “请求失败:未经授权 (401)” UserInfo=0x7ff943f4fcb0 {NSLocalizedDescription=请求失败:未经授权 (401),com.alamofire.serialization。 response.error.response=
假设我正在使用此代码:
MainView *mainViewController = [[MainView alloc] init];
NSURLCredential *credential = [NSURLCredential credentialWithUser:AUTH_USERNAME2 password:AUTH_PASSWORD2 persistence:NSURLCredentialPersistenceForSession];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager changeCurrentDirectoryPath:self.topVideoPath];
__block int countVideos = 0;
__block int totalvideos = [videoItems count];
for (int i = 0; i < [videoItems count]; i++){
NSLog(@"dict array : %@" , self.dictArray);
NSMutableDictionary *itemDict = [self.dictArray objectAtIndex:i];
NSURL *url = [NSURL URLWithString:[itemDict objectForKey:@"video"]];
NSString *videoString = [itemDict objectForKey:@"video"];
NSString *specificPath = [[fileManager currentDirectoryPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.dat", [itemDict objectForKey:@"id"]]];
TopItem *item = [videoItems objectAtIndex:i];
if (![videoString isEqualToString:@""]) {
[fileManager changeCurrentDirectoryPath:self.topVideoPath];
NSString *moviePath = [[fileManager currentDirectoryPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"Movie%@.mp4", [itemDict objectForKey:@"id"]]];
NSString *correctMoviePath = [NSString stringWithFormat:@"KyoskSkeleton/TopVideo/Movie%@.mp4", [itemDict objectForKey:@"id"]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:url]];
[operation setCredential:credential];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:moviePath append:NO];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
float progress = ((float)totalBytesRead) / totalBytesExpectedToRead;
NSLog(@"The downloading progress : %f", progress);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
countVideos = countVideos + 1;
NSLog(@"specific path : %@", specificPath);
[NSKeyedArchiver archiveRootObject:item toFile:specificPath];
if (countVideos == totalvideos ){
mainViewController.topScrollViewContent = self.topScrollViewContent;
if ([self.responseDelegate respondsToSelector:@selector(pushController:)]) {
[self.responseDelegate performSelector:@selector(pushController:) withObject:mainViewController];
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Not Downloaded");
NSLog(@"Error : %@", error);
countVideos = countVideos + 1;
}];
[operation start];
}
}
有人知道怎么回事吗?
提前致谢!
【问题讨论】:
标签: ios ipad afnetworking connectivity download