베타 전체 화면 광고 API로 이전

Google 모바일 광고 SDK v7.67.0에는 일관성을 개선하기 위해 일반 전체 화면 광고 스타일을 공유하는 전면 광고 및 보상형 광고 베타 API가 도입됩니다. 이러한 API는 GADInterstitialAdBetaGADRewardedAdBeta로 표시되며 2021년 초에 GADInterstitialGADRewardedAd를 대체할 예정입니다. 이 가이드에는 전면 광고 및 보상형 광고의 베타 버전으로 이전하는 방법이 나와 있습니다.

새로운 베타 API의 이점

새로운 전체 화면 광고 API는 다음과 같이 크게 두 가지 면에서 현재 전체 화면 광고 API와 차이가 있습니다.

  1. 정적 클래스 메서드 load

    현재 전체 화면 광고를 로드/게재하는 방식은 다음과 같습니다.

    1. 광고 객체 인스턴스를 만들고 이에 대한 참조를 유지합니다.
    2. 로드를 처리하는 대리자를 할당하고 콜백을 표시합니다.
    3. 광고를 로드합니다.
    4. isReady를 사용하여 광고가 로드되었는지 확인합니다.
    5. 광고를 게재합니다.

    새로운 API에서는 이러한 방식이 약간 변경됩니다. 로드 콜백이 더 이상 대리자의 일부가 아닙니다. 대신 load 메서드에 완료 핸들러로 전달됩니다.

    전체 화면 광고를 로드/게재하는 새로운 방식은 다음과 같습니다.

    1. 광고 클래스의 정적 로드 메서드를 호출하고 로드 완료 핸들러를 제공합니다.
    2. 로드 완료 콜백에서는 반환되어 로드된 광고에 대한 참조를 유지합니다.
    3. 콜백 표시를 처리하는 대리자를 할당합니다.
    4. 광고를 게재합니다.

    이러한 새로운 방식은 다음과 같은 이점이 있습니다.

    • 로드되지 않은 광고에 대한 참조가 없습니다.
    • 광고가 로드되는 동안 광고 객체를 보유하고 있지 않아도 됩니다.
  2. 일관된 광고 이벤트

    이벤트 유형 기존 API 베타 API
    로드 이벤트 GADInterstitialDelegate 또는 GADRewardedAdDelegate GADInterstitialAdBetaLoadCompletionHandler 또는 GADRewardedAdBetaLoadCompletionHandler
    프레젠테이션 이벤트 GADFullScreenContentDelegate

    현재는 광고 이벤트를 수신하려면 사용하는 형식에 따라 전면광고의 대리자 속성에 GADInterstitialDelegate 프로토콜을 구현하는 클래스를 등록하거나 보상형 광고의 대리자 속성에 GADRewardedAdDelegate 프로토콜을 구현하는 케이스를 등록해야 했습니다. 동일한 대리자에는 광고 로드 및 광고 프레젠테이션 수명 주기와 관련된 메서드가 있었습니다.

    새 API를 사용하면 로드와 프레젠테이션 이벤트가 구분됩니다. 이제는 광고를 로드하기 전에 단일 대리자를 설정할 필요 없이, 광고를 게재하기 전에 언제든지 GADFullScreenContentDelegate를 등록할 수 있습니다. 각 형식과 관련한 광고 로드 이벤트가 로드 메서드에서 전달된 단일 로드 완료 핸들러로 이동됩니다.

전면 광고

광고 로드

아래의 코드 스니펫에서는 전면 광고를 로드하는 방법과 광고 로드에 성공 또는 실패하는 경우 이벤트를 수신하는 방법을 보여줍니다.

현재 API

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADInterstitialDelegate {

  var interstitial: GADInterstitial!

  override func viewDidLoad() {
    super.viewDidLoad()
    interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
    interstitial.delegate = self
    let request = GADRequest()
    interstitial.load(request)
  }

  /// Tells the delegate an ad request succeeded.
  func interstitialDidReceiveAd(_ ad: GADInterstitial) {
    print("Interstitial ad loaded.")
  }

  /// Tells the delegate an ad request failed.
  func interstitial(_ ad: GADInterstitial, didFailToReceiveAdWithError error: GADRequestError) {
    print("Interstitial ad failed to load with error: \(error.localizedDescription)")
  }
}

Objective-C

@import GoogleMobileAds;
@import UIKit;

@interface ViewController () 

@property(nonatomic, strong) GADInterstitial *interstitial;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  self.interstitial = [[GADInterstitial alloc]
      initWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910"];
  self.interstitial.delegate = self;
  GADRequest *request = [GADRequest request];
  [self.interstitial loadRequest:request];
}

/// Tells the delegate an ad request succeeded.
- (void)interstitialDidReceiveAd:(GADInterstitial *)ad {
  NSLog(@"Insterstitial ad loaded.");
}

/// Tells the delegate an ad request failed.
- (void)interstitial:(GADInterstitial *)ad
    didFailToReceiveAdWithError:(GADRequestError *)error {
  NSLog(@"Interstitial ad failed to load with error: %@", [error localizedDescription]);
}

베타 API

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADFullScreenContentDelegate {

  var interstitial: GADInterstitialAdBeta?

  override func viewDidLoad() {
    super.viewDidLoad()
    let request = GADRequest()
    GADInterstitialAdBeta.load(withAdUnitID:"ca-app-pub-8123415297019784/4985798738",
                               request: request,
                               completionHandler: { (ad, error) in
                                if let error = error {
                                  print("Failed to load interstitial ad with error: \(error.localizedDescription)")
                                  return
                                }
                                self.interstitial = ad
                                self.interstitial.fullScreenContentDelegate = self
    })
  }
}

Objective-C

@import GoogleMobileAds;
@import UIKit;

@interface ViewController () 

@property(nonatomic, strong) GADInterstitialAdBeta *interstitial;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  GADRequest *request = [GADRequest request];
  [GADInterstitialAdBeta loadWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910"
                                  request:request
                        completionHandler:^(GADInterstitialAdBeta *ad, NSError *error) {
    if (error) {
      NSLog(@"Failed to load interstitial ad with error: %@", [error localizedDescription]);
      return;
    }
    self.interstitial = ad;
    self.interstitial.fullScreenContentDelegate = self;
  }];
}

디스플레이 광고

현재 API

Swift

func showInterstitial() {
  ...
  if interstitial.isReady {
    interstitial.present(fromRootViewController: self)
  } else {
    print("Ad wasn't ready")
  }
}

Objective-C

- (void)showInterstitial: {
  ...
  if (self.interstitial.isReady) {
    [self.interstitial presentFromRootViewController:self];
  } else {
    NSLog(@"Ad wasn't ready");
  }
}

베타 API

Swift

func showInterstitial() {
  ...
  if let ad = interstitial {
    ad.present(fromRootViewController: self)
  } else {
    print("Ad wasn't ready")
  }
}

Objective-C

- (void)showInterstitial: {
  ...
  if (self.interstitial) {
    [self.interstitial presentFromRootViewController:self];
  } else {
    NSLog(@"Ad wasn't ready");
  }
}

프레젠테이션 광고 이벤트

아래의 코드 스니펫에서는 광고 표시가 성공 또는 실패했을 때와 광고가 닫혔을 때 콜백을 처리하는 방법을 보여줍니다.

현재 API

Swift

override func viewDidLoad() {
  super.viewDidLoad()
  interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
  interstitial.delegate = self
  ...
}

/// Tells the delegate that an interstitial will be presented.
func interstitialWillPresentScreen(_ ad: GADInterstitial) {
  print("Interstitial ad will be presented.")
}

/// Tells the delegate the interstitial is to be animated off the screen.
func interstitialWillDismissScreen(_ ad: GADInterstitial) {
  print("Interstitial ad will be dismissed.")
}

/// Tells the delegate the interstitial had been animated off the screen.
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
  print("Interstitial ad dismissed.")
}

/// Tells the delegate that a user click will open another app
/// (such as the App Store), backgrounding the current app.
func interstitialWillLeaveApplication(_ ad: GADInterstitial) {
  print("Interstitial ad will leave application.")
}

Objective-C

- (void)viewDidLoad {
  [super viewDidLoad];
  self.interstitial = [[GADInterstitial alloc] initWithAdUnitID:"ca-app-pub-3940256099942544/4411468910"];
  self.interstitial.delegate = self;
  ...
}

/// Tells the delegate that an interstitial will be presented.
- (void)interstitialWillPresentScreen:(GADInterstitial *)ad {
  NSLog(@"Interstitial ad will be presented.");
}

/// Tells the delegate the interstitial is to be animated off the screen.
- (void)interstitialWillDismissScreen:(GADInterstitial *)ad {
  NSLog(@"Interstitial ad will be dismissed.");
}

/// Tells the delegate the interstitial had been animated off the screen.
- (void)interstitialDidDismissScreen:(GADInterstitial *)ad {
  NSLog(@"Interstitial ad dismissed.");
}

/// Tells the delegate that a user click will open another app
/// (such as the App Store), backgrounding the current app.
- (void)interstitialWillLeaveApplication:(GADInterstitial *)ad {
  NSLog(@"Interstitial ad will leave application.");
}

베타 API

Swift

override func viewDidLoad() {
  super.viewDidLoad()
  let request = GADRequest()
  GADInterstitialAdBeta.load(withAdUnitID:"ca-app-pub-8123415297019784/4985798738",
                             request: request,
                             completionHandler: { (ad, error) in
                              if let error = error {
                                print(error.localizedDescription)
                                return
                              }
                              self.interstitial = ad
                              self.interstitial.fullScreenContentDelegate = self
  })
}

func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
  print("Ad did present full screen content.")
}

func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
  print("Ad failed to present full screen content with error \(error.localizedDescription).")
}

func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
  print("Ad did dismiss full screen content.")
}

Objective-C

- (void)viewDidLoad {
  [super viewDidLoad];
  GADRequest *request = [GADRequest request];
  [GADInterstitialAdBeta loadWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910"
                                  request:request
                        completionHandler:^(GADInterstitialAdBeta *ad, NSError *error) {
    if (error) {
      NSLog(@"interstitial:didFailToReceiveAdWithError: %@", [error localizedDescription])
      return;
    }
    self.interstitial = ad;
    self.interstitial.fullScreenContentDelegate = self;
  }];
}

- (void)adDidPresentFullScreenContent:(id)ad {
  NSLog(@"Ad did present full screen content.");
}

- (void)ad:(id)ad didFailToPresentFullScreenContentWithError:(NSError *)error {
  NSLog(@"Ad failed to present full screen content with error %@.", [error localizedDescription]);
}

- (void)adDidDismissFullScreenContent:(id)ad {
  NSLog(@"Ad did dismiss full screen content.");
}

보상형 광고

광고 로드

현재 API

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADRewardedAdDelegate {
  /// The rewarded ad.
  var rewardedAd: GADRewardedAd?

  override func viewDidLoad() {
    super.viewDidLoad()
    rewardedAd = GADRewardedAd(adUnitID: "ca-app-pub-3940256099942544/1712485313")
    rewardedAd.delegate = self
    rewardedAd?.load(GADRequest()) { error in
      if let error = error {
        print("Rewarded ad failed to load with error: \(error.localizedDescription)")
      } else {
        print("Rewarded ad loaded.")
      }
    }
  }
}

Objective-C

@import GoogleMobileAds;
@import UIKit;

@interface ViewController () 

@property(nonatomic, strong) GADRewardedAd *rewardedAd;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  self.rewardedAd = [[GADRewardedAd alloc]
      initWithAdUnitID:@"ca-app-pub-3940256099942544/1712485313"];
  self.rewardedAd.delegate = self;
  GADRequest *request = [GADRequest request];
  [self.rewardedAd loadRequest:request completionHandler:^(GADRequestError * _Nullable error) {
    if (error) {
      NSLog(@"Rewarded ad failed to load with error: %@", [error localizedDescription]);
    } else {
      NSLog(@"Rewarded ad loaded.");
    }
  }];
}

베타 API

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADFullScreenContentDelegate {
  /// The rewarded ad.
  var rewardedAd: GADRewardedAdBeta?

  override func viewDidLoad() {
  super.viewDidLoad()
  let request = GADRequest()
  GADRewardedAdBeta.load(withAdUnitID: "ca-app-pub-8123415297019784/9501821136",
                         request: request, completionHandler: { (ad, error) in
                          if let error = error {
                            print("Rewarded ad failed to load with error: \(error.localizedDescription)")

                            return
                          }
                          self.rewardedAd = ad
                          self.rewardedAd?.fullScreenContentDelegate = self
    })
  }
}

Objective-C

@import GoogleMobileAds;
@import UIKit;

@interface ViewController () 

@property(nonatomic, strong) GADRewardedAdBeta *rewardedAd;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  GADRequest *request = [GADRequest request];
  [GADRewardedAdBeta loadWithAdUnitID:@"ca-app-pub-3940256099942544/1712485313"
                              request:request
                    completionHandler:^(GADRewardedAdBeta *ad, NSError *error) {
    if (error) {
      NSLog(@"Rewarded ad failed to load with error: %@", [error localizedDescription]);
      return;
    }
    self.rewardedAd = ad;
    NSLog(@"Rewarded ad loaded.");
    self.rewardedAd.fullScreenContentDelegate = self;
}

디스플레이 광고 및 보상 처리

보상형 광고를 이용하면 사용자가 보상을 받을 때 이벤트가 처리되어야 합니다. GADRewardedAd API를 사용하면 GADRewardedAdDelegate 프로토콜의 일부로 rewardedAd:userDidEarnReward:를 구현하고, GADRewardedAdBeta API를 사용하면 광고를 표시하기 위해 GADUserDidEarnRewardHandler를 구현합니다.

현재 API

Swift

func showRewardedAd() {
  ...
  if rewardedAd.isReady {
    rewardedAd.present(fromRootViewController: self delegate:self)
  } else {
    print("Ad wasn't ready")
  }
}

/// Tells the delegate that the user earned a reward.
func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarnReward: GADAdReward) {
  // TODO: Reward the user.
}

Objective-C

- (void)showRewardedAd: {
  ...
  if (self.rewardedAd.isReady) {
    [self.rewardedAd presentFromRootViewController:self delegate:self];
  } else {
    NSLog(@"Ad wasn't ready");
  }
}

/// Tells the delegate that the user earned a reward.
- (void)rewardedAd:(GADRewardedAd *)rewardedAd userDidEarnReward:(GADAdReward *)reward {
  // TODO: Reward the user.
}

베타 API

Swift

func showRewardedAd() {
  ...
  if let ad = rewardedAd {
      ad.present(fromRootViewController: self,
                 userDidEarnRewardHandler: {
                  let reward = ad.adReward
                  // TODO: Reward the user.
      })
  } else {
    print("Ad wasn't ready")
  }
}

Objective-C

- (void)showRewardedAd: {
  ...
  if (self.rewardedAd) {
    [self.rewardedAd presentFromRootViewController:self
                          userDidEarnRewardHandler:^ {
      GADAdReward *reward = self.rewardedAd.adReward;
      // TODO: Reward the user.
    }];
  } else {
    NSLog(@"Ad wasn't ready");
  }
}

프레젠테이션 광고 이벤트

GADRewardedAd API를 사용하여 GADRewardedAdDelegate를 광고를 표시하는 메서드에 전달합니다. GADRewardedAdBeta API를 사용하여 광고를 표시하기 전에 GADFullscreenContentDelegate를 광고의 속성으로 설정합니다.

현재 API

Swift

func showRewardedAd() {
  ...
  if rewardedAd.isReady {
    rewardedAd.present(fromRootViewController: self delegate:self)
  } else {
    print("Ad wasn't ready")
  }
}

/// Tells the delegate that the rewarded ad was presented.
func rewardedAdDidPresent(_ rewardedAd: GADRewardedAd) {
  print("Rewarded ad presented.")
}
/// Tells the delegate that the rewarded ad was dismissed.
func rewardedAdDidDismiss(_ rewardedAd: GADRewardedAd) {
  print("Rewarded ad dismissed.")
}
/// Tells the delegate that the rewarded ad failed to present.
func rewardedAd(_ rewardedAd: GADRewardedAd, didFailToPresentWithError error: Error) {
  print("Rewarded ad failed to present with error: \(error.localizedDescription).")
}

Objective-C

- (void)showRewardedAd: {
  ...
  if (self.rewardedAd.isReady) {
    [self.rewardedAd presentFromRootViewController:self delegate:self];
  } else {
    NSLog(@"Ad wasn't ready");
  }
}

/// Tells the delegate that the rewarded ad was presented.
- (void)rewardedAdDidPresent:(GADRewardedAd *)rewardedAd {
  NSLog(@"Rewarded ad presented.");
}

/// Tells the delegate that the rewarded ad failed to present.
- (void)rewardedAd:(GADRewardedAd *)rewardedAd didFailToPresentWithError:(NSError *)error {
  NSLog(@"Rewarded ad failed to present with error: %@",
        [error localizedDescription]);
}

/// Tells the delegate that the rewarded ad was dismissed.
- (void)rewardedAdDidDismiss:(GADRewardedAd *)rewardedAd {
  NSLog(@"Rewarded ad dismissed.");
}

베타 API

Swift

override func viewDidLoad() {
  super.viewDidLoad()
  let request = GADRequest()
  GADRewardedAdBeta.load(withAdUnitID: "ca-app-pub-8123415297019784/9501821136",
                         request: request, completionHandler: { (ad, error) in
                          if let error = error {
                            print(error.localizedDescription)
                            return
                          }
                          self.rewardedAd = ad
                          self.rewardedAd?.fullScreenContentDelegate = self
    })
  }

  /// Tells the delegate that the rewarded ad was presented.
  func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    print("Rewarded ad presented.")
  }
  /// Tells the delegate that the rewarded ad was dismissed.
  func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    print("Rewarded ad dismissed.")
  }
  /// Tells the delegate that the rewarded ad failed to present.
  func ad(_ ad: GADFullScreenPresentingAd,
      didFailToPresentFullScreenContentWithError error: Error) {
    print("Rewarded ad failed to present with error: \(error.localizedDescription).")
  }

Objective-C

- (void)viewDidLoad {
  [super viewDidLoad];

  GADRequest *request = [GADRequest request];
  [GADRewardedAdBeta loadWithAdUnitID:@"ca-app-pub-3940256099942544/1712485313"
                              request:request
                    completionHandler:^(GADRewardedAdBeta *ad, NSError *error) {
    if (error) {
      NSLog(@"Rewarded ad failed to load with error: %@", [error localizedDescription]);
      return;
    }
    self.rewardedAd = ad;
    NSLog(@"Rewarded ad loaded.");
    self.rewardedAd.fullScreenContentDelegate = self;
}

/// Tells the delegate that the rewarded ad was presented.
- (void)adDidPresentFullScreenContent:(id)ad {
  NSLog(@"Rewarded ad presented.");
}

/// Tells the delegate that the rewarded ad failed to present.
- (void)ad:(id)ad
    didFailToPresentFullScreenContentWithError:(NSError *)error {
  NSLog(@"Rewarded ad failed to present with error: %@",
       [error localizedDescription]);
}

/// Tells the delegate that the rewarded ad was dismissed.
- (void)adDidDismissFullScreenContent:(id)ad {
  NSLog(@"Rewarded ad dismissed.");
}