Listen for ad events

Listen for events from advertisements and glean information from these events.


You can listen for ad events by assigning a delegate to the JWP adDelegate property. These events are surfaced using the following method.

func jwplayer(_ player: AnyObject, adEvent event: JWAdEvent)

The player is the instance of JWPlayer which has emitted the event. The event is an instance of JWAdEvent which supplies the following information:

  • Client: This is the ad client being used to present the ad. This is represented as a JWAdClient enumeration value.
  • Type: This is the type of event. This is represented as a JWAdEventType enumeration value.

The properties for the ad event can be queried using a subscript operator. The key for the subscript is a JWEventKey enumeration type. The subscript returns an optional type Any? that must be typecast into a type when retrieved.

let tag = event[.tag] as? URL

If a property is not available, nil is returned.

📘

For a list of types returned for each property, refer to the header documentation for JWAdEventKey or the Event Properties Reference.



Requirement

  • JWPlayerKit 4.0.0+


Listen for ad events

When listening for ad events, you can use the JWPlayerViewController or JWPlayerView.

JWPlayerViewController

When using the JWPlayerViewController, use the following steps to listen for ad events within your content:

  1. Create a subclass of JWPlayerViewController.
  2. Override the ad event method to listen for and handle ad events from the SDK.
class PlayerViewController: JWPlayerViewController {
     // This example assumes you have created a config for your player.

     override func jwplayer(_ player: AnyObject, adEvent event: JWAdEvent) {
          switch event.type {
          case .impression:
               guard let tag = event[.tag] as? URL else {
                    break
               } 

               // Handle tag or other properties here.
          default:
               break
          }
     } 
}

JWPlayerView

When using JWPlayerView and creating your own interface, use the following steps to listen for ad events within your content:

  1. Instantiate an instance of JWPlayerView.
  2. Assign a delegate object, which conforms to JWAdDelegate.
  3. Listen for ad events in your ad delegate.
class PlayerViewController: UIViewController, JWAdDelegate {
     // This example assumes you have created a JWPlayerView in a storyboard or XIB.
     @IBOutlet var playerView: JWPlayerView!

     override func viewDidLoad() {
          super.viewDidLoad()

          // This example assumes you have created a config for your player. This is covered in other documentation.

          playerView.player.adDelegate = self
     }

     override func jwplayer(_ player: AnyObject, adEvent event: JWAdEvent) {
          switch event.type {
          case .impression:
               guard let tag = event[.tag] as? URL else {
                    break
               } 

               // Handle tag or other properties here.
          default:
               break
          }
     } 
}



Did this page help you?