facebook twitter hatena line email

Cordova/admob/基本

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

ダウンロード

https://github.com/admob-plus/admob-plus

zipを落としてきて、unzipして適当なdirに置く。

admob-plusプラグインインストール

既にされてると思うが、されてなければ

cordova plugin add cordova-admob-plus --variable APP_ID_ANDROID=ca-app-pub-3940256099942544~3347511713 --variable APP_ID_IOS=ca-app-pub-3940256099942544~1458002511

上記IDはadmob公式のサンプルidです。

androidxを使う場合は以下プラグインをインストール

cordova plugin add cordova-plugin-androidx
cordova plugin add cordova-plugin-androidx-adapter

Cordovaベースが存在しないとき

以下エラーが発生するとき

Current working directory is not a Cordova-based project.

wwwを作成する。もしくは以下のようなconfig.xmlが無いかもなので作成する。

config.xml

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.example.admobplus" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>admobplus</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="dev@cordova.apache.org" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <plugin name="cordova-plugin-whitelist" spec="1" />
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <platform name="android">
        <allow-intent href="market:*" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
    </platform>
</widget>

admobのサンプルをビルド

cd examples/basic

androidの場合

$ cordova platform add android
$ cordova requirements android
$ cordova build android

platforms/androidにプロジェクトが出力されていればよい。

$ cordova run android

テストフラグ付きで、リワード&インステ静止画&フッターバナーが起動する

iosの場合

$ cordova platform add ios
$ cordova requirements ios
$ cordova build ios

admob-plus-master/examples/basic/platforms/ios/build/device にexportされることを確認。

$ cordova run ios

起動されることを確認。

認証がエラーとなる場合

cordova build iosで以下エラーが出る場合

error: Signing for "AdmobBasicExample" requires a development team. Select a development team in the Signing & Capabilities editor. (in target 'AdmobBasicExample' from project 'AdmobBasicExample')

platforms/ios/AdmobBasicExample.xcworkspaceを開いて、アプリターゲットからSigning & Capabilitiesを選択し、Teamの設定をする

value for SWIFT_VERSION cannot be empty.エラーとなる場合

cordova build iosで以下エラーが出る場合

vi platforms/ios/Pods/Pods.xcodeproj/project.pbxproj

修正前

SWIFT_VERSION = "";

修正後

SWIFT_VERSION = 5.0;

'sendSubviewToBack' has been renamed to 'sendSubview(toBack:)'エラーとなる場合

plugins/cordova-admob-plus/src/ios/AMSBanner.swiftを以下のように変更する

修正前

view.sendSubviewToBack(background)

修正後

view.sendSubview(toBack: background)

GADMobileAdsなどがないエラー

Use of unresolved identifier 'GADMobileAds'
Use of undeclared type 'GADInterstitialDelegate'

以下podを更新して、Google-Mobile-Adsを入れる

$ cd examples/basic/platforms/ios
$ pod update
Installing Google-Mobile-Ads-SDK 7.60.0 (was 7.57.0 and source changed to `https://cdn.cocoapods.org/` from `trunk`)

コードの1行目に以下を追加

import GoogleMobileAds

直らなければ、Podfileを以下のように

target 'AdmobBasicExample' do
    project 'AdmobBasicExample.xcodeproj'
    use_frameworks!
    pod 'Firebase/Core'
    pod 'Firebase/AdMob'
end

GOOGLE_APP_ID in GoogleService-Info.plistのエラーが出るとき

PodFileに pod 'Firebase/AdMob'を追加すると以下が出るかも。

AdmobBasicExample[11216:228420]  - <AppMeasurement>[I-ACS020006] Google App ID from GoogleService-Info.plist is empty. Please, define GOOGLE_APP_ID in GoogleService-Info.plist for Analytics to work reliably. See https://goo.gl/txkZbE

plistは以下ファイルしか無いが、そこにGoogle App IDを足すのかも?

platforms/ios/exportOptions.plist
platforms/ios/AdmobBasicExample/AdmobBasicExample-Info.plist

firebaseのgoogleIdが必要になるので、Podfileを以下サンプルにあるように、戻したほうが良いかも

target 'AdmobBasicExample' do
        project 'AdmobBasicExample.xcodeproj'
        pod 'Google-Mobile-Ads-SDK', '~> 7.57'
end

Error Domain=NSCocoaErrorDomainが出るとき

以下のように出る

AdmobBasicExample[44159:626729] [Client] Updating selectors after delegate addition failed with: Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service 
on pid 0 named com.apple.commcenter.coretelephony.xpc was invalidated." UserInfo={NSDebugDescription=The connection to service on pid 0 named 
com.apple.commcenter.coretelephony.xpc was invalidated.}

ネット接続できてなければ、ネット接続を確認する。

com.apple.commcenter.coretelephony.xpcはそもそも使えない?であれば無視がよいか・・。

参考: https://www.it-swarm.dev/ja/ios/comapplecommcentercoretelephonyxpc%E3%81%A8%E3%81%84%E3%81%86%E5%90%8D%E5%89%8D%E3%81%AE%E3%82%B5%E3%83%BC%E3%83%93%E3%82%B9%E3%81%B8%E3%81%AE%E6%8E%A5%E7%B6%9A%E3%81%8C%E7%84%A1%E5%8A%B9%E5%8C%96%E3%81%95%E3%82%8C%E3%81%BE%E3%81%97%E3%81%9F/807412954/

NSURLErrorDomain error -999が出るとき

以下のように出る

AdmobBasicExample[49060:689390] Failed to load webpage with error: The operation couldn’t be completed. (NSURLErrorDomain error -999.)

広告ができないとき

ここのappIdがあってるか。

platforms/ios/AdmobBasicExample/AdmobBasicExample-Info.plist

admob is using testApplicationIDが表示されるとき

platforms/ios/AdmobBasicExample/Plugins/cordova-admob-plus/AMSPlugin.swift のapplicationIDに直にappIdを、以下のように入れたいidを入れると、どうなるか。

let applicationID = "ca-app-pub-hogehoge" as? String

各種フォーマット広告設置方法

公式マニュアル:https://admob-plus.github.io/docs/show-banner.html

テストIdのサンプル

バナー、静止画インステ、リワード全部入り

config.xml

<plugin name="cordova-admob-plus" spec="../../packages/cordova">
    <variable name="APP_ID_ANDROID" value="ca-app-pub-3940256099942544~3347511713" />
    <variable name="APP_ID_IOS" value="ca-app-pub-3940256099942544~1458002511" />
</plugin>

www/js/index.js

'use strict'

const app = {
  initialize() {
    document.addEventListener(
      'deviceready',
      this.onDeviceReady.bind(this),
      false,
    )
  },
  onDeviceReady() {
    this.receivedEvent('deviceready')

    this.checkIsLoaded().then(() => this.showAds())
  },
  checkIsLoaded() {
    return admob.interstitial.isLoaded().then(result => {
      alert(result)
      return result
    })
  },
  showAds() {
    admob.banner.show({ id: 'test', npa: '1' }).catch(console.log)
    admob.interstitial
      .load({ id: 'test' })
      .then(() => admob.interstitial.show())
      .catch(console.log)
    admob.rewardVideo
      .load({ id: 'test' })
      .then(() => admob.rewardVideo.show())
      .catch(console.log)
  },
  receivedEvent(id) {
    const parentElement = document.getElementById(id)
    const listeningElement = parentElement.querySelector('.listening')
    const receivedElement = parentElement.querySelector('.received')
    listeningElement.setAttribute('style', 'display:none;')
    receivedElement.setAttribute('style', 'display:block;')
    console.log(`Received Event: ${id}`)
  },
}

app.initialize()

バナー

document.addEventListener('deviceready', () => {
  admob.banner.show({
    id: {
      // replace with your ad unit IDs
      android: 'ca-app-pub-xxx/yyy',
      ios: 'ca-app-pub-xxx/zzz',
    },
  }).then(() => {
    setTimeout(() => {
      admob.banner.hide({
        // replace with your ad unit IDs
        android: 'ca-app-pub-xxx/yyy',
        ios: 'ca-app-pub-xxx/zzz',
      })
    }, 10000)
  })
}, false)

インステ

document.addEventListener('deviceready', () => {
  admob.interstitial.load({
    id: {
      // replace with your ad unit IDs
      android: 'ca-app-pub-xxx/yyy',
      ios: 'ca-app-pub-xxx/zzz',
    },
  }).then(() => admob.interstitial.show())
}, false)

リワード

document.addEventListener('deviceready', () => {
  admob.rewardVideo.load({
    id: {
      // replace with your ad unit IDs
      android: 'ca-app-pub-xxx/yyy',
      ios: 'ca-app-pub-xxx/zzz',
    },
  }).then(() => admob.rewardVideo.show())
}, false)

その他参考

ca-app-pub-が書かれてる場所一覧

AppIdや広告Idが違うなどと出たら、確認する。

.//platforms/ios/AdmobBasicExample/Plugins/cordova-admob-plus/AMSPlugin.swift
.//platforms/ios/AdmobBasicExample/AdmobBasicExample-Info.plist
.//platforms/ios/www/plugins/cordova-admob-plus/www/admob.js
.//platforms/ios/ios.json
.//platforms/ios/platform_www/plugins/cordova-admob-plus/www/admob.js
.//platforms/ios/Pods/Google-Mobile-Ads-SDK/Frameworks/GoogleMobileAdsFramework-Current/GoogleMobileAds.framework/GoogleMobileAds
.//platforms/ios/Pods/Google-Mobile-Ads-SDK/Frameworks/GoogleMobileAdsFramework-Current/GoogleMobileAds.framework/Headers/GADRewardedAd.h
.//platforms/ios/Pods/Google-Mobile-Ads-SDK/Frameworks/GoogleMobileAdsFramework-Current/GoogleMobileAds.framework/Headers/GADInterstitial.h
.//platforms/ios/Pods/Google-Mobile-Ads-SDK/Frameworks/GoogleMobileAdsFramework-Current/GoogleMobileAds.framework/Headers/GADNativeExpressAdView.h
.//platforms/ios/Pods/Google-Mobile-Ads-SDK/Frameworks/GoogleMobileAdsFramework-Current/GoogleMobileAds.framework/Headers/GADBannerView.h
.//platforms/ios/AdmobBasicExample.xcarchive/Products/Applications/AdmobBasicExample.app/AdmobBasicExample
.//platforms/ios/AdmobBasicExample.xcarchive/Products/Applications/AdmobBasicExample.app/www/plugins/cordova-admob-plus/www/admob.js
.//platforms/ios/AdmobBasicExample.xcarchive/Products/Applications/AdmobBasicExample.app/Info.plist
.//platforms/android/app/build/intermediates/bundle_manifest/debug/processDebugManifest/bundle-manifest/AndroidManifest.xml
.//platforms/android/app/build/intermediates/merged_manifests/debug/AndroidManifest.xml
.//platforms/android/app/build/intermediates/instant_app_manifest/debug/processDebugManifest/instant-app-manifest/AndroidManifest.xml
.//platforms/android/app/build/intermediates/merged_assets/debug/mergeDebugAssets/out/www/plugins/cordova-admob-plus/www/admob.js
.//platforms/android/app/build/intermediates/transforms/dexMerger/debug/0/classes.dex
.//platforms/android/app/build/intermediates/transforms/externalLibsDexMerger/debug/0/classes.dex
.//platforms/android/app/build/intermediates/transforms/dexBuilder/debug/41/admob/plugin/AdMob.dex
.//platforms/android/app/build/intermediates/javac/debug/compileDebugJavaWithJavac/classes/admob/plugin/AdMob.class
.//platforms/android/app/src/main/AndroidManifest.xml
.//platforms/android/app/src/main/java/admob/plugin/AdMob.java
.//platforms/android/app/src/main/assets/www/plugins/cordova-admob-plus/www/admob.js
.//platforms/android/android.json
.//platforms/android/platform_www/plugins/cordova-admob-plus/www/admob.js
.//plugins/cordova-admob-plus/www/admob.js
.//plugins/cordova-admob-plus/www/base.d.ts
.//plugins/cordova-admob-plus/ts/base.ts
.//plugins/cordova-admob-plus/src/ios/AMSPlugin.swift
.//plugins/cordova-admob-plus/src/android/AdMob.java
.//plugins/android.json
.//plugins/fetch.json
.//plugins/ios.json
.//node_modules/cordova-admob-plus/www/admob.js
.//node_modules/cordova-admob-plus/www/base.d.ts
.//node_modules/cordova-admob-plus/ts/base.ts
.//node_modules/cordova-admob-plus/src/ios/AMSPlugin.swift
.//node_modules/cordova-admob-plus/src/android/AdMob.java
.//config.xml
.//package.json