2013-08-29 25 views
2

我需要下載此視頻http://studiokicks.com/xtr/basic/1.mov視頻的時候我用下面的代碼是沒有得到下載:視頻沒有得到下載

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); 

} 
+0

NSLog在此行後面的returnData:'returnData = [NSMutableData data];'。它是否被初始化並返回一個指針? – pbibergal

+0

當你在調試器中追蹤執行時,你發現了什麼? –

+0

NSLog中只給353 – Swati

回答

1
  1. 您需要設置一個成員NSMutableData類型的self.data
  2. 在開始加載NSURLConnection之前,請初始化:self.data = [NSMutableData new]
  3. connectionDidReceiveData方法做:[self.data appendData:data]。
  4. 而在connectionDidFinishLoading中,您可以查看數據。
+0

請看我的帖子,我已經編輯過。我已經在做你剛纔建議但沒有發佈的問題 – Khushboo