<br />

[Rewarded interstitial](https://support.google.com/admanager/answer/7386053) is a type
of incentivized ad format that allows you offer rewards for ads that appear
automatically during natural app transitions. Unlike rewarded ads, users aren't
required to opt-in to view a rewarded interstitial.

## Prerequisites

Before you continue, [set up Google Mobile Ads SDK](https://developers.google.com/ad-manager/mobile-ads-sdk/ios/quick-start.md.txt).

## Implementation

The primary steps to integrate rewarded interstitial ads are as follows:

- Load an ad
- \[Optional\] Validate SSV callbacks
- Register for callbacks
- Display the ad and handle the reward event

### Load an ad

Loading an ad is accomplished using the [`load(adUnitID:request)`](https://developers.google.com/ad-manager/mobile-ads-sdk/ios/api/reference/Classes/GADRewardedInterstitialAd.md.txt#+loadwithadunitid:request:completionhandler:)
method on the `GADRewardedInterstitialAd` class.

### Swift

```swift
func loadAdManagerRewardedInterstitialAd() async {
  do {
    rewardedInterstitialAd = try await RewardedInterstitialAd.load(
      // Replace this ad unit ID with your own ad unit ID.
      with: "adUnitID", request: AdManagerRequest())
    rewardedInterstitialAd?.fullScreenContentDelegate = self
  } catch {
    print("Rewarded ad failed to load with error: \(error.localizedDescription)")
  }
}
```

### SwiftUI

```swift
import GoogleMobileAds

class RewardedInterstitialViewModel: NSObject, ObservableObject,
  FullScreenContentDelegate
{
  @Published var coins = 0
  private var rewardedInterstitialAd: RewardedInterstitialAd?

  func loadAd() async {
    do {
      rewardedInterstitialAd = try await RewardedInterstitialAd.load(
        with: "ca-app-pub-3940256099942544/6978759866", request: Request())
      rewardedInterstitialAd?.fullScreenContentDelegate = self
    } catch {
      print(
        "Failed to load rewarded interstitial ad with error: \(error.localizedDescription)")
    }
  }
```

### Objective-C

```objective-c
- (void)loadAdManagerRewardedInterstitialAd {
  [GADRewardedInterstitialAd loadWithAdUnitID:"adUnitID"
                                      request:[GAMRequest request]
                            completionHandler:^(GADRewardedInterstitialAd *ad, NSError *error) {
                              if (error) {
                                NSLog(@"Failed to load rewarded interstitial ad with error: %@",
                                      error.localizedDescription);
                                return;
                              }
                              self.rewardedInterstitialAd = ad;
                              self.rewardedInterstitialAd.fullScreenContentDelegate = self;
                            }];
}
```

Replace <var translate="no">adUnitID</var> with your ad unit ID.


> [!TIP]
> **Tip:** You can use ad load calls to build up a cache of preloaded ads before you intend to show them, so that ads can be shown with zero latency when needed. Because ads expire after an hour, you should clear this cache and reload with new ads every hour.

<br />

### \[Optional\] Validate server-side verification (SSV) callbacks

Apps that require extra data in [server-side
verification](https://developers.google.com/ad-manager/mobile-ads-sdk/ios/ssv) callbacks should use the
custom data feature of rewarded ads. Any string value set on a rewarded ad
object is passed to the `custom_data` query parameter of the SSV callback. If no
custom data value is set, the `custom_data` query parameter value won't be
present in the SSV callback.

The following code sample demonstrates how to set custom data on a rewarded
interstitial ad object before requesting an ad.

### Swift

```swift
private func validateAdManagerServerSideVerification() async {
  do {
    rewardedInterstitialAd = try await RewardedInterstitialAd.load(
      // Replace this ad unit ID with your own ad unit ID.
      with: "adUnitID", request: AdManagerRequest())
    let options = ServerSideVerificationOptions()
    options.customRewardText = ""SAMPLE_CUSTOM_DATA_STRING""
    rewardedInterstitialAd?.serverSideVerificationOptions = options
  } catch {
    print("Rewarded ad failed to load with error: \(error.localizedDescription)")
  }
}
```

### Objective-C

```objective-c
- (void)validateAdManagerServerSideVerification {
  // Replace this ad unit ID with your own ad unit ID.
  [GADRewardedInterstitialAd loadWithAdUnitID:"adUnitID"
                                      request:[GAMRequest request]
                            completionHandler:^(GADRewardedInterstitialAd *ad, NSError *error) {
                              if (error) {
                                NSLog(@"Rewarded interstitial ad failed to load with error: %@",
                                      error.localizedDescription);
                                return;
                              }
                              self.rewardedInterstitialAd = ad;
                              GADServerSideVerificationOptions *options =
                                  [[GADServerSideVerificationOptions alloc] init];
                              options.customRewardString = @""SAMPLE_CUSTOM_DATA_STRING"";
                              ad.serverSideVerificationOptions = options;
                            }];
}
```

Replace <var translate="no">SAMPLE_CUSTOM_DATA_STRING</var> with your custom data.

> [!IMPORTANT]
> **Key Point:** The custom reward string is [percent
> escaped](https://en.wikipedia.org/wiki/Percent-encoding) and might require decoding when parsed from the SSV callback.

### Register for callbacks

To receive notifications for presentation events, you must assign the
`GADFullScreenContentDelegate` to the `fullScreenContentDelegate` property of
the returned ad:

### Swift

```swift
rewardedInterstitialAd?.fullScreenContentDelegate = self
```

### SwiftUI

```swift
rewardedInterstitialAd?.fullScreenContentDelegate = self
```

### Objective-C

```objective-c
self.rewardedInterstitialAd.fullScreenContentDelegate = self;
```

The `GADFullScreenContentDelegate` protocol handles callbacks for when the ad
presents successfully or unsuccessfully, and when it is dismissed. The following
code shows how to implement the protocol and assign it to the ad:

### Swift

```swift
func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
  print("\(#function) called.")
}

func adDidRecordClick(_ ad: FullScreenPresentingAd) {
  print("\(#function) called.")
}

func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called.")
}

func adWillDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called.")
}

func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called.")
  // Clear the rewarded interstitial ad.
  rewardedInterstitialAd = nil
}

func ad(
  _ ad: FullScreenPresentingAd,
  didFailToPresentFullScreenContentWithError error: Error
) {
  print("\(#function) called with error: \(error.localizedDescription).")
}
```

### SwiftUI

```swift
func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func adDidRecordClick(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func ad(
  _ ad: FullScreenPresentingAd,
  didFailToPresentFullScreenContentWithError error: Error
) {
  print("\(#function) called")
}

func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func adWillDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
  // Clear the rewarded interstitial ad.
  rewardedInterstitialAd = nil
}
```

### Objective-C

```objective-c
- (void)adDidRecordImpression:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adDidRecordClick:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adWillPresentFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adWillDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adDidDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
  // Clear the rewarded interstitial ad.
  self.rewardedInterstitialAd = nil;
}

- (void)ad:(id)ad didFailToPresentFullScreenContentWithError:(NSError *)error {
  NSLog(@"%s called with error: %@", __PRETTY_FUNCTION__, error.localizedDescription);
}
```

### Display the ad and handle the reward event

> [!IMPORTANT]
> **Key Point:** Before displaying a rewarded interstitial ad to users, you must present the user with an intro screen that provides clear reward messaging and an option to skip the ad before it starts. [Learn more](https://support.google.com/admanager/answer/7386053).

When presenting your ad, you must provide a `GADUserDidEarnRewardHandler` object
to handle the reward for the user.

The following code presents the best method for displaying a rewarded
interstitial ad.

### Swift

```swift
func showRewardedInterstitialAd() {
  guard let rewardedInterstitialAd = rewardedInterstitialAd else {
    return print("Ad wasn't ready.")
  }

  // The UIViewController parameter is an optional.
  rewardedInterstitialAd.present(from: nil) {
    let reward = rewardedInterstitialAd.adReward
    print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)")
    // TODO: Reward the user.
  }
}
```

### SwiftUI

Listen to UI events in the view to display the ad.

```swift
var rewardedInterstitialBody: some View {
  // ...
  }
  .onChange(
    of: showAd,
    perform: { newValue in
      if newValue {
        viewModel.showAd()
      }
    }
  )
```

Present the rewarded interstitial ad from the view model:

```swift
func showAd() {
  guard let rewardedInterstitialAd = rewardedInterstitialAd else {
    return print("Ad wasn't ready.")
  }

  rewardedInterstitialAd.present(from: nil) {
    let reward = rewardedInterstitialAd.adReward
    print("Reward amount: \(reward.amount)")
    self.addCoins(reward.amount.intValue)
  }
}
```

### Objective-C

```objective-c
- (void)showRewardedInterstitialAd {
  [self.rewardedInterstitialAd presentFromRootViewController:self
                                    userDidEarnRewardHandler:^{
                                      GADAdReward *reward = self.rewardedInterstitialAd.adReward;

                                      NSString *rewardMessage = [NSString
                                          stringWithFormat:@"Reward received with "
                                                           @"currency %@ , amount %ld",
                                                           reward.type, [reward.amount longValue]];
                                      NSLog(@"%@", rewardMessage);
                                      // TODO: Reward the user.
                                    }];
}
```

## Examples on GitHub

View the full rewarded interstitial ads examples in your preferred language:

- [Swift](https://github.com/googleads/googleads-mobile-ios-examples/tree/main/Swift/admob/RewardedInterstitialExample)
- [SwiftUI](https://github.com/googleads/googleads-mobile-ios-examples/tree/main/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Rewarded-Interstitial)
- [Objective-C](https://github.com/googleads/googleads-mobile-ios-examples/tree/main/Objective-C/admob/RewardedInterstitialExample)

## Next steps

Learn more about [user privacy](https://developers.google.com/ad-manager/mobile-ads-sdk/ios/privacy.md.txt).