2

I need to download this video http://studiokicks.com/xtr/basic/1.mov The video is not getting downloaded when I use the following code:

NSURL *requestURL = [NSURL URLWithString:itemURL];
NSURLRequest *request = [NSURLRequest requestWithURL:requestURL];
NSData *data = [NSData dataWithContentsOfURL:requestURL];
NSLog(@"data - %d",[data length]);
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];


-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"didReceiveResponse");
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    if ((([httpResponse statusCode]/100) == 2)) {


        returnData = [NSMutableData data];

    }
    else
    {

        NSDictionary *userInfo = [NSDictionary dictionaryWithObject: NSLocalizedString(@"HTTP Error", @"Error message displayed when receving a connection error.")forKey:NSLocalizedDescriptionKey];
        NSError *error = [NSError errorWithDomain:@"HTTP" code:[httpResponse statusCode] userInfo:userInfo];
        NSLog(@"%@",error);
    }
}

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

    [returnData appendData:data];
    // Build an array from the dictionary for easy access to each entry
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection1
{
    NSString *filePath = [VIDEOPATH stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mov",videoName]];
    BOOL success = [returnData writeToFile:filePath atomically:YES];
    NSLog(@"successfully saved - %d",success);

}
4

1 回答 1

1
  1. 您需要设置self.data类型的成员NSMutableData
  2. 在开始加载之前NSURLConnection,初始化:self.data = [NSMutableData new]
  3. connectionDidReceiveData方法中做:[ self.data appendData:data]。
  4. 并且connectionDidFinishLoading可以在其中查看数据。
于 2013-08-29T09:11:53.850 回答