---
metadata:
  - name: generator
    content: Diplodoc Platform v5.63.2
alternate:
  - https://ads.yandex.com/helpcenter/en/dev/ios/swiftui/rewarded.md
  - https://ads.yandex.com/helpcenter/ru/dev/ios/swiftui/rewarded.md
  - https://ads.yandex.com/helpcenter/zh/dev/ios/swiftui/rewarded.md
  - href: https://ads.yandex.com/helpcenter/en/dev/ios/swiftui/rewarded.md
    type: text/markdown
    title: Markdown version
  - href: https://ads.yandex.com/helpcenter/en/llms.txt?revision=r21589873
    rel: describedby
---
> **Documentation Index:** Fetch the complete configuration index at https://ads.yandex.com/helpcenter/en/llms.txt?revision=r21589873

# Rewarded ads (SwiftUI)

<!-- source: en/dev/_includes/rewarded.md -->
Rewarded ads are a popular fullscreen ad format where users receive incentives for viewing ads.
<!-- endsource: en/dev/_includes/rewarded.md -->

<!-- source: en/dev/_includes/rewarded.md -->
Ad impressions are opt-in: for example, users can initiate them to get game bonuses or extra lives.
<!-- endsource: en/dev/_includes/rewarded.md -->

<!-- source: en/dev/_includes/rewarded.md -->
Strong user motivation makes this ad format the most popular and profitable adoption in free apps.
<!-- endsource: en/dev/_includes/rewarded.md -->

{% cut "Appearance" %}

{% list tabs %}

- Combinatorial ads

  <iframe width="200" height="405.5" allow="autoplay" src="https://runtime.strm.yandex.ru/player/video/vplv6tshzb3euhokoa5q?autoplay=1&mute=0&loop=1&loop=1" frameborder="0" allowfullscreen></iframe>

- Video ad

  <iframe width="200" height="405.5" allow="autoplay" src="https://runtime.strm.yandex.ru/player/video/vplvv2aom7x2nte5gckn?autoplay=1&mute=0&loop=1&loop=1" frameborder="0" allowfullscreen></iframe>

{% endlist %}

{% endcut %}

This guide shows you how to integrate rewarded ads into an iOS app using SwiftUI. Besides code samples and instructions, it contains recommendations and links to additional resources.

## Prerequisite {#pre}

<!-- source: en/dev/_includes/pre-ios.md -->
1. Follow the SDK integration steps described under [Quick start](https://ads.yandex.com/helpcenter/en/dev/ios/quick-start.md).
2. First, you need to [initialize](https://ads.yandex.com/helpcenter/en/dev/ios/quick-start.md#init) the advertising SDK.
3. Make sure you're using the [latest version of the Yandex Mobile Ads SDK](https://ads.yandex.com/helpcenter/en/dev/platforms.md), and if you're using mediation, the latest version of the [unified build](https://ads.yandex.com/helpcenter/en/dev/platforms.md).
<!-- endsource: en/dev/_includes/pre-ios.md -->

## Implementation {#implement}

Key steps to integrate rewarded ads using SwiftUI:

1. Add the `.rewardedAd(isPresented:request:onEvent:)` modifier.
1. Manage loading using `Binding<AdRequest?>` and display via `Binding<Bool>`.
1. Handle `RewardedAdEvent` events in the `onEvent` closure.
1. Reward the user for watching the ad on the `.didReward` event.

## Features of rewarded ad integration {#features}

1. If the `.didFailToLoad` event returns an error, don't try to load a new ad again. If you need to retry the load from the error handler, limit the number of attempts to avoid endless failed requests and connection issues.

2. Apply the modifier to a `view` that persists throughout the display scenario.

## Loading ads {#load}

To load a rewarded ad, set a non-nil `AdRequest` in the `Binding` passed to `.rewardedAd`.

To get notified about successful and failed loads, as well as other events, use `onEvent` with `RewardedAdEvent`.

```swift
import SwiftUI
import YandexMobileAds

struct RewardedView: View {
    @State private var adRequest: AdRequest?
    @State private var isPresented = false

    var body: some View {
        VStack {
            Button("Load and show") {
                adRequest = AdRequest(adUnitID: "R-M-XXXXX-YY")
            }
        }
        .rewardedAd(isPresented: $isPresented, request: $adRequest) { event in
            switch event {
            case .didLoad:
                isPresented = true
            case .didDismiss, .didFailToShow, .didFailToLoad:
                adRequest = nil
            default:
                break
            }
        }
    }
}
```

To load an ad, you need the ad placement ID obtained in the Yandex Advertising Network interface (adUnitId).

You can expand the ad request parameters using `AdRequest`. To do this, pass information about the user's interests, page context, location, and other additional data in the request. Context can greatly improve ad relevance. To learn more, see [Ad targeting](https://ads.yandex.com/helpcenter/en/dev/ios/target.md).

## Displaying ads {#ad-view}

<!-- source: en/dev/_includes/rewarded.md -->
Rewarded ads are an incentivized ad format that allows users to earn rewards by viewing ads. These rewards may include extra lives or the ability to advance to the next level in a game. The reward format is determined by the app itself.
<!-- endsource: en/dev/_includes/rewarded.md -->

Once the ad loads successfully, you'll receive a `.didLoad` event. Set `isPresented = true` to display the ad, or pre-set the presentation flag. Presentation will be delayed if the ad hasn't finished loading.

```swift
struct RewardedView: View {
    @State private var adRequest: AdRequest?
    @State private var isPresented = false

    var body: some View {
        ContentView()
            .rewardedAd(isPresented: $isPresented, request: $adRequest) { event in
                switch event {
                case .didLoad:
                    isPresented = true
                case .didDismiss, .didFailToShow, .didFailToLoad:
                    adRequest = nil
                default:
                    break
                }
            }
    }
}
```

## Reward issuance {reward-send}

Once the ad impression is recorded, `.didReward(Reward)` is sent to `onEvent`. Use this method to reward the app user. You should grant the reward in the `.didReward` handler without relying solely on `.didDismiss`.

```swift
struct RewardedView: View {
    @State private var adRequest: AdRequest?
    @State private var isPresented = false

    var body: some View {
        ContentView()
            .rewardedAd(isPresented: $isPresented, request: $adRequest) { event in
                switch event {
                case .didReward(let reward):
                    sendReward(reward)
                case .didDismiss, .didFailToShow, .didFailToLoad:
                    adRequest = nil
                default:
                    break
                }
            }
    }

    private func sendReward(_ reward: Reward) {
        // Grant the reward
    }
}
```

## Testing rewarded ad integration {#test}

### Using demo ad units for ad testing

<!-- source: en/dev/_includes/test-ios-rewarded.md -->
Use test ads to check your ad integration and the app itself. To make sure that test ads are returned for each ad request, you can use a special demo ad placement ID.

Demo adUnitId: `demo-rewarded-yandex`.

{% note warning %}

Before publishing your app in the store, make sure to replace the demo placement ID with the real ID you obtained in the Yandex Advertising Network interface.

{% endnote %}

For the list of all available demo ad placement IDs, see [Demo ad units for testing](https://ads.yandex.com/helpcenter/en/dev/ios/demo-blocks.md).
<!-- endsource: en/dev/_includes/test-ios-rewarded.md -->

### Testing ad integration

<!-- source: en/dev/_includes/test-integration-ios.md -->
You can test your ad integration using the native Console tool.

To view detailed logs, call the `YandexAds` class's `enableLogging` method.

```swift
YandexAds.enableLogging()
```

To view SDK logs, go to the Console tool and set `Subsystem = com.mobile.ads.ads.sdk`. You can filter logs by category or error level.

If you're having problems integrating ads, you'll get a detailed report on the issues and recommendations for how to fix them.

<img src= "https://yastatic.net/s3/doc-binary/src/dev/mobile-ads/common/integration-ios-2.png">
<!-- endsource: en/dev/_includes/test-integration-ios.md -->

## Additional resources {#resources}

* <!-- source: en/dev/_includes/github-pubdev-links.md -->
  Link to [GitHub](https://github.com/yandexmobile/yandex-ads-sdk-ios/blob/master/Examples/YandexMobileAdsExample/YandexMobileAdsExample/Yandex/Rewarded/RewardedAdViewController.swift).
  <!-- endsource: en/dev/_includes/github-pubdev-links.md -->
