# 在 iOS 中实现插页式广告

> 在 iOS 应用中实现插页式广告。加载广告内容，通过 Objective-C 或 Swift 代码展示广告，并使用委托来处理广告事件。

## 插页式广告加载示例##interstitial-ad-load-example

使用 `UADSLoadConfigurationBuilder` 使用放置/位置 ID、聚合元数据和可选的广告标记来构建加载请求，然后将其通道给 `UADSInterstitialAd.load()`。完成代码块在成功时接收加载的`UADSInterstitialAd`实例。保留它，因为稍后需要显示广告，或者在失败时显示报错对象。

如果在 SDK 加载广告后未在设定时间内显示广告，则广告服务到期。在加载的广告实例上分配 `onAdExpired` 来检测此情况。尝试`show()`过期的奖励广告会失败，这一点尤其重要，因为奖励广告服务通常会预先加载并保留，直到用户请求奖励。使用回调可清除存储的引用并发出新的`load()`调用，以便在需要时准备好新的广告。

1. **Objective-C**

   ```objective-c
   @property（strong） UADSInterstitialAd *interstitialAd;

   UADSLoadConfigurationBuilder *builder =
       [[UADSLoadConfigurationBuilder alloc] initWithPlacementId:placementId];
   builder = [builder withMediationInfo:self.mediationInfo];
   builder = [builder withAdMarkup:bidResponse];

   [UADSInterstitialAd 加载：[构建器构建]
               完成：^(UADSInterstitialAd *ad, id<UnityAdsError> 报错) {
       if (error) {
           NSLog(@"Failed: %ld %@", (long)error.code, error.message);
       } else {
           __weak typeof(self) weakSelf = self;
           ad.onAdExpired = ^(UnityAd *expiredAd) {
               weakSelf.interstitialAd = nil;
               // reload here
           };
       }
   }];
   ```

2. **Swift**

   ```swift
   var interstitialAd：UADSInterstitialAd?

   let config = UADSLoadConfigurationBuilder(placementId: placementId)
     .with(mediationInfo: mediationInfo)
     .with(adMarkup: bidResponse)
     .build()

   UADSInterstitialAd.load(config) { ad, error in
     if let error = error {
         print("Failed: \(error.code) \(error.message)")
     } else {
         self.interstitialAd = ad
             ad?.onAdExpired = { [weak self] _ in
       self?.interstitialAd = nil
       // reload here
       }
     }
   }
   ```

## 插页式广告展示示例##interstitial-ad-show-example

对保留的`UADSInterstitialAd`实例调用 `show()`，传递使用呈现视图控制器构建的`UADSShowConfiguration`和 `UADSInterstitialShowDelegate`。委托为广告生命周期的每个阶段提供回调：广告显示时的`showDidStart`、用户与其交互时的`showDidClick`、关闭时的`showDidComplete`以及发生报错时的`showDidFail`。

1. **Objective-C**

   ```objective-c
   @interface MyDelegate：NSObject <UADSInterstitialShowDelegate>
   @end

   @implementation MyDelegate
   - (void)showDidStart:(UADSInterstitialAd *)unityAd { }
   }
   - (void)showDidClick:(UADSInterstitialAd *)unityAd { }
   }
   - (void)showDidComplete：(UADSInterstitialAd *)unityAd
                     with:(UADSShowFinishState)finishState {
   }
   - (void)showDidFail：(UADSInterstitialAd *)unityAd
               报错：(id<UnityAdsError>)报错 {
   }
   @end

   UADSShowConfigurationBuilder *builder =
   [[UADSShowConfigurationBuilder alloc] init];
   builder = [builder withViewController:viewController];

   [self.interstitialAd show:[builder build] delegate:delegate];
   ```

2. **Swift**

   ```swift
   类 MyDelegate：NSObject, UADSInterstitialShowDelegate {
   func showDidStart(_ unityAd:UADSInterstitialAd) {
   }

   func showDidClick(_ unityAd:UADSInterstitialAd) {
   }

   func showDidComplete(
       _ unityAd:UADSInterstitialAd,
       with finishState：UADSShowFinishState
   ) {
   }

   func showDidFail(
       _ unityAd:UADSInterstitialAd,
       报错：UnityAdsError
   ) {
   }
   }

   let config = UADSShowConfigurationBuilder()
   .with(viewController: viewController)
   .build()

   interstitialAd?.show(config, delegate: delegate)
   ```

**后续步骤**：请参阅关于[在 iOS 中实现奖励广告](/ads-ios/4.19.0/sdk-integration/rewarded-ads.md)的文档以改进您的实现。
