How to detect a movie being played in a WKWebView?
Since the solution(s) to this question required a lot of research and different approaches, I'd like to document it here for others to follow my thoughts. If you're just interested in the final solution, look for some fancy headings.
The app I started with, was pretty simple. It's a Single-View Application that imports WebKit
and opens a WKWebView
with some NSURL
:
import UIKit
import WebKit
class ViewController: UIViewController {
var webView: WKWebView!
override func viewDidAppear(animated: Bool) {
webView = WKWebView()
view = webView
let request = NSURLRequest(URL: NSURL(string: "http://tinas-burger.tumblr.com/post/133991473113")!)
webView.loadRequest(request)
}
}
The URL includes a video that is (kind of) protected by JavaScript. I really haven't seen the video yet, it was just the first I discovered. Remember to add
NSAppTransportSecurity
andNSAllowsArbitraryLoads
to yourInfo.plist
or you will see a blank page.
WKNavigationDelegate
The WKNavigationDelegate
won't notify you about a video being played. So setting webView.navigationDelegate = self
and implementing the protocol won't bring you the desired results.
NSNotificationCenter
I assumed that there must be an event like SomeVideoPlayerDidOpen
. Unfortunately there wasn't any, but it might have a SomeViewDidOpen
event, so I started inspecting the view hierarchy:
UIWindow
UIWindow
WKWebView
WKScrollView
...
...
UIWindow
UIWindow
UIView
AVPlayerView
UITransitionView
UIView
UIView
UIView
...
UIView
...
AVTouchIgnoringView
...
As expected there will be an additional UIWindow
added which might have an event and hell yes it does have!
I extended viewDidAppear:
by adding a new observer:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "windowDidBecomeVisible:", name: UIWindowDidBecomeVisibleNotification, object: nil)
And added the corresponding method:
func windowDidBecomeVisible(notification: NSNotification) {
for mainWindow in UIApplication.sharedApplication().windows {
for mainWindowSubview in mainWindow.subviews {
// this will print:
// 1: `WKWebView` + `[WKScrollView]`
// 2: `UIView` + `[]`
print("\(mainWindowSubview) \(mainWindowSubview.subviews)")
}
As expected it returns the view hierarchy as we inspected earlier. But unfortunately it seems like the AVPlayerView
will be created later.
If you trust your application that the only UIWindow
it'll open is the media player, you're finished at this point. But this solution wouldn't let me sleep at night, so let's go deeper...
Injecting An Event
We need to get notified about the AVPlayerView
being added to this nameless UIView
. It seems pretty obvious that AVPlayerView
must be a subclass of UIView
but since it's not officially documented by Apple I checked the iOS Runtime Headers for AVPlayerView
and it definitely is a UIView
.
Now that we know that AVPlayerView
is a subclass of UIView
it will probably added to the nameless UIView
by calling addSubview:
. So we'd have to get notified about a view that was added. Unfortunately UIView
doesn't provide an event for this to be observed. But it does call a method called didAddSubview:
which could be very handy.
So let's check wether a AVPlayerView
will be added somewhere in our application and send a notification:
let originalDidAddSubviewMethod = class_getInstanceMethod(UIView.self, "didAddSubview:")
let originalDidAddSubviewImplementation = method_getImplementation(originalDidAddSubviewMethod)
typealias DidAddSubviewCFunction = @convention(c) (AnyObject, Selector, UIView) -> Void
let castedOriginalDidAddSubviewImplementation = unsafeBitCast(originalDidAddSubviewImplementation, DidAddSubviewCFunction.self)
let newDidAddSubviewImplementationBlock: @convention(block) (AnyObject!, UIView) -> Void = { (view: AnyObject!, subview: UIView) -> Void in
castedOriginalDidAddSubviewImplementation(view, "didAddsubview:", subview)
if object_getClass(view).description() == "AVPlayerView" {
NSNotificationCenter.defaultCenter().postNotificationName("PlayerWillOpen", object: nil)
}
}
let newDidAddSubviewImplementation = imp_implementationWithBlock(unsafeBitCast(newDidAddSubviewImplementationBlock, AnyObject.self))
method_setImplementation(originalDidAddSubviewMethod, newDidAddSubviewImplementation)
Now we can observe the notification and receive the corresponding event:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerWillOpen:", name: "PlayerWillOpen", object: nil)
func playerWillOpen(notification: NSNotification) {
print("A Player will be opened now")
}
Better notification injection
Since the AVPlayerView
won't get removed but only deallocated we'll have to rewrite our code a little bit and inject some notifications to the AVPlayerViewController
. That way we'll have as many notifications as we want, e.g.: PlayerWillAppear
and PlayerWillDisappear
:
let originalViewWillAppearMethod = class_getInstanceMethod(UIViewController.self, "viewWillAppear:")
let originalViewWillAppearImplementation = method_getImplementation(originalViewWillAppearMethod)
typealias ViewWillAppearCFunction = @convention(c) (UIViewController, Selector, Bool) -> Void
let castedOriginalViewWillAppearImplementation = unsafeBitCast(originalViewWillAppearImplementation, ViewWillAppearCFunction.self)
let newViewWillAppearImplementationBlock: @convention(block) (UIViewController!, Bool) -> Void = { (viewController: UIViewController!, animated: Bool) -> Void in
castedOriginalViewWillAppearImplementation(viewController, "viewWillAppear:", animated)
if viewController is AVPlayerViewController {
NSNotificationCenter.defaultCenter().postNotificationName("PlayerWillAppear", object: nil)
}
}
let newViewWillAppearImplementation = imp_implementationWithBlock(unsafeBitCast(newViewWillAppearImplementationBlock, AnyObject.self))
method_setImplementation(originalViewWillAppearMethod, newViewWillAppearImplementation)
let originalViewWillDisappearMethod = class_getInstanceMethod(UIViewController.self, "viewWillDisappear:")
let originalViewWillDisappearImplementation = method_getImplementation(originalViewWillDisappearMethod)
typealias ViewWillDisappearCFunction = @convention(c) (UIViewController, Selector, Bool) -> Void
let castedOriginalViewWillDisappearImplementation = unsafeBitCast(originalViewWillDisappearImplementation, ViewWillDisappearCFunction.self)
let newViewWillDisappearImplementationBlock: @convention(block) (UIViewController!, Bool) -> Void = { (viewController: UIViewController!, animated: Bool) -> Void in
castedOriginalViewWillDisappearImplementation(viewController, "viewWillDisappear:", animated)
if viewController is AVPlayerViewController {
NSNotificationCenter.defaultCenter().postNotificationName("PlayerWillDisappear", object: nil)
}
}
let newViewWillDisappearImplementation = imp_implementationWithBlock(unsafeBitCast(newViewWillDisappearImplementationBlock, AnyObject.self))
method_setImplementation(originalViewWillDisappearMethod, newViewWillDisappearImplementation)
Now we can observe these two notifications and are good to go:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerWillAppear:", name: "PlayerWillAppear", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerWillDisappear:", name: "PlayerWillDisappear", object: nil)
func playerWillAppear(notification: NSNotification) {
print("A Player will be opened now")
}
func playerWillDisappear(notification: NSNotification) {
print("A Player will be closed now")
}
URL of the video
I spent a couple of hours digging some iOS Runtime Headers to guess where I could find the URL pointing to the video, but I couldn't manage to find it. When I was digging into some source files of WebKit itself, I had to give up and accept that there's no easy way to do it, although I believe it's somewhere hidden and can be reached, but most likely only with a lot of effort.
How can I get current audio URL playing inside my app from WKWebView?
You need to subscribe on notification :
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemBecameCurrent:)
name:@"AVPlayerItemBecameCurrentNotification"
object:nil];
And then do this inside method playerItemBecameCurrent
-(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];}
How to get the url of currently playing video in UIWebview
There is a hacky way of doing it 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, I noticed you mentioned YouTube - it's worth pointing out Apple WILL reject your app if it has the ability to download YouTube videos AT ALL, because it's against YouTube's Terms of Service.
Related Topics
Uiview Infinite 360 Degree Rotation Animation
Wkwebview Not Loading Local Files Under iOS 8
How to Have Stored Properties in Swift, the Same Way I Had on Objective-C
How to Change the Status Bar Background Color and Text Color on iOS 7
Change Paid App to Free But Know If User Previously Purchased It
Creating a Left-Arrow Button (Like Uinavigationbar'S "Back" Style) on a Uitoolbar
How to Get My Ip Address Programmatically on Ios/Macos
Load Resources from Relative Path Using Local HTML in Uiwebview
How to Change Navigation Bar Color in iOS 7
Disabling User Selection in Uiwebview
Difference Between Observedobject and Stateobject in Swiftui
Core Data: Quickest Way to Delete All Instances of an Entity
Swift Parse Json - the Data Couldn'T Be Read Because It Isn'T in the Correct Format