Ios AFN网络下载循环丢失身份验证

Ios AFN网络下载循环丢失身份验证,ios,ipad,afnetworking,connectivity,download,Ios,Ipad,Afnetworking,Connectivity,Download,因此,我的情况是,在进入主屏幕之前,我必须在iOS应用程序中下载多个视频文件,我正在使用AFNetworking 2。如果我必须下载一个文件,一切正常,但当我必须下载多个文件时,有些文件会因此错误而失败,它并不总是相同的文件,似乎是随机的..: 错误:Error Domain=com.alamofire.Error.serialization.response Code=-1011“请求失败:未经授权(401)”UserInfo=0x7ff943f4fcb0{NSLocalizedDescrip

因此,我的情况是,在进入主屏幕之前,我必须在iOS应用程序中下载多个视频文件,我正在使用AFNetworking 2。如果我必须下载一个文件,一切正常,但当我必须下载多个文件时,有些文件会因此错误而失败,它并不总是相同的文件,似乎是随机的..:

错误:Error Domain=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];
}
}
MainView*mainViewController=[[MainView alloc]init];
NSURLCredential*credential=[nsurlCredentialCredentialWithUser:AUTH_用户名2密码:AUTH_密码2持久性:NSURLCredentialPersistenceForSession];
NSFileManager*fileManager=[NSFileManager defaultManager];
[fileManager changeCurrentDirectoryPath:self.topVideoPath];
__block int countVideos=0;
__block int totalvideos=[videoItems count];
对于(int i=0;i<[videoItems count];i++){
NSLog(@“dict数组:%@”,self.dictArray);
NSMutableDictionary*itemDict=[self.dictArray objectAtIndex:i];
NSURL*url=[NSURL URLWithString:[itemDict objectForKey:@“视频”];
NSString*videoString=[itemDict objectForKey:@“video”];
NSString*specificPath=[[fileManager currentDirectoryPath]stringByAppendingPathComponent:[NSString stringWithFormat:@“%@.dat”,[itemDict objectForKey:@“id”]];
TopItem*item=[videoItems对象索引:i];
如果(![videoString IsequalString:@“”){
[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*操作=[[AFHTTPRequestOperation alloc]initWithRequest:[NSURLRRequestWithURL:url]];
[操作设置凭证:凭证];
operation.outputStream=[NSOutputStream outputStreamToFileAtPath:moviePath-append:NO];
[operation setDownloadProgressBlock:^(NSUInteger字节读取、long long totalBytesRead、long long totalBytesExpectedToRead){
浮动进度=((浮动)totalBytesRead)/totalBytesExpectedToRead;
NSLog(@“下载进度:%f”,进度);
}];
[操作setCompletionBlockWithSuccess:^(AFHTTPRequestOperation*操作,id响应对象){
countVideos=countVideos+1;
NSLog(@“特定路径:%@”,specificPath);
[NSKeyedArchiver archiveRootObject:item-toFile:specificPath];
如果(countVideos==totalvideos){
mainViewController.topScrollViewContent=self.topScrollViewContent;
if([self.responseDelegate respondsToSelector:@selector(pushController:)])){
[self.responseDelegate performSelector:@selector(pushController:)with object:mainViewController];
}
}
}失败:^(AFHTTPRequestOperation*操作,NSError*错误){
NSLog(@“未下载”);
NSLog(@“错误:%@”,错误);
countVideos=countVideos+1;
}];
[操作启动];
}
}
有人知道出了什么问题吗


提前感谢!

最后我用一个NSOperationque修复了它,maxConcurrentOperationCount=3,所以同一时间只下载了3个视频。可能是服务器的连接太长太多了