如何检测在 WKWebView 中播放的电影?

Posted

技术标签:

【中文标题】如何检测在 WKWebView 中播放的电影?【英文标题】:How to detect a movie being played in a WKWebView? 【发布时间】:2016-05-03 06:16:37 【问题描述】:

我想知道是否可以检测到 WKWebView 中正在播放的电影?

另外我想知道打开的流的确切 URL?

【问题讨论】:

【参考方案1】:

由于这个问题的解决方案需要大量的研究和不同的方法,我想在这里记录它,以便其他人跟随我的想法。 如果您只是对最终解决方案感兴趣,请寻找一些花哨的标题。

我开始使用的应用程序非常简单。这是一个单视图应用程序,它导入 WebKit 并打开一个带有一些 NSURLWKWebView

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) 
    

该网址包含受(某种)javascript 保护的视频。 我真的还没有看过这个视频,这只是我发现的第一个视频。记得在你的Info.plist 中添加NSAppTransportSecurityNSAllowsArbitraryLoads,否则你会看到一个空白页。 p>

WKNavigationDelegate

WKNavigationDelegate 不会通知您正在播放的视频。所以设置webView.navigationDelegate = self 并实现协议不会给你带来想要的结果。

NSNotificationCenter

我假设一定有像SomeVideoPlayerDidOpen 这样的事件。不幸的是没有,但它可能有一个SomeViewDidOpen 事件,所以我开始检查视图层次结构:

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)")

正如预期的那样,它返回了我们之前检查过的视图层次结构。但不幸的是,AVPlayerView 似乎稍后会创建。

如果您相信您的应用程序会打开唯一的UIWindow 是媒体播放器,那么您就完成了。但是这个解决方案不会让我晚上睡觉,所以让我们更深入......

注入事件

我们需要收到有关 AVPlayerView 被添加到这个无名 UIView 的通知。很明显AVPlayerView 必须是UIView 的子类,但由于Apple 没有正式记录它,我检查了ios Runtime Headers for AVPlayerView,它肯定UIView

现在我们知道AVPlayerViewUIView 的子类,它可能会通过调用addSubview: 添加到无名UIView。因此,我们必须收到有关已添加视图的通知。不幸的是,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")

更好的通知注入

由于AVPlayerView 不会被删除而只会被释放,我们将不得不稍微重写我们的代码并向AVPlayerViewController 注入一些通知。这样我们就会有尽可能多的通知,例如: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 运行时标头来猜测在哪里可以找到指向视频的 URL,但我无法找到它。当我深入研究 WebKit 本身的一些源文件时,我不得不放弃并接受没有简单的方法可以做到这一点,尽管我相信它隐藏在某个地方并且可以到达,但很可能只有付出了很多努力。

【讨论】:

我也有同样的需求,但是对于 os/x;遗憾的是通知不匹配? @Fabio Poloni 我可以从 WKWebView 获取带有通知的视频 url,因为 appstore 中有一些此类应用程序可用。请参阅此***.com/questions/55377677/… 请帮助我。【参考方案2】:

我尝试将一些用户脚本添加(注入)到WKWebView,这种方式比方法调配安全一点:

相关代码如下:

let contentController = WKUserContentController()

if let jsSource = NSBundle.mainBundle().URLForResource("video_play_messenger", withExtension: "js"),
    let jsSourceString = try? String(contentsOfURL: jsSource) 
    let userScript = WKUserScript(source: jsSourceString, injectionTime: .AtDocumentEnd, forMainFrameOnly: true)

    contentController.addUserScript(userScript)
    contentController.addScriptMessageHandler(self, name: "callbackHandler")


let webConfiguration = WKWebViewConfiguration()
webConfiguration.userContentController = contentController

webView = WKWebView(frame: CGRect.zero, configuration: webConfiguration)
let request = NSURLRequest(URL: NSURL(string: "URL_FOR_VIDEO")!)
webView.loadRequest(request)

对于WKWebView的控制器,符合WKScriptMessageHandler并实现该方法:

func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) 
    if message.name == "callbackHandler" 
        if let messageString = message.body as? String where messageString == "VideoIsPlaying" 
            // Vide is being played
        
    

video_play_messenger.js 添加到您的项目中:

function videoTags() 
    return document.getElementsByTagName("video");


function setupVideoPlayingHandler() 
    try 
        var videos = videoTags()
        for (var i = 0; i < videos.length; i++) 
            videos.item(i).onplaying = function() 
                webkit.messageHandlers.callbackHandler.postMessage("VideoIsPlaying");
            
        
     catch (error) 
        console.log(error);
    


function setupVidePlayingListener() 
    // If we have video tags, setup onplaying handler
    if (videoTags().length > 0) 
        setupVideoPlayingHandler();
        return
    

    // Otherwise, wait for 100ms and check again.
    setTimeout(setupVidePlayingListener, 100);


setupVidePlayingListener();

参考:http://www.kinderas.com/technology/2014/6/15/wkwebview-and-javascript-in-ios-8-using-swift

【讨论】:

以上是关于如何检测在 WKWebView 中播放的电影?的主要内容,如果未能解决你的问题,请参考以下文章

如何在WKWebView中检测视频播放器的播放/暂停事件?

如何检测 WKWebView 正在关闭,来自 javascript

有没有办法在 iOS 应用程序的电影播放视图中禁用或检测屏幕录制?

如何在 WKWebview 中添加观察者以播放和暂停音频文件的操作?

如何在WKWebView中禁用YouTube视频的自动播放?

IOS 12 WebRTC - 如何将 RTCMediaStream 传递给 wkwebview?