加载单个插页式广告

插页式广告是全屏广告,会在应用中的过渡点(例如,屏幕之间或游戏关卡之间的暂停时段)展示。用户可以点按广告前往其目标页面,也可以关闭广告回到应用界面。如需了解详情,请参阅插页式广告指南和案例研究。

本指南介绍了如何将插页式广告植入到 React Native 应用中。

前提条件

Android

ca-app-pub-3940256099942544/1033173712

iOS

ca-app-pub-3940256099942544/4411468910

加载广告

如需加载插页式广告,请对 InterstitialAd 类调用 load 方法。

以下示例会在成功时加载插页式广告,在失败时记录错误:

export async function loadInterstitialAd(
  adUnitId: string,
): Promise<InterstitialAd | null> {
  try {
    const ad = await InterstitialAd.load({adUnitId});
    return ad;
  } catch (error) {
    const loadError = error as LoadAdError;
    console.error('Failed to load interstitial ad:', loadError.message);
    return null;
  }
}

监听广告事件

在展示广告之前,请在已加载的 InterstitialAd 实例上设置 adEventCallback 监听器。此监听器会接收广告生命周期事件(例如广告在屏幕上展示或用户点按广告)的回调。如需了解可用的回调,请参阅 AdEventCallback。

以下示例设置了用于监听广告事件的回调:

export function registerAdEvents(ad: InterstitialAd) {
  ad.adEventCallback = {
    // Called when the ad is presented on screen.
    onAdShowedFullScreenContent: () => {
      console.log('Interstitial ad showed full screen content.');
    },
    // Called when the ad is closed by the user.
    onAdDismissedFullScreenContent: () => {
      console.log('Interstitial ad dismissed full screen content.');
      ad.destroy();
    },
    // Called when presentation fails.
    onAdFailedToShowFullScreenContent: (error) => {
      console.error('Failed to show interstitial ad:', error);
      ad.destroy();
    },
    // Called when an impression is recorded.
    onAdImpression: () => {
      console.log('Interstitial ad recorded an impression.');
    },
    // Called when the user taps the ad.
    onAdClicked: () => {
      console.log('Interstitial ad was clicked.');
    },
    // Called when the ad is estimated to have earned money.
    onAdPaid: (adValue) => {
      console.log(
        `Interstitial ad paid: ${adValue.amountMicros}` +
          ` ${adValue.currencyCode}`,
      );
    },
  };
}

此示例还会在用户关闭广告或广告未能展示时调用 destroy 方法。此过程会释放广告使用的资源。

展示广告

如需展示插页式广告,请在已加载的 InterstitialAd 实例上调用 show 方法。

以下示例展示了一个插页式广告:

export function showInterstitialAd(ad: InterstitialAd) {
  ad.show();
}

查看完整示例

本部分通过一个完整示例介绍了如何将插页式广告植入到 React Native 应用中。

以下示例将广告集成到 React Native 组件中,在状态中管理广告实例,并提供用于加载和展示广告的控件:

export function InterstitialSingleLoadScreen() {
  const [ad, setAd] = useState<InterstitialAd | null>(null);
  const [isLoaded, setIsLoaded] = useState(false);
  const [status, setStatus] = useState<string>('Idle');

  useEffect(() => {
    return () => {
      ad?.destroy();
    };
  }, [ad]);

  const handleLoad = async () => {
    setStatus('Loading Interstitial ad...');
    const loadedAd = await loadInterstitialAd(INTERSTITIAL_AD_UNIT_ID);
    if (loadedAd) {
      registerAdEvents(loadedAd);
      const onDismiss =
        loadedAd.adEventCallback?.onAdDismissedFullScreenContent;
      loadedAd.adEventCallback = {
        ...loadedAd.adEventCallback,
        onAdDismissedFullScreenContent: () => {
          onDismiss?.();
          setStatus('Ad dismissed.');
        },
      };
      setAd((prevAd) => {
        prevAd?.destroy();
        return loadedAd;
      });
      setIsLoaded(true);
      setStatus('Interstitial ad loaded!');
    } else {
      setStatus('Failed to load Interstitial ad.');
    }
  };

  const handleShow = () => {
    if (ad) {
      showInterstitialAd(ad);
      setIsLoaded(false);
    }
  };

  return (
    <View style={styles.container}>
      <Text style={styles.title}>Interstitial Ad</Text>
      <Text style={styles.status}>Status: {status}</Text>
      <View style={styles.buttonGroup}>
        <Button title="Load Ad" onPress={handleLoad} />
        <View style={styles.spacerSmall} />
        <Button title="Show Ad" onPress={handleShow} disabled={!isLoaded} />
      </View>
    </View>
  );
}

后续步骤

集成插页式广告后,请探索以下主题: