从HTML + UIWebView下载视频

时间:2012-09-07 13:39:22

标签: iphone ios video uiwebview download

在我的新应用程序中,我需要从不同的网站下载视频,Say it is video downloader application。为此,我正在计划搜索html for .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),所以我可以使用它来运行。我需要了解该网址

2 个答案:

答案 0 :(得分: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];

    }
}

答案 1 :(得分:1)

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