2011-09-08 6 views
0

다른 경로의 URL에서 10 개의 다른 비디오를 다운로드하고 싶습니다. 내 URL은 http://someurl/document/path1.mp4 개까지 path10.mp4라고 말할 수 있습니다. http 연결 방법으로이 작업을 수행하고 싶습니다. 가능합니다. 어떻게해야합니까. 내가 이것을 할 경우 connetion1, 연결 2 .......10 연결 (connection) 데이터를 추적해야합니다. connectionDidreceive Response 메서드에서 응답을 얻고 있습니다.xcode에서 복수 웹 서비스를 호출하는 방법

기본적으로 원하는 것은 1 번째 비디오를 다운로드하고 3 번째 비디오를 다운로드하는 것과 같은 비디오를 다운로드하는 것입니다. 그러나 나는 원하는 모든 비디오를 동시에 다운로드 할 수 있습니다. 어떻게 가능합니까?

답변

1

이것은 확실히 가능합니다.

이러한 여러 가지 요청을 관리하고 시작할 수있는 좋은 방법을 찾고 있다면; 나는 this 스레드가이 문제에 대해 밝힐 수 있다고 믿습니다. 도움이 될 수있는 여러 요청을 관리하기위한 몇 가지 권장 사항을 강조한 것 같습니다.

0
  1. 파일 URL이있는 경우 게시 방법이 필요하지 않을 수 있습니다. 이 메소드는 서버에 매개 변수를 보낼 때 특별히 사용됩니다.

  2. 글쎄 ConnectiondidReceiveResponse 응답이 도착한 인수에서 연결 개체를 보냅니다.

  3. 모범 사례는 하나의 연결 개체가있는 클래스를 작성하고 해당 클래스를 다른 URL 및 파일 특정 매개 변수 (예 : 저장 위치 등)로 초기화하는 것입니다. 그런 다음 해당 클래스는 다운로드의 모든 복잡성을 처리합니다. 완료가되면 호출자 클래스에 파일 이름을 알릴 수 있습니다.

0
-(void)getMeetings 
{ 
    NSString *requestURL = [NSString stringWithFormat:@"%@",@"someurl"]; 
    [self webserviceCreate:nil urlOfwebservice:[NSURL URLWithString:requestURL] tag:1]; 
} 

-(void)webserviceCreatePost:(NSDictionary *)dict urlOfwebservice:(NSURL *)url tag:(int)tag 
{ 
    NSError *error = nil; 
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict 
                 options:NSJSONWritingPrettyPrinted 
                 error:&error]; 

    NSString *requestJson = @""; 
    if (!jsonData) { 
     //Deal with error 
    } else { 
     requestJson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
    } 

    NSLog(@"jsonRequest is %@", requestJson); 

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
                  cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 
    connectionToInfoMapping = CFDictionaryCreateMutable(kCFAllocatorDefault,0,&kCFTypeDictionaryKeyCallBacks,&kCFTypeDictionaryValueCallBacks); 

    NSData *requestData = [requestJson dataUsingEncoding:NSUTF8StringEncoding]; 

    [request setHTTPMethod:@"POST"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    [request setValue:[[NSUserDefaults standardUserDefaults]valueForKey:@"SessionKey"] forHTTPHeaderField:@"Authorization"]; 
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody: requestData]; 

    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 
    CFDictionaryAddValue(connectionToInfoMapping,(__bridge const void *)(connection), 
         (__bridge const void *)([NSMutableDictionary 
                dictionaryWithObjectsAndKeys:[NSMutableData data],@"receivedData",[NSString stringWithFormat:@"%d",tag],@"tag", nil])); 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    // [receivedData setLength:0]; 
    NSMutableDictionary *connectionInfo = CFDictionaryGetValue(connectionToInfoMapping, (__bridge const void *)(connection)); 
    receivedData = [connectionInfo objectForKey:@"receivedData"]; 
    [receivedData setLength:0]; 

} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    NSMutableDictionary *connectionInfo = CFDictionaryGetValue(connectionToInfoMapping, (__bridge const void *)(connection)); 
    [[connectionInfo objectForKey:@"receivedData"] appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    [HUD hide:YES]; 
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:[error localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSMutableDictionary *connectionInfo = CFDictionaryGetValue(connectionToInfoMapping, (__bridge const void *)(connection)); 

    int tag = [[connectionInfo valueForKey:@"tag"] intValue]; 

    if (tag == 1) 
    { 
     NSArray *arrMeeting = [NSJSONSerialization JSONObjectWithData:[connectionInfo valueForKey:@"receivedData"] options:0 error:nil]; 
} 
} 
관련 문제