Skip to content

Commit 0267182

Browse files
committed
ステップ2「アプリにネイティブ機能をつけよう」
コミュニティプラグイン「Google AdMobで広告の表示をしよう」
1 parent 9ab61a3 commit 0267182

12 files changed

+54
-4
lines changed

android/app/capacitor.build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ android {
1010
apply from: "../capacitor-cordova-android-plugins/cordova.variables.gradle"
1111
dependencies {
1212

13+
implementation project(':rdlabo-capacitor-admob')
1314

1415
}
1516

android/app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
android:supportsRtl="true"
1212
android:theme="@style/AppTheme">
1313

14+
<meta-data
15+
android:name="com.google.android.gms.ads.APPLICATION_ID"
16+
android:value="ca-app-pub-1053575285730954~3749355430"/>
17+
1418
<activity
1519
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
1620
android:name="io.ionic.starter.MainActivity"

android/app/src/main/java/io/ionic/starter/MainActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public void onCreate(Bundle savedInstanceState) {
1616
this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
1717
// Additional plugins you've installed go here
1818
// Ex: add(TotallyAwesomePlugin.class);
19+
add(jp.rdlabo.capacitor.plugin.admob.AdMob.class);
1920
}});
2021
}
2122
}

android/capacitor.settings.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
// DO NOT EDIT THIS FILE! IT IS GENERATED EACH TIME "capacitor update" IS RUN
22
include ':capacitor-android'
33
project(':capacitor-android').projectDir = new File('../node_modules/@capacitor/android/capacitor')
4+
5+
include ':rdlabo-capacitor-admob'
6+
project(':rdlabo-capacitor-admob').projectDir = new File('../node_modules/@rdlabo/capacitor-admob/android/@rdlabo/capacitor-admob')

ios/App/App/AppDelegate.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import UIKit
22
import Capacitor
3+
import GoogleMobileAds
34

45
@UIApplicationMain
56
class AppDelegate: UIResponder, UIApplicationDelegate {
@@ -9,6 +10,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
910

1011
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
1112
// Override point for customization after application launch.
13+
GADMobileAds.sharedInstance().start(completionHandler: nil)
1214
return true
1315
}
1416

ios/App/App/Info.plist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5+
<key>GADApplicationIdentifier</key>
6+
<string>ca-app-pub-1053575285730954~8646458891</string>
57
<key>CFBundleDevelopmentRegion</key>
68
<string>en</string>
79
<key>CFBundleDisplayName</key>

ios/App/Podfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def capacitor_pods
1010
# Automatic Capacitor Pod dependencies, do not delete
1111
pod 'Capacitor', :path => '../../node_modules/@capacitor/ios'
1212
pod 'CapacitorCordova', :path => '../../node_modules/@capacitor/ios'
13-
13+
pod 'RdlaboCapacitorAdmob', :path => '../../node_modules/@rdlabo/capacitor-admob'
1414
# Do not delete
1515
end
1616

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@ionic-native/splash-screen": "^5.0.0",
2828
"@ionic-native/status-bar": "^5.0.0",
2929
"@ionic/angular": "^4.7.1",
30+
"@rdlabo/capacitor-admob": "^0.1.4",
3031
"core-js": "^2.5.4",
3132
"rxjs": "~6.5.1",
3233
"tslib": "^1.9.0",

src/app/app.component.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Component } from '@angular/core';
33
import { Platform } from '@ionic/angular';
44
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
55
import { StatusBar } from '@ionic-native/status-bar/ngx';
6+
import { Plugins } from '@capacitor/core';
67

78
@Component({
89
selector: 'app-root',
@@ -16,6 +17,9 @@ export class AppComponent {
1617
private statusBar: StatusBar
1718
) {
1819
this.initializeApp();
20+
Plugins.AdMob.initialize(
21+
platform.is('ios') ? 'ca-app-pub-1053575285730954~8646458891' : 'ca-app-pub-1053575285730954~3749355430'
22+
);
1923
}
2024

2125
initializeApp() {

src/app/tab2/tab2.page.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@
66
</ion-toolbar>
77
</ion-header>
88

9-
<ion-content></ion-content>
9+
<ion-content>
10+
<div class="ion-padding">
11+
<ion-button (click)="displayAdMob()" *ngIf="!isAdsense" color="primary">広告をだす</ion-button>
12+
<ion-button (click)="hideAdMob()" *ngIf="isAdsense" color="danger">広告を隠す</ion-button>
13+
</div>
14+
</ion-content>

src/app/tab2/tab2.page.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
import { Component } from '@angular/core';
2+
import { Plugins } from '@capacitor/core';
3+
import { AdOptions, AdSize, AdPosition } from '@rdlabo/capacitor-admob';
24

35
@Component({
46
selector: 'app-tab2',
57
templateUrl: 'tab2.page.html',
68
styleUrls: ['tab2.page.scss']
79
})
810
export class Tab2Page {
9-
11+
isAdsense = false;
1012
constructor() {}
1113

14+
displayAdMob() {
15+
const options: AdOptions = {
16+
adId: 'ca-app-pub-3940256099942544/6300978111',
17+
adSize: AdSize.BANNER,
18+
position: AdPosition.BOTTOM_CENTER,
19+
margin: '60',
20+
};
21+
Plugins.AdMob.showBanner(options).then(
22+
success => this.isAdsense = true,
23+
error => this.isAdsense = false
24+
);
25+
}
26+
27+
hideAdMob() {
28+
Plugins.AdMob.hideBanner().then(
29+
success => this.isAdsense = false
30+
);
31+
}
1232
}

yarn.lock

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@
329329
which "^1.3.0"
330330
xml2js "^0.4.19"
331331

332-
"@capacitor/[email protected]":
332+
"@capacitor/[email protected]", "@capacitor/core@latest":
333333
version "1.1.1"
334334
resolved "https://registry.yarnpkg.com/@capacitor/core/-/core-1.1.1.tgz#8e32270d2aa7a843c40b14e4d10c76c62e451d49"
335335
integrity sha512-7jnPMy9rGt5VB2QS/1xYEhHspnh2J8i9j0JA6uUtwCUBQs5bmNCqiQ8MYjHTW5NE+1jl2rnX8CU4d8sUT3/esg==
@@ -399,6 +399,13 @@
399399
tree-kill "1.2.1"
400400
webpack-sources "1.3.0"
401401

402+
"@rdlabo/capacitor-admob@^0.1.4":
403+
version "0.1.4"
404+
resolved "https://registry.yarnpkg.com/@rdlabo/capacitor-admob/-/capacitor-admob-0.1.4.tgz#83ed9da9e3e4a84dd090a2a5c6535659bc793511"
405+
integrity sha512-P8ltIc3l8OUUBvhAWEjqM1zI5/v5YcCnYYsSevGYCLJbBhkpTUYmtoHToCrFmmEhzY3nZ98nKu+gNvOU+KBDRw==
406+
dependencies:
407+
"@capacitor/core" latest
408+
402409
"@schematics/[email protected]":
403410
version "8.1.3"
404411
resolved "https://registry.yarnpkg.com/@schematics/angular/-/angular-8.1.3.tgz#dc465dae7ef658ac945551e12990676fdac188ec"

0 commit comments

Comments
 (0)