【问题标题】:Video download from HTML + UIWebView从 HTML + UIWebView 下载视频
【发布时间】:2012-09-01 09:45:26
【问题描述】:

在我的新应用程序中,我需要从不同的网站下载视频,假设它是视频下载器应用程序。为此,我计划在 html 中搜索 .mp4 和 .Flv url,然后尝试下载视频。有很多应用程序已经在做同样的事情

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

我要问的是,我们如何下载视频?任何代码或链接或其他东西。这个应用程序是如何工作的?任何帮助都会非常感激。

我需要的是当您在 UIWebview 中打开页面时假设您打开“www.youtube.com”并选择要播放的视频,然后它会要求下载。对于下载,我需要 URL(嵌入式 url、Flv url、mpv url),这样我就可以让它发挥作用。我需要知道那个 URL

【问题讨论】:

标签: iphone ios video uiwebview download


【解决方案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];

    }
}

【讨论】:

  • 感谢 Anthony 的快速响应,这很有帮助,但我需要的是当您在 UIWebview 中打开页面时假设您打开“www.youtube.com”并选择要播放的视频然后它要求下载.对于下载,我需要 URL(嵌入式 url、Flv url、mpv url),这样我就可以让它发挥作用。我需要知道那个 URL
  • @Mangesh Vyas ,您找到解决问题的方法了吗?我的应用中有相同的任务,我现在正在寻找解决方案。
  • 没有petr ...我还没有....即使我也很感兴趣..我发现的是网页端的硬编码,然后在html中搜索视频URL,然后开始下载......但我正在寻找的是 safari 浏览器是否有任何 API 或一些 hack,因为视频可以通过网络浏览器播放......
  • 有没有人找到解决方案。我仍在寻找答案,因为市场上有很多应用程序,所以一定有办法做到这一点。
  • 如果您使用 UIWebView,请点击此链接stackoverflow.com/questions/55377677/…
【解决方案2】:

如果你真的想去黑客,你会得到苹果的私有库,“webkit”,如果你尝试找到 UIWebview 的子视图,它可能会对你有所帮助,我从未尝试过,但你可以用这个进行测试逻辑。

【讨论】:

  • @Mangesh,你找到解决方案了吗?我现在也在做同样的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-24
  • 2013-09-10
  • 2010-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-04
相关资源
最近更新 更多