Search code examples
iosswift2subclassingnsnotification

UIWebView and Swift: Detect when a video starts playing


In Objective-C, I subscribed to the UIWindowDidBecomeVisibleNotification to know if some view gets above my current view controller, using:

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

So far, so good. Then, in the notification, I could check if the object is not of certain classes (like _UIAlertControllerShimPresenterWindow -alert views- or UITextEffectsWindow -native sharing view-). In Objective-C, I did it this way:

- (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
    }
}

This allowed me to stop playing sounds (in this case, an HTTP radio streaming) when starting a video from a UIWebView (which is used to present news). I've tried to do the same thing in Swift, so I subscribed to the notification:

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

And now, when receiving the notification...

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

Xcode says Use of undeclared type '_UIAlertControllerShimPresenterWindow'. The same thing happens with UITextEffectsWindow.

I assume that I have to import something to detect those classes, but what should I import?

Is there any other way to do it without bridging Objective-C? How could I access that class?

Thank you in advance.


Solution

  • you could compare the class name instead the class itself, refer here to get the class name.