# iOS > Integration guides > Native Ad

Source: https://help-display-sdk.equativ.com/ios/integration/native-ad.html

This page explains how to use **Equativ Display SDK** to display a native ad in your application.

---

## Overview

Native ads are loaded and displayed by [`SASNativeAdView`](https://help-display-sdk.equativ.com/ios/API/Classes/SASNativeAdView.html) instances.

To load and display a native ad, you will need:
* A fully configured **Equativ Display SDK**.
* An instance of [`SASAdPlacement`](https://help-display-sdk.equativ.com/ios/API/Classes/SASAdPlacement.html) that will be used to perform ad calls to the delivery engine.
* An instance of [`SASNativeAdView`](https://help-display-sdk.equativ.com/ios/API/Classes/SASNativeAdView.html) that will load and show the native ad.
* A view controller implementing the [`SASNativeAdViewDelegate`](https://help-display-sdk.equativ.com/ios/API/Protocols/SASNativeAdViewDelegate.html) protocol.

The next sections describe in details how to create and load a native ad.

You can also refer to the [samples](https://github.com/smartadserver/equativ-display-sdk-samples-ios) if you want to copy/paste the whole integration.

## Importing the SDK

Before using the SDK, you must import the framework:

``` swift
import SASDisplayKit
```

## Configuring the SDK

The SDK needs to be configured before making any ad calls. You will have to call the method [`configure()`](https://help-display-sdk.equativ.com/ios/API/Classes/SASConfiguration.html#/c:objc(cs)SASConfiguration(im)configure) of [`SASConfiguration`](https://help-display-sdk.equativ.com/ios/API/Classes/SASConfiguration.html) shared instance to do so.

This method should be called as soon as possible. A good place to do it is in your application's delegate, in the `application(_:didFinishLaunchingWithOptions:)` method.

``` swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  // ...

  // Configuring the SDK
  SASConfiguration.shared.configure()

  // ...
}
```

Note that any ad call performed before calling the [`configure()`](https://help-display-sdk.equativ.com/ios/API/Classes/SASConfiguration.html#/c:objc(cs)SASConfiguration(im)configure) method will fail.

## Creating a placement

You will need an _ad placement_ to perform an ad call.

Creating an ad placement is done by instantiating a [`SASAdPlacement`](https://help-display-sdk.equativ.com/ios/API/Classes/SASAdPlacement.html) object using a _site ID_, a _page ID_ and a _format ID_. You can also provide some additional information, like targeting informations (more information in the [API documentation](https://help-display-sdk.equativ.com/ios/API/Classes/SASAdPlacement.html)).

``` swift
let adPlacement = SASAdPlacement(siteId: SOME_SITE_ID, pageId: SOME_PAGE_ID, formatId: SOME_FORMAT_ID)
```

> You can add several information to your _ad placements_ in order to **increase the monetization**.
>
> For instance you can provide a [_Seller Defined Audience_](https://help-display-sdk.equativ.com/sellerdefinedaudience.html.md) object, or a [_Supply Chain Object_](https://help-display-sdk.equativ.com/ios/API/Classes/SASAdPlacement.html#/c:objc(cs)SASAdPlacement(py)supplyChainObjectString) if your are an inventory reseller.

For testing purposes, it is possible to instantiate generic ad placements that will always deliver an ad of a particular format. This is done by using the [`SASAdPlacement(testAd:)`](https://help-display-sdk.equativ.com/ios/API/Classes/SASAdPlacement.html#/c:objc(cs)SASAdPlacement(im)initWithTestAd:) initializer (check the [`SASAdPlacementTest`](https://help-display-sdk.equativ.com/ios/API/Enums/SASAdPlacementTest.html) enum for an exhaustive list of the formats you can get using test placements).

Don't forget to remove all test placements before releasing your app!

## Native ad view instantiation

A native ad is displayed using a [`SASNativeAdView`](https://help-display-sdk.equativ.com/ios/API/Classes/SASNativeAdView.html) view instance.

Note that you should always set the current `UIViewController` as the [`modalParentViewController`](https://help-display-sdk.equativ.com/ios/API/Classes/SASNativeAdView.html#/c:objc(cs)SASNativeAdView(py)modalParentViewController) of the native ad view. The native ad might not be able to handle click events properly if you don't provide this information.

``` swift
// Instantiating the native ad view
let nativeAdView = SASNativeAdView(frame: .zero)

// Setting the modal parent view controller
nativeAdView.modalParentViewController = self

// Adding the native ad view to the current view controller
view.addSubview(nativeAdView)

// Add some constraints to position the view in your view hierarchy…
// ...
```

## Loading an ad

Once the native ad view is properly instantiated, an ad can be loaded using the _ad placement_ created earlier.

``` swift
nativeAdView.loadAd(with: adPlacement)
```

The native ad creative will be automatically displayed when loaded.

> To propose a smooth integration to your users and avoid displaying an empty space in case there is no ad to show,
> it is advised to hide the [`SASNativeAdView`](https://help-display-sdk.equativ.com/ios/API/Classes/SASNativeAdView.html) instance until an ad is successfully loaded.
>
> To do that, you must listen to ad loading events, as described in the next section.

## Listening to ad loading event

You can listen to ad loading events (as well as other native ad view related events) by implementing
the [`SASNativeAdViewDelegate`](https://help-display-sdk.equativ.com/ios/API/Protocols/SASNativeAdViewDelegate.html) protocol.

``` swift
func nativeAdView(_ nativeAdView: SASNativeAdView, didLoadWith adInfo: SASAdInfo, nativeAdAssets: SASNativeAdAssets) {
    print("Native ad loaded with info: \(adInfo)")
}

func nativeAdView(_ nativeAdView: SASNativeAdView, didFailToLoad error: Error) {
    print("Native ad did fail to load with error: \(error)")
}

func nativeAdViewClicked(_ nativeAdView: SASNativeAdView) {
    print("Native ad was clicked")
}

func nativeAdViewDidRequestClose(_ nativeAdView: SASNativeAdView) {
    print("Native ad did request close")
}
```

You can find all the delegate's methods in the [API documentation](https://help-display-sdk.equativ.com/ios/API/Protocols/SASNativeAdViewDelegate.html).

## Native ad rendering

### Default implementation

Starting with the **Equativ Display SDK** __v8.3__ the native ad is fully rendered internally. No need for the publisher to handle the rendering on his side from the native ad assets.

The **Equativ Display SDK** will automatically choose between several default layouts to always use the most suitable one for the received native ad.

### Advanced implementation

However, you are also able to display the native ad in your own custom layout. For this you will have to implement the optional [`nativeAdView(_:didRequestBinderFor:) `](https://help-display-sdk.equativ.com/ios/API/Protocols/SASNativeAdViewDelegate.html#/c:objc(pl)SASNativeAdViewDelegate(im)nativeAdView:didRequestBinderForNativeAdAssets:) method of the [`SASNativeAdViewDelegate`](https://help-display-sdk.equativ.com/ios/API/Protocols/SASNativeAdViewDelegate.html) protocol to provide your custom layout to the **Equativ Display SDK**.

```swift
 func nativeAdView(_ nativeAdView: SASNativeAdView, didRequestBinderFor nativeAdAssets: SASNativeAdAssets) -> SASNativeAdViewBinder? {
    // ...
 }
```

You have to create a [`SASNativeAdViewBinder`](https://help-display-sdk.equativ.com/ios/API/Classes/SASNativeAdViewBinder.html) instance to let the **Equativ Display SDK** know which layout to use and how to use it to render the native ad in it.

You can do it with the [`SASNativeAdViewBinderBuilder`](https://help-display-sdk.equativ.com/ios/API/Classes/SASNativeAdViewBinderBuilder.html) class. Then you will have set every view instances of your layout on the view binder's builder.

```swift
 func nativeAdView(_ nativeAdView: SASNativeAdView, didRequestBinderFor nativeAdAssets: SASNativeAdAssets) -> SASNativeAdViewBinder? {
    // Create your custom view here
    // ...

    // Initialize the builder using your custom view
    let builder = SASNativeAdViewBinderBuilder(baseView: customView)
    // Views binding
    builder.withTitleLabel(customView.titleLabel)
    builder.withBodyLabel(customView.bodyLabel)
    builder.withIconView(customView.iconImageView)
    // ...

    return builder.build()
 }
```

> Please note that the **Equativ Display SDK** will never update your custom layout itself. Therefore, you are
> responsible for hiding the relevant view if necessary, typically when some assets are empty.
>
> To do so, you can use the [`SASNativeAdAssets`](https://help-display-sdk.equativ.com/ios/API/Classes/SASNativeAdAssets.html) instance given as parameter of [`nativeAdView(_:didRequestBinderFor:) `](https://help-display-sdk.equativ.com/ios/API/Protocols/SASNativeAdViewDelegate.html#/c:objc(pl)SASNativeAdViewDelegate(im)nativeAdView:didRequestBinderForNativeAdAssets:).
> You can find several examples of integration in the [samples](https://github.com/smartadserver/equativ-display-sdk-samples-ios).

## Displaying native ad through SASBannerView

Starting with **Equativ Display SDK** __v8.3__ you can also display native ads within a [`SASBannerView`](https://help-display-sdk.equativ.com/ios/API/Classes/SASBannerView.html) in a very seamless way. To do so,
simply follow the [SASBannerView integration article](https://help-display-sdk.equativ.com/ios/integration/banner.html.md),
and load any native ad placement. The received native ad will be rendered in a default layout and displayed inside the [`SASBannerView`](https://help-display-sdk.equativ.com/ios/API/Classes/SASBannerView.html).
