识别在ios中使用webview播放的YouTube视频网址?

时间:2016-06-30 08:24:07

标签: ios objective-c video youtube uiwebview

我有一个webview控件并在Youtube中请求一些搜索。 有很多结果视频。 用户单击其中一个。 如何播放视频网址?

2 个答案:

答案 0 :(得分:0)

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemBecameCurrent:)
                                             name:@"AVPlayerItemBecameCurrentNotification"
                                           object:nil];


- (void)playerItemBecameCurrent:(NSNotification*)notification 
{
        AVPlayerItem *playerItem = [notification object];
        if(playerItem == nil) return;

        // Break down the AVPlayerItem to get to the path
        AVURLAsset *asset = (AVURLAsset*)[playerItem asset];
        NSURL *url = [asset URL]; // this is URL you need
}

答案 1 :(得分:0)

如果您只想获取网址,请尝试以下代码:

  

NSString * currentURL = [webView stringByEvaluatingJavaScriptFromString:@" window.location"];

但是,如果你想获得所有事件,只需使用webview委托方法:

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
        NSString *URLString = [[request URL] absoluteString];
        //now you have the url;
       return YES;
   }