Download video from HTML + UIWebView

In my new application I need to download videos from different websites, let's say this is an application for downloading videos. To do this, I plan to search for html for the .mp4 and .Flv URLs, and then try to download the video. There are many applications that are already doing the same.

http://itunes.apple.com/in/app/video-downloader-super-lite/id481701140?mt=8

I ask how can we download a video? Any code or link or something else. how does this app work? Any help really like it.

I need it when you open the page in UIWebview. Say you open "www.youtube.com" and select a video to play, and then request it to download. To download, I need a URL (Embeded url, Flv URL, mpv URL), so I can use it to work. I need to know that URL

+6
source share
2 answers

If you can use the AFNetworking library, it's pretty simple. You can make an HTTP request and use the outputStream property to download the file to your device. Let's say you connect the download button to the downloadVideoFromURL:withName:

 - (void)downloadVideoFromURL:(NSURL*)url withName:(NSString*)videoName { //filepath to your app documents directory NSString *appDocPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *videosPath = [appDocPath stringByAppendingPathComponent:@"Videos"]; NSString *filePath = [videosPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4", videoName]]; //check to make sure video hasn't been downloaded already if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { //file was already downloaded } //video wasn't downloaded, so continue else { //enable the network activity indicator [AFNetworkActivityIndicatorManager sharedManager].enabled = YES; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest]; //create a temporary filepath while downloading NSString *tmpPath = [videosPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-tmp.mp4", videoName]]; //the outputStream property is the key to downloading the file operation.outputStream = [NSOutputStream outputStreamToFileAtPath:tmpPath append:NO]; //if operation is completed successfully, do following [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { //rename the downloaded video to its proper name [[NSFileManager defaultManager] moveItemAtPath:tmpPath toPath:filePath error:nil]; //disable network activity indicator [AFNetworkActivityIndicatorManager sharedManager].enabled = NO; //optionally, post a notification to anyone listening that the download was successful [[NSNotificationCenter defaultCenter] postNotificationName:@"DownloadedVideo" object:nil]; //if the operation fails, do the following: } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error: %@", error); //delete the downloaded file (it is probably partially downloaded or corrupt) [[NSFileManager defaultManager] removeItemAtPath:tmpPath error:nil]; //disable network activity indicator [AFNetworkActivityIndicatorManager sharedManager].enabled = NO; }]; //start the operation [operation start]; } } 
+1
source

If you really want to hack, you will get the apple webkit private library, if you try to find the UIWebview sub-items, this may help you, I have never tried, but you can test the logic with that.

+1
source

Source: https://habr.com/ru/post/924855/


All Articles