> For the complete documentation index, see [llms.txt](https://aatkit.gitbook.io/ios-integration/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://aatkit.gitbook.io/ios-integration/formats/banner/auto-load-banner.md).

# Auto Load Banner

Integrate Auto Load Banners

Auto Load banner placement is our most recent implementation of a banner that serves as a container for banner ads. It is commonly used as a static, constantly shown part of the app’s presentation. The ad content of the banner changes over time depending on the `Refresh Rate` placement setting on [Gravite dashboard](https://dashboard.gravite.net).

### Create Placement <a href="#create-placement" id="create-placement"></a>

To create an instance of [AATAutoLoadBannerPlacement](https://docs.gravite.net/aatkit/ios/docs/documentation/aatkit/aatautoloadbannerplacement), use the following API:

{% tabs %}
{% tab title="Swift" %}

```swift
lazy var placement = AATSDK.createAutoLoadBannerPlacement(name: "<PLACEMENT_NAME>", size: .banner320x50)
```

{% endtab %}

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

```objectivec
id<AATAutoLoadBannerPlacement> placement = [AATSDK createAutoLoadBannerPlacementWithName:@"<PLACEMENT_NAME>" size:AATBannerPlacementSizeBanner300x50];
```

{% endtab %}
{% endtabs %}

You have to specify a placement name and the size argument of the type `AATBannerPlacementSize` which specifies the size of the ad to be displayed.

### Determine the Appropriate Banner Size

To ensure you choose the best banner size for a device, use the following API to determine whether the device is a tablet or not. This information will help you select the optimal banner size for the device type.

{% tabs %}
{% tab title="Swift" %}

```swift
if AATSDK.isTablet() {
    placement = AATSDK.createAutoLoadBannerPlacement(name: "<PLACEMENT_NAME>", size: .banner768x90)
} else {
    placement = AATSDK.createAutoLoadBannerPlacement(name: "<PLACEMENT_NAME>", size: .banner320x53)
}
```

{% endtab %}

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

```objectivec
if ([AATSDK isTablet]) {
    placement = [AATSDK createAutoLoadBannerPlacementWithName:@"PLACEMENT_NAME" size:AATBannerSizeBanner768x90];
} else {
    placement = [AATSDK createAutoLoadBannerPlacementWithName:@"PLACEMENT_NAME" size:AATBannerSizeBanner320x53];
}
```

{% endtab %}
{% endtabs %}

### Listen to Callbacks (optional) <a href="#listen-to-callbacks-optional" id="listen-to-callbacks-optional"></a>

Through the use of [AATAutoLoadBannerPlacementDelegate](https://docs.gravite.net/aatkit/ios/docs/documentation/aatkit/aatautoloadbannerplacementdelegate), you can listen to the different placement callbacks.

{% tabs %}
{% tab title="Swift" %}

```swift
placement.delegate = self
```

{% endtab %}

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

```objectivec
placement.delegate = self;
```

{% endtab %}
{% endtabs %}

### Get the Banner View

The autoload banner placement provides a banner ad view once and will keep responsible for refreshing the displayed ad content over time. So, once you’ve created the placement, get the ad container view via this method `func getPlacementView() -> UIView?` and display it on the screen. The placement will update the view content every time it has got a new ad.

{% tabs %}
{% tab title="Swift" %}

```swift
if let bannerAdView = placement.getPlacementView() {
    view.addSubview(bannerAdView)
    // Change the bannerAdView frame to your desired location on the screen
}
```

{% endtab %}

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

```
UIView *bannerAdView = [self.placement getPlacementView];
[self.view addSubview:bannerAdView];
// Change the bannerAdView frame to your desired location on the screen
```

{% endtab %}
{% endtabs %}

### Request Ad

You can load ads for the autoload banner placement automatically.

If you do not set the refresh time interval seconds on the dashboard, AATKit will:

* Reload every 30 seconds.
* respect the refresh time interval setting of the Dashboard (which means, you can configure the interval without having to re-publish your app).

Note: please make sure to disable (or not enable) automatic banner refresh at your ad networks of choice. Otherwise, the different auto reloads would interfere with each other.

{% tabs %}
{% tab title="Swift" %}

```swift
// reload the banner placement every 30 seconds.
placement.startAutoReload()
```

{% endtab %}

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

```objectivec
//reload the banner placement each 30 seconds.
[self.placement startAutoReload];
```

{% endtab %}
{% endtabs %}

The minimum refresh time is 30 seconds.

This needs to stop the auto-reload when it is no longer needed (e.g. if the view controller presenting ads will disappear):

{% tabs %}
{% tab title="Swift" %}

```swift
placement.stopAutoReload()
```

{% endtab %}

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

```objectivec
[self.placement stopAutoReload];
```

{% endtab %}
{% endtabs %}

**One Placement for Multiple Screens**

If the same placement is used on various different pages of the app and you want to load a new ad every time the user navigates to another page, you can achieve that by calling `stopAutoReload()` in the first controller and then call `startAutoReload()` in the next controller. This will immediately load and present a banner ad.

{% tabs %}
{% tab title="Swift" %}

```swift
placement.stopAutoReload()
// move to next screen
placement.startAutoReload()
```

{% endtab %}

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

```objectivec
[self.placement stopAutoReload];
[self.placement startAutoReload];
```

{% endtab %}
{% endtabs %}

### Complete Code Example <a href="#complete-code-example" id="complete-code-example"></a>

{% tabs %}
{% tab title="Swift" %}

```swift
class ViewController: UIViewController {
    // Create the placement
    lazy var placement = AATSDK.createAutoLoadBannerPlacement(name: "<PLACEMENT_NAME>", size: .banner320x50)
 
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        // [IMPORTANT] Notify AATKit about the currently active view controller
        AATSDK.controllerViewDidAppear(controller: self)
        
        // Set placement delegate to listen to the callbacks
        placement.delegate = self
        
        // Get the banner view
        if let bannerAdView = placement.getPlacementView() {
            view.addSubview(bannerAdView)
            // Change the bannerAdView frame to your desired location on the screen
        }
        
        // reload the banner placement every 30 seconds.
        placement.startAutoReload()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        // [IMPORTANT] Remove the currently active view controller
        AATSDK.controllerViewWillDisappear()
        // [IMPORTANT] Stop placement auto-reload
        placement?.stopAutoReload()
    }
}

extension ViewController: AATAutoLoadBannerPlacementDelegate {
    func aatHaveAd(placement: AATPlacement) {
        // The placement has loaded a new ad
    }

    func aatNoAd(placement: AATPlacement) {
        // The placement could not load a new ad
    }
    
    func aatPauseForAd(placement: AATPlacement) {
        // The placement has displayed an ad
    }

    func aatResumeAfterAd(placement: AATPlacement) {
        // Back to the app after clicking on the ad
    }
}
```

{% endtab %}

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

```objectivec
@interface ViewController () <AATAutoLoadBannerPlacementDelegate>
@property id<AATAutoLoadBannerPlacement> placement;
@end

@implementation ViewController
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    // [IMPORTANT] Notify AATKit about the currently active view controller
    [AATSDK controllerViewDidAppearWithController:self];
    
    // Create the placement
    self.placement = [AATSDK createAutoLoadBannerPlacementWithName:@"<PLACEMENT_NAME>" size:AATBannerPlacementSizeBanner300x50];
    
    // Set placement delegate to listen to the callbacks
    self.placement.delegate = self;
    
    // Get the banner view
    UIView *bannerAdView = [self.placement getPlacementView];
    [self.view addSubview:bannerAdView];
    // Change the bannerAdView frame to your desired location on the screen
    
    //reload the banner placement every 30 seconds.
    [self.placement startAutoReload];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    // [IMPORTANT] Remove the currently active view controller
    [AATSDK controllerViewWillDisappear];
    // [IMPORTANT] Stop placement autoreload
    [self.placement stopAutoReload];
}

#pragma mark - AATAutoLoadBannerPlacementDelegate
- (void)aatHaveAdWithPlacement:(id<AATPlacement>)placement {
    // The placement has loaded a new ad
}

- (void)aatNoAdWithPlacement:(id<AATPlacement>)placement {
    // The placement could not load a new ad
}

- (void)aatPauseForAdWithPlacement:(id<AATPlacement>)placement {
    // The placement has displayed an ad
}

- (void)aatResumeAfterAdWithPlacement:(id<AATPlacement>)placement {
    // Back to the app after clicking on the ad
}
@end
```

{% endtab %}
{% endtabs %}
