> ## Documentation Index
> Fetch the complete documentation index at: https://docs.interactivelayers.com/llms.txt
> Use this file to discover all available pages before exploring further.

# AVPlayer for iOS and tvOS

> Compose the video and graphics with ILAVPlayerOverlay.

Add the Swift package under `apple/` to your Xcode project and link `ILAVPlayerOverlay` to the iOS 16+ or tvOS 16+ target. Mount one video surface in a container whose rectangle the app controls. Place playback controls above it. The native Swift view uses the existing AVPlayer.

```swift theme={null}
import AVFoundation
import ILAVPlayerOverlay
import UIKit

@MainActor
func attachGraphics(to stage: UIView, player: AVPlayer) -> ILOverlayPlayerView {
    let surface = ILOverlayPlayerView(player: player, source: .hlsDateRanges)
    surface.frame = stage.bounds
    surface.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    stage.insertSubview(surface, at: 0)
    return surface
}
```

The default `.hlsDateRanges` mode reads dated HLS cues. For session JSON, initialize the view with `.sidecar`. Capture `player.currentItem` before fetching, then apply `try surface.replaceSidecar(data, for: item)` on the main actor. Keep responses ordered. `AVPlayerItem.currentDate()` needs a valid program date mapping in both modes.

Set `surface.isInLinearAdBreak` during externally stitched spots and `isPresentationSuspended` while another surface covers playback. Call `surface.stop()` to release observers and restore the original video rectangle. Run `swift test` and verify the HLS sample on the app's target device. Watch layout restoration after a seek, pause and item change. [Shared fixture and cue schema](https://docs.interactivelayers.com/player-sdk.md) · [Apple SDK documentation](https://github.com/InteractiveLayerscom/IL-VideoPlayers-SDK/blob/main/apple/README.md)

## IL ads from the plan

`ILAdPlanPlayer` reads the IL plan, plays the breaks, draws the Super, the L, the **Overlay 300×250** on iOS and the product, and shows **Buy** during an iADServer ad. To play the breaks with IMA, add the `ILAVPlayerOverlayIMA` product and pass `linearEngine: ILAdPlanIMA(overlay: overlay)`; without an engine, breaks use the VAST's MP4. For the Overlay on iOS, add `ILAVPlayerOverlayGMA` and `GADApplicationIdentifier` in `Info.plist`. There are two ways to use it. [Which to choose](https://docs.interactivelayers.com/player-sdk.md#native-app-ads)

### The SDK requests the plan

In **Settings → General → My own player**, choose AVPlayer and register the bundle id in **Bundle ID of the Apple app**. `load` requests `GET https://console.interactivelayers.com/il-player/app-plan?project=…&asset=…&app=apple:<bundle id>`, over HTTPS only and without cookies, and never holds the content back for more than 5 seconds.

```swift theme={null}
let plan = await ILAdPlanPlayer.load(overlay: surface, player: player, origin: "https://console.interactivelayers.com",
                                     project: projectID, asset: assetID, locale: "en",
                                     linearEngine: nil, events: { type, format, id in print(type, format ?? "-", id) })
await plan?.start()
player.play()
```

* Without a plan, `load` returns `nil` and emits `plan_unavailable` once, with the `app=` value as `id` and the reason as format: `http_204` (no plan yet, or a limit was reached), `http_404` (unknown project, video or app: check the registration first), `timeout`, `network`, `app_id`, `stitched` or `invalid`. The content plays anyway.
* A `version: 2` plan expires after 120 s. `load` requests it again up to three times, 20, 12 and 5 s before expiry, with the same shopping session, and stops at the first success (`plan_renewed`). Only if all three fail does it emit `plan_renew_failed`, once; on expiry it emits `plan_expired`, removes what it drew and the content continues. If you get the plan yourself, renew it with `replacePlan(_:)`.
* On the Google Ad Manager requests the SDK makes itself, it adds `msid` (bundle id) and `an` (app name) when the tag lacks them, and never `url` or `description_url`. Tags handed to IMA are left untouched: IMA adds the app identity on its own.
* With `commerceEnabled = true` and `commerce.break_url` in the plan, the SDK requests the product when an iADServer ad with shopping starts and draws **Buy** while that ad lasts.

### The app plays its breaks

If the app requests its breaks from the [per-break API](https://docs.interactivelayers.com/vod-operations.md#app-break-graphics), your backend creates the playback with `"graphics": true` and passes `GET /v1/vod/playbacks/{id}/plan` to the app. Attach that plan and tell the SDK about each break and each ad. It works with `ILAVPlayerOverlay` alone, without IMA or GMA.

```swift theme={null}
plan.setBreaks([58, 178])                 // your break positions, in content seconds
plan.breakStarted(at: 58)                 // when your break starts; 0 for a pre-roll
plan.adStarted(tagURL: adTagURL, adID: imaAd.adId, creativeID: imaAd.creativeID)
// or, with the VAST XML: plan.adStarted(tagURL: adTagURL, vastXML: vast)
plan.adEnded()                            // on complete, skip or error
plan.breakEnded()
```

* **Report the breaks with `setBreaks` before they arrive.** The SDK applies `scenes.pacing.cut_clearance_seconds` only around reported breaks. Without reports, graphics follow content time and there is no **Buy**.
* **Do not pass your own `sessionID`.** **Buy** needs the session to be the plan's `commerce.session`. If you pass another one and the plan has `break_url`, the SDK says so once with `buy_unavailable` (reason `session`).
* **Fetch `ad_tag_url` exactly as given**, without touching `sid` or `correlator`.
* With a plan that carries its own breaks, `breakStarted` is refused with `break_refused` (`plan_has_cuts`): two ways of driving breaks are never mixed.
* A break without `breakEnded()` is ended after 300 s with `break_error`. The SDK never pauses, seeks or replaces your item during your break.

### Limits and verification

* The **Overlay 300×250** is not available on tvOS: Google's display SDK declares iOS only.
* The banner designed in iADServer is not drawn: the app shows the product card.
* On Apple, the **Buy** panel closes when the ad ends, even if the viewer opened it.
* A project with server-side insertion gets a plan the SDK refuses (`stitched`): the app plays without IL ads.
* Package tests run on Mac Catalyst and on the iPhone 17 and Apple TV 4K simulators, and the example app was tested on those simulators with a simulated ad: a local clip served by a local stand-in for the ad server. No real Google Ad Manager, IMA or iADServer ad was played, and nothing ran on an iPhone, an iPad or an Apple TV.

[Ad plan on Apple](https://github.com/InteractiveLayerscom/IL-VideoPlayers-SDK/blob/main/apple/README.md#plan-de-anuncios)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.