0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Flutter】AdMobの実装(iOSのみ、バナー広告)

Posted at

FlutterでのAdMob広告表示方法。
(AdMob自体の登録については触れない)

これだけあれば最低限の実行はできる。

pubspec.yaml の修正

を使用する。

dependencies:
  google_mobile_ads: ^3.0.0

(バージョンはその時点の最新等に合わせてよしなに設定する)

ios/Runner/Info.plist の修正

iOS用のアプリIDを登録する。
(下記はテスト用のIDなので実際は払い出されたIDを登録する)

    <string>ca-app-pub-3940256099942544~1458002511</string>
    <key>SKAdNetworkItems</key>

:warning: 「pubspec.yaml」にパッケージを追加して、ここに追加しないまま実行するとクラッシュするので気をつける

初期化

main()で、初期化する

import 'package:google_mobile_ads/google_mobile_ads.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();

  // 広告を初期化
  MobileAds.instance.initialize();

  runApp(const MyApp());
}

広告をロードする

バナー広告のテスト用IDなので実際は払い出されたIDを登録する

void _loadAd() {
  _bannerAd = BannerAd(
    adUnitId: 'ca-app-pub-3940256099942544/2934735716',
    size: AdSize.banner,
    request: const AdRequest(),
    listener: const BannerAdListener(),
  );

  _bannerAd!.load();
}

広告を表示する

:warning: サイズを指定しないと落ちたので指定する

SizedBox(
  width: _bannerAd!.size.width.toDouble(),
  height: _bannerAd!.size.height.toDouble(),
  child: AdWidget(ad: _bannerAd!),
),

最後に開放

@override
void dispose() {
  _bannerAd?.dispose();

  super.dispose();
}
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?