iOS - Download Video

╄→尐↘猪︶ㄣ 提交于 2019-12-11 04:36:31

问题


I'd like to download a video from a remote URL and save it to a file in an iPhone app. I know the video link works, since I have used it from AVPlayer, however, I am unable to download it. The response is always (null).

What is wrong with the following code?

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:someURLString]];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:someFilePath append:NO];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Successfully downloaded file to %@", [NSURL fileURLWithPath:someFilePath]);
        NSLog(@"THE RESPONSE: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

    [operation start];

Update

I commented out the operation.outputStream line, and this time I got a response. Does this mean that there is something wrong with the file path?


回答1:


just create a link to that file, then use NSURLConnection to download.

Create a URL connection to download:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:strFileUrl]]; //strFileURL is url of your video/image
NSURLConnection *conection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
[conec start];
[request release];

Get path of file to save data:

strFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:strFileName];

Your class must adopt 3 methods of NSURLConnectionDelegate protocol: (please read about Protocol and Delegate)

   - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
        // create 
        [[NSFileManager defaultManager] createFileAtPath:strFilePath contents:nil attributes:nil];
        file = [[NSFileHandle fileHandleForUpdatingAtPath:strFilePath] retain];// read more about file handle
        if (file)   {
            [file seekToEndOfFile];
        }
    }



 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)receivedata
    {    
        //write each data received
        if( receivedata != nil){
            if (file)  { 
                [file seekToEndOfFile];
            } 
            [file writeData:receivedata]; 
        }
    }

    - (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
        //close file after finish getting data;
        [file closeFile];
    }

    - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
        //do something when downloading failed
    }

If you want to review you file, use a UIWebview to load it:

 NSURL *fileURL = [NSURL fileURLWithPath:strFilePath];
    [wvReview loadRequest:[NSURLRequest requestWithURL:fileURL]];



回答2:


This is what was going wrong.

I was using the url of the video, and as part of the file path.

Why is this wrong? It has backslashes, so I assume iOS was getting confused.

Lesson learned: make sure that the string that you add to a directory to create a file does not have backslashes.

I hope this helps anyone else who makes this silly mistake. :P



来源:https://stackoverflow.com/questions/17769651/ios-download-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!