> For the complete documentation index, see [llms.txt](https://docs.blueseasx.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.blueseasx.com/ios/banner-ads.md).

# Banner Ads

### **Step1. Load**

Before requesting ads, please ensure that the SDK has been initialized.

To request banner ads, declare a `BSXMediationInfo` object. This object should include the ad mediation platform, the mediation version, and the BlueSea Adapter version used when requesting the ads. If you are integrating BlueSea SDK through your own mediation platform, it is recommended to declare the `BSXMediationInfo` object and pass in the mediation information to facilitate monetization data management and channel monetization optimization. If you are integrating BlueSea SDK through third-party mediations like MAX, LevelPlay, etc., you can skip this step.

```
BSXMediationInfo *mediationInfo = [[BSXMediationInfo alloc] initWithMediationName:@"<in-house or mediation>" mediationVersion:@"<mediation version-name>" adapterVersion:@"<adapter-version-name>"];
```

* `<in house or mediation name>`: Pass the value as "in-house" or the specific mediation platform name.
* `<mediation-version-name>`: Pass the version number of the mediation SDK.
* `<adapter-version-code>`: Pass the version number of the adapter.

To request an ad, create a `BSXAdView` with the specified Placement and call the loadAd method. Add the `BSXAdView` as a child of your parent layout when you’re going to display the ad. You should also set a `BSXAdViewDelegate` to track ad load status and other related events. The following example shows how to request an ad:

```
@interface BannerViewController () <BSXAdViewDelegate>
@property(nonatomic, strong) BSXAdView *adView;
@property(nonatomic, strong) UIView *adContainer;
@end

@implementation BannerViewController
- (BSXAdView *)createAdView {
    BSXAdView *adView = [[BSXAdView alloc] initWithFrame:self.adContainer.bounds];
    [adView setPlacementId:BSXBannerAdPlacementId];
    [adView setMediationInfo:self.mediationInfo];
    return adView;
}

- (void)loadBannerAd {
    self.adView = [self createAdView];
    self.adView.delegate = self;
    [self.adView loadAd];
}

#pragma mark - BSXAdViewDelegate
- (void)didBSXBannerAd:(BSXAdView *)adView loadError:(BSXAdError*)error {
    NSLog(@"BannerAd load fail.");
}
- (void)didBSXBannerAdLoaded:(BSXAdView *)adView {
    NSLog(@"BannerAd loaded.");
}
- (void)didBSXBannerAd:(BSXAdView *)adView displayFailed:(BSXAdError*)error {
    NSLog(@"BannerAd display fail.");
}
- (void)didBSXBannerAdDisplayed:(BSXAdView *)adView {
    NSLog(@"BannerAd displayed.");
}
- (void)didBSXBannerAdClicked:(BSXAdView *)adView {
    NSLog(@"BannerAd clicked.");
}

@end
```

`BSXAdViewDelegate` callback:

* `- (void)didBSXBannerAd:(BSXAdView *)adView loadError:(BSXAdError*)error`: Indicates that no ads were filled. This may be due to the advertiser not bidding or other issues. You can check the error code and message using `BSXAdError.errorCode` and `BSXAdError.errorMsg`. For details on error codes, refer to [Test & Error Handling](/ios/test-and-error-handling.md)
* `- (void)didBSXBannerAdLoaded:(BSXAdView *)adView`: Indicates that the ad was successfully filled

### **Step2. Show**

Once you receive the `onAdLoaded` callback, use the addSubView method to add the `adView` to your window, and set a `BSXAdViewDelegate` to listen for ad events.

```
@interface BannerViewController () <BSXAdViewDelegate>
@property(nonatomic, strong) BSXAdView *adView;
@property(nonatomic, strong) UIView *adContainer;
@end

@implementation BannerViewController
- (BSXAdView *)createAdView {
    BSXAdView *adView = [[BSXAdView alloc] initWithFrame:self.adContainer.bounds];
    [adView setPlacementId:BSXBannerAdPlacementId];
    [adView setMediationInfo:self.mediationInfo];
    return adView;
}
- (void)showBannerView:(id)target {
    if (self.adView) {
        [self.adView removeFromSuperview];
        BSXAdSize *adSize = [self.adView getAdSize];
        self.adView.frame = CGRectMake(0, 0, adSize.width, adSize.height);
        self.adView.center = CGPointMake(CGRectGetWidth(self.adContainer.frame) / 2, CGRectGetHeight(self.adContainer.frame) / 2);
        [self.adContainer addSubview:self.adView];
    } 
}

#pragma mark - BSXAdViewDelegate
- (void)didBSXBannerAd:(BSXAdView *)adView loadError:(BSXAdError*)error {
    NSLog(@"BannerAd load fail.");
}
- (void)didBSXBannerAdLoaded:(BSXAdView *)adView {
    NSLog(@"BannerAd loaded.");
}
- (void)didBSXBannerAd:(BSXAdView *)adView displayFailed:(BSXAdError*)error {
    NSLog(@"BannerAd display fail.");
}
- (void)didBSXBannerAdDisplayed:(BSXAdView *)adView {
    NSLog(@"BannerAd displayed.");
}
- (void)didBSXBannerAdClicked:(BSXAdView *)adView {
    NSLog(@"BannerAd clicked.");
}

@end
```

Before displaying the ad, call `[BSXAdView isExpired]` to check whether the ad has expired (the typical validity period is 1 hour). If the ad has expired, request a new one.

### **Step3. Destroy**

If you no longer need the ad, you can destroy it using the following method:

```
@implementation BannerViewController
- (void)destroyAdView {
    if (self.adView) {
        [self.adView removeFromSuperview];
        [self.adView destroy];
    }
}
@end
```
