# Banner/MREC Ads

To integrate Banner/MREC 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="#id-01h8e3fqsmwn10s7y43akm1pdm" id="id-01h8e3fqsmwn10s7y43akm1pdm"></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="#id-01h8e3fqsnp2bazspsrf4n0047" id="id-01h8e3fqsnp2bazspsrf4n0047"></a>

Retain each `IASDK` module on the client side, and declare properties, for example:

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

```objective-c
@property (nonatomic, strong) IAAdSpot *adSpot;
@property (nonatomic, strong) IAViewUnitController*viewUnitController;
@property (nonatomic, strong) IAMRAIDContentController*mraidContentController;
```

{% endcode %}

Optionally, create the `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, 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="#id-01h89m2e9jyr2t7srjqetzchqy" id="id-01h89m2e9jyr2t7srjqetzchqy"></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 = 10; 
}];
```

{% endcode %}

## Initializing the MRAID Content Controller <a href="#id-01h8e3vmc7tps9j55yfk8ta180" id="id-01h8e3vmc7tps9j55yfk8ta180"></a>

Initialize the `IAMRAIDContentController`:

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

```objective-c
IAMRAIDContentController *mraidContentController =
[IAMRAIDContentController build: ^(id<IAMRAIDContentControllerBuilderh>  _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 %}

## Declaring Your View Controller <a href="#id-01h89dgv9f5wd3z51wsggjyag7" id="id-01h89dgv9f5wd3z51wsggjyag7"></a>

Declare that your view controller conforms to the `IAMRAIDContentDelegate` protocol:

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

```objective-c
@interface YourViewController () <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 View Unit Controller <a href="#id-01h8e4r05n1c0f9ae2zrreewfc" id="id-01h8e4r05n1c0f9ae2zrreewfc"></a>

Initialize the `IAViewUnitController`:

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

```objective-c
IAViewUnitController *viewUnitController = [IAViewUnitController
    build:^(id<IAViewUnitControllerBuilder> _Nonnull builder) {
      builder.unitDelegate = self;
      // all the required content controllers should be added to the desired
      // unit controller:
      [builder addSupportedContentController:self.mraidContentController];
    }];

self.viewUnitController = viewUnitController;
// the View Unit Controller should be retained by a client side;
```

{% endcode %}

## Declaring Your View Controller <a href="#id-01h8e4d3128zwwmex23d9p51t1" id="id-01h8e4d3128zwwmex23d9p51t1"></a>

Declare that your view controller conforms to the `IAUnitDelegate` protocol:

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

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

{% 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="#id-01h89dgv9fvtnan2petmwenpmq" id="id-01h89dgv9fvtnan2petmwenpmq"></a>

Initialize the `IAAdSpot` and pass your `adRequest` object:

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

```objective-c
IAAdSpot *adSpot = [IAAdSpot build:^(id<IAAdSpotBuilder>  _Nonnull builder) {
    builder.adRequest = adRequest; 
    // pass here the ad request object;    
    // all the supported (by a client side) unit controllers,
    // (in this case - view unit controller) should be added to the desired ad spot:     
    [builder addSupportedUnitController:self.viewUnitController];
}];

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

{% endcode %}

## Fetching the Ad <a href="#id-01h89dgv9fmy9zfr2v60evr0ch" id="id-01h89dgv9fmy9zfr2v60evr0ch"></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_01k7xyfzm4wa9xg8pcqw01qpax" id="h_01k7xyfzm4wa9xg8pcqw01qpax"></a>

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

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

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

[self.adSpot fetchAdWithCompletion:^(IAAdSpot* _Nullable adSpot,
                                     IAAdModel* _Nullable adModel,
                                     NSError* _Nullable error) {
  if (error) {
    NSLog(@"Failed to get an ad: %@\n", error);
  } else {
    if (adSpot.activeUnitController == weakSelf.viewUnitController) {
      [weakSelf.viewUnitController showAdInParentView:weakSelf.view];
    }
  }
}];
```

{% endcode %}

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

In SDK Bidding, DT Exchange must first generate a token for the Mediation SDK to use when sending the request to the 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 %}

<br>
