# How to subscribe to ad events on iOS

When building a custom UI, or when logging events to an analytics service, app developers often need to be able to listen (and react) to ad events. For example, if an ad break starts, they want to draw a countdown on top of their UI. Alternatively, when an ad error occurs, they want to log this to an analytics server.

This article explains how you can programmatically subscribe to ad-related events.

## Subscribing to an event

This subsection explains how you detect ad-related events. The other subsections zoom in on particular use-cases, such as detecting the beginning and end of an ad.

If you are scheduling [client-side advertisements](https://docs-preview.optiview.dolby.com/pr-863/theoplayer/how-to-guides/ios/ads/set-up-vast-and-vmap.md), then you use the `Ads` interface to detect ad events.

The `Player` API exposes an [`ads`](https://docs-preview.optiview.dolby.com/pr-863/theoplayer/v11/api-reference/ios/Classes/THEOplayer#/s:13THEOplayerSDK0A0C3adsAA3Ads_pvp) property which belongs to the [`Ads`](https://docs-preview.optiview.dolby.com/pr-863/theoplayer/v11/api-reference/ios/Protocols/Ads) interface. Because this `Ads` interface inherits from [`EventDispatcher`](https://docs-preview.optiview.dolby.com/pr-863/theoplayer/v11/api-reference/ios/Ads%20Events), you can leverage event listeners in this interface.

The ad-related events are documented in [AdsEventTypes](https://docs-preview.optiview.dolby.com/pr-863/theoplayer/v11/api-reference/ios/Structs/AdsEventTypes).

To subscribe to an event, you select an event from this page, use the `Ads` interface, call the `addEventListener` method, and set the event as the first parameter, and the callback as the second parameter.

For example, if you want to track the start of an ad break, you could use the following snippet:

```swift
player.ads.addEventListener(type: AdsEventTypes.AD_BREAK_BEGIN) { (event) in
    print(event)
}
```

## Ad begin and end

Detecting the start and stop of ad breaks and individual ad pods is a common requirement.

This use-case is an application of [Subscribing to an event](#subscribing-to-an-event), and relates to the following events:

* `AD_BEGIN`
* `AD_BREAK_BEGIN`
* `AD_END`
* `AD_BREAK_END`

```swift
player.ads.addEventListener(type: AdsEventTypes.AD_BREAK_BEGIN) { (event) in
    print(event)
}
```

## Ad error

Ad error events might be triggered when an ad blocker is active, or when an "empty" ad is returned. As an app developer, you might want to detect and possibly react to this.

This use-case is an application of [Subscribing to an event](#subscribing-to-an-event), specifically the `AD_ERROR` event.

The following snippet would trigger the callback when an ad error occurs:

```swift
player.ads.addEventListener(type: AdsEventTypes.AD_ERROR) { (event) in
    print(event)
}
```

## Server-side ads

If you're scheduling server-side ads, you might need to use a different interface than the `Ads` interface which you use when scheduling client-side ads.

* [Google DAI](https://docs-preview.optiview.dolby.com/pr-863/theoplayer/how-to-guides/ios/ads/google-dai.md): if you're using Google DAI, you can use the same API as the one for client-side ads, as described in the sections above.
* [Yospace](https://docs-preview.optiview.dolby.com/pr-863/theoplayer/how-to-guides/ios/ads/yospace.md): if you're using Yospace, you must use the Yospace interface. You can read more about detecting ad-related events with Yospace at our [Yospace documentation](https://docs-preview.optiview.dolby.com/pr-863/theoplayer/how-to-guides/ios/ads/yospace.md).

If you're building a custom server-side ad insertion solution, you might be interested in using our `TextTrack` API to detect `id3/emsg/EventStream/EXT-X-DATERANGE` cues, and the `timeupdate` event in the `Player` interface to determine the current playhead position.

## Resources

* More samples are available on our GitHub repository for the [iOS SDK](https://github.com/THEOplayer/samples-ios-sdk).
* [THEOplayer advertisement tester](https://www.theoplayer.com/theoplayer-demo-advertisement-tester-vpaid-vast-vmap)
