从HTML + UIWebView的视频下载(Video download from HTML +

2019-07-31 10:06发布

在我的新应用程序,我需要从不同的,不同的网站下载视频,说它是视频下载的应用程序。 对于这个东西我刨被搜索的MP4和FLV的网址HTML,然后尝试下载视频。 有很多应用已经在做相同的

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

什么,我问的是,我们如何可以下载视频? 任何代码或链接什么的。 如何申请工作? 任何帮助将很感激。

当您打开在一个UIWebView的网页说你开“www.youtube.com”,然后选择要播放的影片则询问是否下载什么,我需要的是。 对于下载我需要的网址(URL嵌入式,FLV网址,MPV URL),这样我就可以PAAS这个工作。 我需要知道关于URL

Answer 1:

如果你能够使用AFNetworking库,这是很简单的。 你可以让一个HTTP请求和使用其outputStream属性将文件下载到您的设备。 假设你挂钩下载按钮的功能downloadVideoFromURL:withName:

- (void)downloadVideoFromURL:(NSURL*)url withName:(NSString*)videoName
{
    //filepath to your app's 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];

    }
}


Answer 2:

如果你真的想要去的黑客,你,你会得到苹果的私人图书馆,“WebKit的”,如果你试图找到一个UIWebView的子视图,它可以帮助你,我从来没有尝试过,但你可以用这个逻辑测试。



文章来源: Video download from HTML + UIWebView