2014-09-25 219 views
4

我想攔截UIWebView中的點擊,然後使用視頻的URL。這怎麼可能?我發現了一個有點類似的帖子,它指向與檢測和攔截UIWebView中的視頻播放

webView:shouldStartLoadWithRequest:navigationType: 

委託人。我似乎無法使用此代理獲取視頻的加載網址。

我想要得到的代碼在iOS8上

回答

8

在那裏工作是監聽AVPlayerItemBecameCurrentNotification通知找到URL的哈克的方式。當UIWebView顯示媒體播放器時觸發此通知,併發送一個AVPlayerItem作爲通知的對象。

例如:

[[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]; 
    NSString *path = [url absoluteString]; 
} 

這適用於任何視頻(音頻)。然而,在媒體播放器加載之後,這是無價值的,因此您無法阻止播放器在此時啓動(如果這是您的意圖)。

+3

任何人都有這個更新的解決方案與WKWebView? – 2017-01-01 23:05:08

+0

當WKWebView播放視頻時,不會發送任何通知。您可以收聽所有通知並打印日誌以證明它。 @CharltonProvatas – 2017-02-28 13:58:30

+0

我能夠使用UIWindow的通知獲取它。我不認爲這將工作,但視頻播放內聯:https://developer.apple.com/reference/uikit/uiwindow – 2017-02-28 14:46:59

1

如果上述解決方案不適用於任何人,則嘗試偵聽AVPlayerItemNewAccessLogEntry而不是AVPlayerItemBecameCurrentNotification。似乎之後會調用AVPlayerItemNewAccessLogEntry,這是我能夠設置信息的地方。

相關問題