
ブログ
そんなあなたに向けて、初心者でもわかりやすく解説します!
AdMob(Google Mobile Ads)はGoogleが提供するアプリ内広告サービスです。
ca-app-pub-xxxx~yyyy
形式)sudo gem install cocoapods
pod init
以下のようにPodfileを編集:
platform :ios, '13.0'
target 'YourAppName' do
use_frameworks!
pod 'Google-Mobile-Ads-SDK'
end
pod install
YourApp.xcodeproj ではなく YourApp.xcworkspace を開いてください!
プロジェクトのInfo.plist に以下を追加:
<key>GADApplicationIdentifier</key>
<string>ca-app-pub-xxxx~yyyy</string>
import UIKit
import GoogleMobileAds
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
MobileAds.shared.start(completionHandler: nil)
return true
}
}
import SwiftUI
import GoogleMobileAds
struct BannerAdView: UIViewRepresentable {
func makeUIView(context: Context) -> BannerView {
let banner = BannerView(adSize: AdSizeBanner)
banner.adUnitID = "ca-app-pub-3940256099942544/2934735716"
banner.rootViewController = UIApplication.shared.connectedScenes
.compactMap { $0 as? UIWindowScene }
.first?.windows.first?.rootViewController
banner.load(Request())
return banner
}
func updateUIView(_ uiView: BannerView, context: Context) {}
}
struct ContentView: View {
var body: some View {
VStack {
Spacer()
Text("アプリのメイン画面")
Spacer()
BannerAdView()
.frame(width: 320, height: 50)
}
}
}
import GoogleMobileAds
import SwiftUI
class InterstitialAdManager: NSObject, FullScreenContentDelegate {
private var interstitial: InterstitialAd?
private var adUnitID: String
init(adUnitID: String) {
self.adUnitID = adUnitID
super.init()
loadAd()
}
func loadAd() {
let request = Request()
InterstitialAd.load(with: adUnitID, request: request) { ad, error in
if let error = error {
print("❌ Failed to load interstitial ad: \(error.localizedDescription)")
return
}
self.interstitial = ad
self.interstitial?.fullScreenContentDelegate = self
print("✅ Interstitial ad loaded")
}
}
func showAd(from rootViewController: UIViewController) {
if let ad = interstitial {
ad.present(from: rootViewController)
} else {
print("⚠️ Interstitial ad not ready")
}
}
// Ad closed → 次をロード
func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
print("🔁 Ad dismissed. Loading next...")
loadAd()
}
}
struct ContentView: View {
let interstitial = InterstitialAdManager(adUnitID: "ca-app-pub-3940256099942544/4411468910") // テスト用ID
var body: some View {
VStack {
Button("全画面広告を表示") {
if let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let root = scene.windows.first?.rootViewController {
interstitial.showAd(from: root)
}
}
}
}
}
// 5問ごとに広告を表示
if questionCount % 5 == 0 {
if let root = UIApplication.shared.connectedScenes.first?.delegate as? UIWindowSceneDelegate,
let vc = root.window??.rootViewController {
interstitial.showAd(from: vc)
}
}
AppDelegate.swiftに以下のコードを追加する
MobileAds.shared.requestConfiguration.tagForChildDirectedTreatment = true
Sandbox: bash(xxxx) deny(1) file-write-create・・・
Xcodeの「Build Settings」 > 「Build Options」内にある ENABLE_USER_SCRIPT_SANDBOXING
を No
に設定してください。
これにより、Pod関連のビルド時に発生するSandbox制限エラーを回避できる可能性があります。
AdMobはアプリ収益化において非常に有力な手段です。特に教育・キッズ向けアプリでは、広告の表示方法とプライバシー配慮が必須になります。このガイドをもとに、安全かつ効果的に広告を導入しましょう。
この記事へのトラックバックはありません。
この記事へのコメントはありません。