我正在使用下面的代码从 UIWebView 中提取网址:它工作正常,但是,用于 WKWebView 的同一代码已不再起作用。谁能帮我?在WKWebView中播放的视频不是全屏播放,而是Inlineplacyback。
我的代码是:
NotificationCenter.default.addObserver(self, selector: #selector(self.playerItemBecameCurrent(_:)), name: NSNotification.Name("AVPlayerItemBecameCurrentNotification"), object: nil)
@objc func playerItemBecameCurrent(_ sender : NSNotification){
let playerItem: AVPlayerItem? = sender.object as? AVPlayerItem
if playerItem == nil {
print("player item nil")
return
}
// Break down the AVPlayerItem to get to the path
let asset = playerItem?.asset as? AVURLAsset
let url: URL? = asset?.url
let path = url?.absoluteString
print(path!,"video url")
}
响应URL:
这是视频URL ,而不是网页URL,所以请帮助我。 谢谢。
答案 0 :(得分:7)
这是一种黑客手段,但这是我发现完成此任务的唯一方法。
首先将自己设置为WKWebView导航委托:
self.webView?.navigationDelegate = self
现在收听所有导航更改,并保存请求的网址:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if let urlStr = navigationAction.request.url?.absoluteString {
//Save presented URL
//Full path can be accessed via self.webview.url
}
decisionHandler(.allow)
}
现在,您只需要知道新屏幕何时可见,并使用您保存的URL(即可知道新可见屏幕的视频URL)。
您可以通过听 UIWindowDidBecomeVisibleNotification 通知来做到这一点:
NotificationCenter.default.addObserver(self, selector: #selector(windowDidBecomeVisibleNotification(notif:)), name: NSNotification.Name("UIWindowDidBecomeVisibleNotification"), object: nil)
然后检查导航窗口是否不是您的窗口,这意味着确实打开了一个新屏幕:
@objc func windowDidBecomeVisibleNotification(notif: Notification) {
if let isWindow = notif.object as? UIWindow {
if (isWindow !== self.view.window) {
print("New window did open, check what is the currect URL")
}
}
}
答案 1 :(得分:2)
您可以尝试在WKWebView
中注入JS,如下所示:https://paulofierro.com/blog/2015/10/12/listening-for-video-playback-within-a-wkwebview
答案 2 :(得分:2)
您可以使用以下代码从webview的网址中获取html内容
let docString = webView.stringByEvaluatingJavaScriptFromString("document.documentElement.outerHTML")
这种情况下,您将获得整个html内容,
然后在html字符串中查找href链接
let regex = try! NSRegularExpression(pattern: "<a[^>]+href=\"(.*?)\"[^>]*>")
let range = NSMakeRange(0, docString.characters.count)
let matches = regex.matches(in: docString, range: range)
for match in matches {
let htmlLessString = (docString as NSString).substring(with: match.rangeAt(1))
print(htmlLessString)
}
使用
检查它是否为youtube网址正则表达式:“ @https?://(www。)?youtube.com/。[^ \ s。,“ \'] + @ i”
跳出框框!
您可以进行api调用以获取网址。使用php,.net等网络语言似乎很容易。
使用PHP获取网页内所有网址的代码(使用适合您的任何语言)
$url="http://wwww.somewhere.com";
$data=file_get_contents($url);
$data = strip_tags($data,"<a>");
$d = preg_split("/<\/a>/",$data);
foreach ( $d as $k=>$u ){
if( strpos($u, "<a href=") !== FALSE ){
$u = preg_replace("/.*<a\s+href=\"/sm","",$u);
$u = preg_replace("/\".*/","",$u);
print $u."\n";
}
}
一一检查是否为youtube网址。
$sText = "Check out my latest video here http://www.youtube.com/?123";
preg_match_all('@https?://(www\.)?youtube.com/.[^\s.,"\']+@i', $sText, $aMatches);
var_dump($aMatches);
如果要检查示例应用程序是否使用相同的方法,请获取Web调试代理并对其进行挖掘
上面的许多解释来自其他网站。
我希望它总结您的需要! 编码愉快!
答案 3 :(得分:1)
从WKNavigationDelegate的webView(_:decidePolicyFor:decisionHandler :)方法中的导航操作的request属性中检索完整URL。
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if let urlStr = navigationAction.request.url?.absoluteString {
//urlStr is your URL
}
decisionHandler(.allow)
}
也不要忘记遵守协议
webView.navigationDelegate = self
答案 4 :(得分:1)
在ViewController中尝试此操作,以在WKWebView上添加URL观察器:
override func loadView() {
let webConfig = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfig)
webView.addObserver(self, forKeyPath: "URL", options: .new, context: nil)
view = webView
}
重载observeValue以获取URL请求:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == #keyPath(WKWebView.url) {
let urlRequest:String = webView.url?.absoluteString ?? ""
print(urlRequest)
}
}
最后...初始化观察者:
deinit { webView.removeObserver(self, forKeyPath: "URL") }