-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;
}
감사를 링크 : 그럼 당신은, 특정 수의 등
나는이 자습서를 읽지 않은, 병렬로 수행 할 수 있지만, 아주 철저하게 보이는 수 비동기 적으로 다운로드를 실행하는
NSOperationQueue
을 사용할 수 있습니다 . – Rishi