【问题标题】:UIWebView and Swift: Detect when a video starts playingUIWebView 和 Swift:检测视频何时开始播放
【发布时间】:2015-12-21 20:35:02
【问题描述】:

在 Objective-C 中,我订阅了 UIWindowDidBecomeVisibleNotification 以了解是否某些视图超出了我当前的视图控制器,使用:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(videoStartedPlaying:)
                                             name:UIWindowDidBecomeVisibleNotification
                                           object:nil];

到目前为止,一切都很好。然后,在通知中,我可以检查对象是否属于某些类(例如 _UIAlertControllerShimPresenterWindow -alert views- 或 UITextEffectsWindow -nativesharing view-)。在 Objective-C 中,我是这样做的:

- (void)videoStartedPlaying:(NSNotification *)notification
{
    if (
        <radio_is_playing>
        &&
        ! [notification.object isKindOfClass:NSClassFromString(@"_UIAlertControllerShimPresenterWindow")] // Alert view
        &&
        ! [notification.object isKindOfClass:NSClassFromString(@"UITextEffectsWindow") ] // Share
        )
    {
        // Video, stop the radio stream
    }
}

这让我在从UIWebView(用于呈现新闻)开始播放视频时停止播放声音(在本例中为 HTTP 广播流)。我尝试在 Swift 中做同样的事情,所以我订阅了通知:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "videoStartedPlaying:", name: UIWindowDidBecomeVisibleNotification, object: nil)

现在,当收到通知时...

func videoStartedPlaying(notification: NSNotification) {
    if <radio_is_playing> && !notification.object? is _UIAlertControllerShimPresenterWindow && !notification.object? is UITextEffectsWindow {
        // Stop the radio stream
    }
}

Xcode 说Use of undeclared type '_UIAlertControllerShimPresenterWindow'UITextEffectsWindow 也会发生同样的事情。

我假设我必须导入 something 来检测这些类,但我应该导入什么?

在不桥接 Objective-C 的情况下,有没有其他方法可以做到这一点?我怎样才能访问该课程?

提前谢谢你。

【问题讨论】:

  • 没人涉足这个吗?
  • 我只是检查notification.objevt是否是UIWindow,做暂停的事情

标签: ios swift2 subclassing nsnotification


【解决方案1】:

您可以比较类名而不是类本身,请参考here 以获取类名。

【讨论】:

  • 它使用第一个选项。我之前看到过这个答案,但我不知道为什么当时它不起作用。非常感谢。编辑:作为参考,如果其他人想做类似的事情,在呈现分享动作时(例如按下“消息”后),现在也会出现一个新事件UIRemoteKeyboardWindow
猜你喜欢
  • 2017-02-16
  • 2013-12-13
  • 2012-11-22
  • 2014-11-20
  • 1970-01-01
  • 1970-01-01
  • 2014-10-04
  • 2012-03-19
  • 2012-03-16
相关资源
最近更新 更多