2013-04-17 3 views
-1

동시에 서버에서 여러 파일을 다운로드해야합니다. 현재 한 번에 하나의 비디오를 다운로드하고 있습니다. 아래는 같은 코드입니다. 이제 여러 비디오를 동시에 다운로드하고 진행중인 모든 다운로드에 대해 별도의 진행률 표시 줄을 유지해야합니다. 그리고이 코드는 큰 비디오를 다운로드하거나 더 나은 접근 방식을 취할 것입니다. 연결ios sdk에서 동시에 여러 비디오 파일을 다운로드하십시오.

NSString *requestString = [NSMutableString stringWithString:VIDEO_LINK]; 
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:requestString] cachePolicy:NO timeoutInterval:15.0]; 
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES]; 

를 생성하고 다음과 같이 콜백을 처리하기위한

감사

// 글로벌 헤더 변수

float contentSize; 
NSMutableData *responseAsyncData; 
UIProgressView *progressBar; 

// 코드 ...

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 

    if ([response isKindOfClass:[NSHTTPURLResponse class]]) 
    { 
     NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response; 
     contentSize = [httpResponse expectedContentLength]; 
    } 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 

    if(responseAsyncData==nil) 
    { 
     responseAsyncData = [[NSMutableData alloc] initWithLength:0]; 
    } 
    [responseAsyncData appendData:data]; 
    float progress = (float)[responseAsyncData length]/(float)contentSize; 
    [progressBar setProgress:progress]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    NSLog(@"Error: %@", [error localizedDescription]); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSError* error; 

    if(responseAsyncData) 
    { 
     //filepath = Path to my location where i am storing 
     BOOL pass = [responseAsyncData writeToFile:filepath atomically:YES]; 
     if (pass) { 
      NSLog(@"Saved to file: %@", filepath); 
     } else { 
      NSLog(@"Video not saved."); 
     } 
     [progressBar setProgress:0]; 
    } 
    responseAsyncData = nil; 
} 

답변

관련 문제