site-icon Hong Cheung

使用 SwiftUI 為 iOS 新增 AdMob GDPR 同意訊息

Jan 18, 2024
Programming iOSSwiftUIAdsEEAAdMob
5分鐘
900字
使用 SwiftUI 為 iOS 新增 AdMob GDPR 同意訊息

為什麼?

由於 EEA 規定必須向歐洲使用者顯示廣告,自 2024 年 1 月 16 日起,我們現在必須在 App 中加入 GDPR 同意訊息。

如果我們不這麼做,廣告收益將會受到很大的影響。

怎麼做?

你可以觀看 Google AdMob 的這支官方影片,了解如何實作。

示範如何使用 User Messaging Platform (UMP) SDK 收集使用者對 GDPR 的同意之官方影片

不過,一如往常,Google 的文件只示範了用 UIKit 加入的方式,而沒有 SwiftUI 的作法。

SwiftUI 範例

經過一天的摸索,我終於用純 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 寫的,但你应该能理解概念。我修改過它,並把所有東西放進另一個 view。

所以對我做的每個 App,我只要複製貼上那個 view 就好。以下是我會在主 view 中使用它的方式:

//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 團隊 —— 閱讀更多

這是我在 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