【问题标题】:Identifying youtube video url playing in webview in ios?识别在 ios 的 webview 中播放的 youtube 视频 url?
【发布时间】:2016-11-02 05:29:26
【问题描述】:
我有一个 webview 控件并请求在 Youtube 中进行一些搜索。
有很多结果视频。
用户单击其中之一。
如何获取播放的视频地址?
【问题讨论】:
标签:
ios
objective-c
video
youtube
uiwebview
【解决方案1】:
[[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
}
【解决方案2】:
如果您只想获取 URL,请尝试以下代码:
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;
}