我想知道是否可以检测WKWebView中正在播放的电影吗?
另外,我想知道打开的流的确切URL吗?

最佳答案

由于此问题的解决方案需要大量研究和不同方法,因此我想在此记录下来,以供其他人遵循我的想法。如果您只对最终解决方案感兴趣,请寻找一些有趣的标题。

我开始使用的应用程序非常简单。这是一个单 View 应用程序,它导入WebKit并使用一些WKWebView打开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)
    }
}



WKNavigationDelegate
WKNavigationDelegate不会通知您正在播放的视频。因此,设置webView.navigationDelegate = self并实现该协议(protocol)不会为您带来所需的结果。

NSNotificationCenter

我假设必须有一个类似SomeVideoPlayerDidOpen的事件。不幸的是没有,但是可能有一个SomeViewDidOpen事件,所以我开始检查 View 层次结构:
UIWindow
    UIWindow
        WKWebView
            WKScrollView
            ...
        ...
UIWindow
    UIWindow
        UIView
            AVPlayerView
        UITransitionView
            UIView
                UIView
                    UIView
                        ...
                    UIView
                        ...
                    AVTouchIgnoringView
                        ...

如预期的那样,将添加一个额外的UIWindow,其中可能有一个事件,是的,确实如此!

我通过添加新的观察者扩展了viewDidAppear::
NSNotificationCenter.defaultCenter().addObserver(self, selector: "windowDidBecomeVisible:", name: UIWindowDidBecomeVisibleNotification, object: nil)

并添加了相应的方法:
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)")
}

正如预期的那样,它返回了我们先前检查的 View 层次结构。但不幸的是,似乎AVPlayerView将在以后创建。

如果您信任您的应用程序,它将打开的唯一UIWindow是媒体播放器,那么您到此为止。但是这种解决方案不会让我晚上休眠,所以让我们更深入...

注入(inject)事件

我们需要获得有关将AVPlayerView添加到此无名UIView的通知。似乎很明显AVPlayerView必须是UIView的子类,但是由于Apple尚未正式记录它,因此我检查了iOS Runtime Headers for AVPlayerView ,它肯定是UIView

现在我们知道AVPlayerViewUIView的子类,它很可能会通过调用UIView来添加到无名的addSubview:中。因此,我们必须收到有关添加的 View 的通知。不幸的是,UIView并没有提供可观察到的事件。但是它确实调用了一种称为didAddSubview:的方法,该方法可能非常方便。

因此,让我们检查是否在我们的应用程序中的某处添加了AVPlayerView并发送通知:
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)

现在我们可以观察通知并接收相应的事件:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerWillOpen:", name: "PlayerWillOpen", object: nil)

func playerWillOpen(notification: NSNotification) {
    print("A Player will be opened now")
}

更好的通知注入(inject)

由于AVPlayerView不会被删除,而只会被释放,因此我们将不得不重新编写一些代码,并向AVPlayerViewController注入(inject)一些通知。这样,我们将拥有所需的任意数量的通知,例如:PlayerWillAppearPlayerWillDisappear:
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)

现在我们可以观察到这两个通知,并且很好:
 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")
}

影片网址

我花了几个小时来挖掘一些iOS运行时 header ,以猜测在哪里可以找到指向视频的URL,但是我找不到它。当我研究WebKit本身的一些源文件时,我不得不放弃并接受没有简单的方法来做到这一点,尽管我认为它是隐藏的并且可以访问的,但很可能只有大量的努力。

07-24 21:22