AdMobをiosに組み込んでみようという事で覚書
「AdMobをiOSに組み込んでみよう(環境設定編)」の続きです。
https://qiita.com/sanoh/items/2c99918fe7c9ba01821c
■Setp1.画面のレイアウトを設定しよう
・ストーリーボードを立ち上ボタンを作成しよう(Layoutの仕方は省きます)
■Step2.ボタンをソースに追加
Buttonを右クリックで「ViewController.m」の@endの上にドロップします。
名前を聞いてくるので今回は「doSomethig」として「Connect」を選択します。
すると以下のような状態になります
■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