0
1

More than 3 years have passed since last update.

AdMobをiOSに組み込んでみよう(全画面広告編)

Last updated at Posted at 2020-06-15

AdMobをiosに組み込んでみようという事で覚書

「AdMobをiOSに組み込んでみよう(環境設定編)」の続きです。
https://qiita.com/sanoh/items/2c99918fe7c9ba01821c

■Setp1.画面のレイアウトを設定しよう

・ストーリーボードを立ち上ボタンを作成しよう(Layoutの仕方は省きます)
image0203.png

■Step2.ボタンをソースに追加

Buttonを右クリックで「ViewController.m」の@endの上にドロップします。
image0204.png
名前を聞いてくるので今回は「doSomethig」として「Connect」を選択します。
image0205.png
すると以下のような状態になります
image0206.png

■Step3.プログラム

ViewController.mを編集します

#import "ViewController.h"
@import GoogleMobileAds;

@interface ViewController () <GADInterstitialDelegate>
@property(nonatomic, strong) GADInterstitial *interstitial;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.interstitial = [self createAndLoadInterstitial];
}
//  広告の読み込み設定
- (GADInterstitial *)createAndLoadInterstitial {
  GADInterstitial *interstitial =
      [[GADInterstitial alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910"];
  interstitial.delegate = self;
  [interstitial loadRequest:[GADRequest request]];
  return interstitial;
}
/// 広告を閉じるときに呼ばれる
- (void)interstitialDidDismissScreen:(GADInterstitial *)ad {
  self.interstitial = [self createAndLoadInterstitial];
  NSLog(@"interstitialDidDismissScreen");
}
//  ボタンが押されたよ
- (IBAction)doSomething:(id)sender {
    if (self.interstitial.isReady) {
      [self.interstitial presentFromRootViewController:self];
    }
}

@end

image0207.png

■Step4.実行

実行すると
image0208.png
ボタンを押すと広告が現れます
image0209.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1