# Rewarded Ads

To integrate Rewarded ads, ensure you have integrated the [IASDKCore Libraries](https://docs.digitalturbine.com/dt-exchange/publishers/sdk-configuration/integrating-the-ios-sdk/..#h_01h959gwafx2axgb4wwt1nxt6w). Configure the ad placement and its controllers, create an Ad Request, and then fetch and display the ad.

## Importing the SDK <a href="#h_01k5nqvxah6gp8zgrba5ywaenz" id="h_01k5nqvxah6gp8zgrba5ywaenz"></a>

Import the following `IASDKCore` module into your desired view controller:

{% code title="Objective-C" %}

```objective-c
#import <IASDKCore/IASDKCore.h>
```

{% endcode %}

## Adding Properties <a href="#h_01k5nqvxahk3hdmzwxedx5c8dr" id="h_01k5nqvxahk3hdmzwxedx5c8dr"></a>

Retain each DT Exchange SDK entity, and declare properties for:

* `adSpot` (placement)
* `UnitController`
* `videoContentController`&#x20;
* `mraidConentController`

{% code title="Objective-C" %}

```objective-c
@property (nonatomic, strong) IAAdSpot *adSpot;
@property (nonatomic, strong) IAFullscreenUnitController *unitController;
@property (nonatomic, strong) IAVideoContentController *videoContentController;
@property (nonatomic, strong) IAMRAIDContentController *mraidContentController;
```

{% endcode %}

Optionally, create an `IAUserData` object for better ad targeting:

{% code title="Objective-C" %}

```objective-c
IAUserData *userData = [IAUserData build:^(id  _Nonnull builder) 
{ 
    builder.age = 34;     
    builder.gender = IAUserGenderTypeMale;    
    builder.zipCode = @"90210";
}];
IASDKCore.sharedInstance.userData = userData;
```

{% endcode %}

{% hint style="info" %}
The `build:` method is synchronous and runs on the same thread that it is invoked from, similar to iOS's `enumerateObjectsUsingBlock`. As a result, special memory management like `weak` references are not needed inside the block.
{% endhint %}

Set global variables for SDK bidding provided in `IASDKCore` shared instance:

* `userData`
* `muteAudio`
* `mediationType`
* `debugger`

#### Example:

{% code title="Objective-C" %}

```objective-c
IASDKCore.sharedInstance.userData = userData;
IASDKCore.sharedInstance.muteAudio = YES;
IASDKCore.sharedInstance.mediationType = IAMediationMax.new;
IASDKCore.sharedInstance.debugger = [IADebugger build:^(id  _Nonnull builder) {
        builder.server = @"wv.inner-active";
        builder.mockResponsePath = @"bannerresponseforci";
        builder.database = @"4321";
    }];
```

{% endcode %}

## Creating the Ad Request Object <a href="#h_01k5nqp1p5b66wwqv1ddqbb51y" id="h_01k5nqp1p5b66wwqv1ddqbb51y"></a>

{% hint style="info" %}
This step is not mandatory for SDK Bidding mediation.
{% endhint %}

Initialize an `IAAdRequest` and provide the `spotID` and a `timeout` value:

{% code title="Objective-C" %}

```objective-c
IAAdRequest *adRequest = [IAAdRequest build:^(id  _Nonnull builder) 
{ 
    builder.spotID = @"YOUR SPOT ID";    
    builder.timeout = 5;    
}];
```

{% endcode %}

Optionally, you can opt to mute non-rewarded interstitial ads by adding:

{% code title="Objective-C" %}

```objective-c
//You can also call the mute through the static object
//In both cases, the result is the same:
IASDKCore.sharedInstance().muteAudio = YES
```

{% endcode %}

## Initializing the Video Content Controller <a href="#h_01k5nqvxahf79yfhrg3q67a6tw" id="h_01k5nqvxahf79yfhrg3q67a6tw"></a>

Initialize the `IAVideoContentController`:

{% code title="Objective-C" %}

```objective-c
IAVideoContentController *videoContentController =
    [IAVideoContentController build:^(id _Nonnull builder) {
      builder.videoContentDelegate = self;
      // A delegate should be passed in order to get video content related
      // callbacks;
    }];

self.videoContentController = videoContentController;
// The Video Content ControllerPage should be retained by a client side
```

{% endcode %}

## Declaring Your View Controller <a href="#h_01k5nqvxahr71kz09q0r8ewhqp" id="h_01k5nqvxahr71kz09q0r8ewhqp"></a>

Declare your view controller conforms to the `IAVideoContentDelegate` protocol:

{% code title="Objective-C" %}

```objective-c
@interface YourViewController  ()<IAVideoContentDelegate
```

{% endcode %}

For more information, see [Video Content Delegate Protocols](https://docs.digitalturbine.com/dt-exchange/publishers/sdk-configuration/delegate-protocols#video-content).

## Initializing the MRAID Content Controller <a href="#h_01k5nqvxah1ff3ge05vm34zn83" id="h_01k5nqvxah1ff3ge05vm34zn83"></a>

Initialize the `IAMRAIDContentController`:

{% code title="Objective-C" %}

```objective-c
IAMRAIDContentController *mraidContentController =
    [IAMRAIDContentController build:^(id _Nonnull builder) {
      builder.MRAIDContentDelegate = self;
      // A delegate should be passed in order to get video content related
      // callbacks;
    }];

self.mraidContentController = mraidContentController;
// The MRAID Content ControllerPage should be retained by a client side
```

{% endcode %}

Declare your view controller conforms to `IAMRAIDContentDelegate` protocol:

{% code title="Objective-C" %}

```
@interface YourViewController () <IAVideoContentDelegate, IAMRAIDContentDelegate> 
```

{% endcode %}

For more information, see [HTML/MRAID Delegate Protocols](https://docs.digitalturbine.com/dt-exchange/publishers/sdk-configuration/delegate-protocols#html-mraid).

## Initializing the Full-Screen Unit Controller <a href="#h_01k5nqvxah0r0bj2hgzhkh1wz5" id="h_01k5nqvxah0r0bj2hgzhkh1wz5"></a>

Initialize the `IAMFullscreenUnitController`:

{% code title="Objective-C" %}

```objective-c
IAFullscreenUnitController *fullscreenUnitController =
    [IAFullscreenUnitController build:^(id _Nonnull builder) {
      builder.unitDelegate = self;
      // All the required content controllers should be added to the desired
      // unit controller:
      [builder addSupportedContentController:self.videoContentController];
      [builder addSupportedContentController:self.mraidContentController];
    }];
self.unitController = fullscreenUnitController;
// The Fullscreen Unit Controller should be retained by a client side;
```

{% endcode %}

## Declaring Your View Controller <a href="#h_01k5nqvxah2jw0n7ctjz03wd99" id="h_01k5nqvxah2jw0n7ctjz03wd99"></a>

Declare your view controller conforms to `IAUnitDelegate` protocol:

{% code title="Objective-C" %}

```objective-c
@interface YourViewController () <IAVideoContentDelegate,
                                  IAMRAIDContentDelegate,
                                  IAUnitDelegate>
```

{% endcode %}

For more information, see [Unit Content Delegate Protocols](https://docs.digitalturbine.com/dt-exchange/publishers/sdk-configuration/delegate-protocols#unit).

## Initializing the Placement <a href="#h_01k5nqq6htf1fx89e7qfjg9vdd" id="h_01k5nqq6htf1fx89e7qfjg9vdd"></a>

Initialize the `IAAdSpot` (placement) and pass your `adRequest` object:

{% code title="Objective-C" %}

```objective-c
IAAdSpot *adSpot = [IAAdSpot build:^(id  _Nonnull builder) {
      builder.adRequest = adRequest; 
// pass here the ad request object;
      [builder addSupportedUnitController:self.unitController];
}];

self.adSpot = adSpot; 
// the Ad Spot should be retained by a client side;
```

{% endcode %}

## Fetching the Ad <a href="#id-01h89dgv9g33ejfkre2yjzefgd" id="id-01h89dgv9g33ejfkre2yjzefgd"></a>

The way in which the ad is fetched depends on the mediation type — Waterfall or SDK Bidding.

### Fetching the Ad for Waterfall <a href="#h_01k7y2q4am7j6p7w3yhxd5hz17" id="h_01k7y2q4am7j6p7w3yhxd5hz17"></a>

In Waterfall mediation, use `fetchAdWithCompletion:` and `showAdAnimated:` to fetch the ad:

{% code title="Objective-C" %}

```objective-c
// Declare a weak var, because of a retained block:
__weak typeof(self) weakSelf = self;

[self.adSpot fetchAdWithCompletion:^(IAAdSpot* _Nullable adSpot,
                                     IAAdModel* _Nullable adModel,
                                     NSError* _Nullable error) {
  if (!error) {
    [weakSelf.unitController showAdAnimated:YES completion:nil];
  }
}];
```

{% endcode %}

### Fetching the Ad for SDK Bidding <a href="#h_01k7y2q4amvh15661rj01q98jk" id="h_01k7y2q4amvh15661rj01q98jk"></a>

In SDK Bidding, DT Exchange must first generate a token for the Mediation SDK to use when sending the request to server.

#### **Generating the Token**

The token can be generated upon SDK initialization.

{% code title="Objective-C" %}

```objective-c
NSString *biddingToken = FMPBiddingManager.sharedInstance.biddingToken;
```

{% endcode %}

Return type: `NSString *`

If an error occurs, the returned value is `nil`.

#### **Loading the Ad**

If DT Exchange wins the auction, load the ad with the `signaldata (adm)` that was received from the server:

{% code title="Objective-C" %}

```objective-c
[self.adSpot
    loadAdWithMarkup:adm
      withCompletion:^(IAAdSpot *_Nullable adSpot, IAAdModel *_Nullable adModel,
                       NSError *_Nullable error) {
        if (error) {
          NSLog(@"Failed to get an ad: %@\n", error);
        } else {
          // If is in-view ad unit response, show in view:
          if (adSpot.activeUnitController == weakSelf.viewUnitController) {
            [weakSelf.viewUnitController showAdInParentView:weakSelf.view];
          }
          // If is fullscreen ad unit response, show as fullscreen:
          else if (adSpot.activeUnitController == weakSelf.fsUnitController) {
            [weakSelf.fsUnitController showAdAnimated:YES completion:nil];
          }
        }
      }];
```

{% endcode %}

## Implementing Mandatory Protocols <a href="#id-01h89dgv9gqh22jgs33prpxq14" id="id-01h89dgv9gqh22jgs33prpxq14"></a>

Implement a view controller for an ad (and other related modal screens) presentation:

{% code title="Objective-C" %}

```objective-c
- (UIViewController* _Nonnull)IAParentViewControllerForUnitController:
    (IAUnitController* _Nullable)unitController {
  return self;

  // In case 'self' is a UIViewController subclass;
}
```

{% endcode %}

Use `IAAdDidReward` to receive rewarded ad callbacks:

{% code title="Objective-C" %}

```objective-c
- (void)IAAdDidReward:(IAUnitController * _Nullable)unitController 
{
    NSLog(@"The ad did reward.");
}
```

{% endcode %}

&#x20;
