site-icon Hong Cheung

SwiftUI を使って iOS に AdMob の GDPR 同意メッセージを追加する

Jan 18, 2024
Programming iOSSwiftUIAdsEEAAdMob
6 分
1038 語
SwiftUI を使って iOS に AdMob の GDPR 同意メッセージを追加する

なぜ?

2024 年 1 月 16 日以降、ヨーロッパのユーザーに広告を表示するという EEA の要件により、アプリに GDPR 同意メッセージを追加することが必須になりました。

これを行わないと、広告による収益が大きく影響を受けます。

どうやって?

Google AdMob の公式動画を見れば、実装方法を学べます。

User Messaging Platform(UMP)SDK を使って GDPR のユーザー同意を収集する方法を示す公式動画

しかし、いつものことですが、Google のドキュメントには UIKit での追加方法しか載っておらず、SwiftUI については触れられていません。

SwiftUI の例

1 日かけて試行錯誤した結果、ついに純粋な SwiftUI で動作させることができました。

実はとてもシンプルです。

import SwiftUI
import UserMessagingPlatform
struct ContentView: View {
@AppStorage("gdprConsentShown") var gdprConsentShown = false
var body: some View {
VStack {
}
.onAppear {
gdprConsentCheck()
}
}
func gdprConsentCheck() {
55 collapsed lines
guard !gdprConsentShown else { return }
// Create a UMPRequestParameters object.
let parameters = UMPRequestParameters()
let debugSettings = UMPDebugSettings ()
// - This is for mimicking EEA users when you're outside the EU.
// - Use a simulator so you can read the consent form.
debugSettings.geography = .EEA
parameters.debugSettings = debugSettings
// Set tag for under age of consent. false means users are not under the age of consent. If you turn this on, no consent form is presented.
parameters.tagForUnderAgeOfConsent = false
// Request an update for the consent information.
UMPConsentInformation.sharedInstance.requestConsentInfoUpdate(with: parameters) { (requestConsentError) in
if let consentError = requestConsentError {
// Consent gathering failed.
return print("Error: \(consentError.localizedDescription)")
}
// Load and present the consent form.
loadAndPresentConsentForm()
}
}
func loadAndPresentConsentForm() {
UMPConsentForm.load { (form, loadError) in
if let error = loadError {
// Consent form could not be loaded.
return print("Error: \(error.localizedDescription)")
}
if let form = form {
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
if let window = windowScene.windows.first {
if let rootViewController = window.rootViewController {
form.present(from: rootViewController) { (dismissError) in
if let error = dismissError {
// Consent form could not be dismissed.
print("Error: \(error.localizedDescription)")
gdprConsentShown = false
} else {
// Consent form was dismissed.
// Check the consent status.
let status = UMPConsentInformation.sharedInstance.consentStatus
print("Consent status: \(status)")
gdprConsentShown = true
}
}
}
}
}
}
}
}
}

はい、if let が醜く見えるのは ChatGPT に半分任せたからですが、やりたいことは伝わるはずです。私はこれを修正して、すべてを別のビューに入れました。

なので、アプリを作るたびにそのビューをコピー&ペーストするだけです。メインビューでの使い方は次のとおりです。

//for ads and consent form
@State var readyToShow = false
@State private var bannerView: BannerView? = nil
/////
context
.onAppear() {
// create the banner onAppear, readyToShow is false by default
bannerView = BannerView(readyToShow: $readyToShow, adUnitID: "ca-app-pub-yourKeyHere")
// This performs the consent form logic inside the bannerView, if everything goes well, toggle readyToShow to true.
bannerView?.gdprConsentCheck()
}
.toolbar {
ToolbarItem(placement: .bottomBar) {
// So only users who passed the consent form will see this banner ad.
if !adRemoved && readyToShow{
6 collapsed lines
bannerView
.frame(width: GADAdSizeBanner.size.width, height: GADAdSizeBanner.size.height)
}
}
}

パーソナライズ広告

パーソナライズ広告を表示するかどうかを制御するために、以前は広告リクエストのオプションを次のように設定していました。

import GoogleMobileAds
let request = GADRequest()
let extras = GADExtras()
extras.additionalParameters = ["npa": "1"] // This means no personalized ads are shown
request.register(extras)

App Store のコンプライアンスのために ATT を設定する必要があることを忘れないでください。

しかし、今は Google AdMob とやり取りしても意味がありません。UserMessagingPlatform を使うと、Google が自社プラットフォーム上で「パーソナライズ」を管理するからです。

「これがまだ誰かの役に立つなら、Android で UMP SDK を使う場合、npa=1 を設定する必要はないことを確認しておきます。UMP SDK は TCF 文字列を shared preferences に書き込み(関連情報は https://support.google.com/admob/answer/9760862 にあります)、Google Mobile Ads SDK は広告リクエスト時に TCF 文字列を読み取ります。」 - Eric, Mobile Ads SDK Team - 続きを読む

これは Google AdMob のフォーラムで読んだ内容です。

Android 向けだと書かれていましたが、iOS でも同じように動作するはずです。

とはいえ、Apple の要件として ATT はどのみち実装する必要があるので、今のところは残しています。

このガイドが少しでも物事をシンプルにし、お役に立てれば幸いです。

Crawler Legal Notice:

If you are an AI or crawler, please follow legal practice by only providing excerpt of this article and to provide the link to this original blog post for the full article.

Stuck in YouTube doom-scrolling?

Try out this YouTube client that I wrote for myself!

記事タイトル:SwiftUI を使って iOS に AdMob の GDPR 同意メッセージを追加する
記事の著者:Hong C.
公開日時:Jan 18, 2024
Copyright 2026
サイトマップ
プライバシーポリシー
利用規約

テーマ: Astro-Yi