4

I would like to intercept a click in an UIWebView and then use the URL of the video. How is this possible? I found a somewhat similar post which pointed met to the

webView:shouldStartLoadWithRequest:navigationType:

delegate. I cant seem to get the loaded url of the video with this delegate.

I am trying to get the code working in iOS8

Andrew Ho
  • 618
  • 9
  • 21

2 Answers2

9

There is a hacky way of finding the URL by listening for the AVPlayerItemBecameCurrentNotification notification. This notification is fired when a UIWebView shows the media player, and it sends an AVPlayerItem as the notification's object.

For example:

[[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];
}

This works for any video (and audio). However it is worth nothing that this is fired after the media player has loaded, so you can't stop the player from launching at this point (if that was your intention).

ttarik
  • 3,824
  • 1
  • 32
  • 51
  • 5
    anyone have an updated solution for this with WKWebView? – Charlton Provatas Jan 01 '17 at 23:05
  • When WKWebView playing back video, no notification will be sent. You can listen to all notifications and print log to prove it. @CharltonProvatas – DawnSong Feb 28 '17 at 13:58
  • I was able to get it using UIWindow's notifications. I don't think this would work however for videos playing inline : https://developer.apple.com/reference/uikit/uiwindow – Charlton Provatas Feb 28 '17 at 14:46
  • @CharltonProvatas Which UIWindow notification did you use? And was it an `AVPlayerItem` you got via the notification? – Valdimar Mar 19 '17 at 18:46
  • 2
    @Valdimar I used the .UIWindowDidResignKey notification. I was not able to get an instance of the AVPlayerItem by doing this, however I was able to surpress the video going full screen using this method. When going full screen from WKWebView/UIWebview, AVPlayerViewController creates an extra UIWindow instance, so you can use "makeKeyAndVisible" on your original UIWindow instance to surpress the video. – Charlton Provatas Mar 21 '17 at 11:30
  • This is no more work in WKWebView how can do this in WKWebView can you help me. – Virani Vivek Mar 30 '19 at 12:42
1

If the above solution doesn't work for anyone then try listening to AVPlayerItemNewAccessLogEntry rather than AVPlayerItemBecameCurrentNotification. AVPlayerItemNewAccessLogEntry seems to be called afterwards and is where I was able to set the info.