site-icon Hong Cheung

AdMob-GDPR-Einwilligungsnachricht für iOS mit SwiftUI hinzufügen

Jan 18, 2024
Programming iOSSwiftUIAdsEEAAdMob
4 Minuten
680 Wörter
AdMob-GDPR-Einwilligungsnachricht für iOS mit SwiftUI hinzufügen

Warum?

Seit dem 16. Januar 2024 müssen wir unseren Apps eine GDPR-Einwilligungsnachricht hinzufügen, da die EWR-Anforderungen vorschreiben, Nutzern in Europa Werbung anzuzeigen.

Wenn wir das nicht tun, werden die Werbeeinnahmen stark beeinträchtigt.

Wie?

Du kannst dir dieses offizielle Video von Google AdMob ansehen, um zu erfahren, wie man das umsetzt.

Offizielles Video, das zeigt, wie man mit dem User Messaging Platform (UMP) SDK die Nutzereinwilligung für die GDPR einholt

Doch wie immer zeigt die Google-Dokumentation nur den UIKit-Weg, um das hinzuzufügen, nicht SwiftUI.

SwiftUI-Beispiel

Nach einem Tag des Ausprobierens habe ich es endlich mit reinem SwiftUI zum Laufen gebracht.

Es ist eigentlich ganz einfach:

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
}
}
}
}
}
}
}
}
}

Ja, die if let-Kaskade sieht hässlich aus, weil sie halb von ChatGPT stammt, aber du verstehst das Prinzip. Ich habe sie angepasst und alles in eine andere View ausgelagert.

Für jede App, die ich erstelle, kopiere ich also einfach diese View und füge sie ein. So würde ich sie in meiner Haupt-View verwenden:

//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)
}
}
}

Personalisierte Werbung

Um zu steuern, ob personalisierte Werbung angezeigt wird, haben wir früher die Werbeanfrage-Option ungefähr so gesetzt:

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

Denk daran, dass wir ATT für die App-Store-Konformität einrichten müssen.

Allerdings bringt es jetzt nichts mehr, mit Google AdMob zu interagieren. Denn wenn du UserMessagingPlatform verwendest, verwaltet Google die „Personalisierung“ auf seiner Plattform.

„Falls das noch jemandem hilft: Bestätigt, dass man bei Verwendung des UMP SDK für Android npa=1 nicht setzen muss. Das UMP SDK schreibt den TCF-String in die Shared Preferences (relevante Infos unter https://support.google.com/admob/answer/9760862), und das Google Mobile Ads SDK liest den TCF-String bei Werbeanfragen aus.“ – Eric, Mobile Ads SDK Team – Mehr erfahren

Das habe ich im Google AdMob-Forum gelesen.

Obwohl sie sagten, es gelte für Android, gehe ich davon aus, dass es auf iOS genauso funktioniert.

Aber wir müssen ATT für Apple trotzdem implementieren, also behalte ich es vorerst bei.

Ich hoffe, diese Anleitung vereinfacht die Dinge ein wenig und hilft dir.

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!

Artikeltitel:AdMob-GDPR-Einwilligungsnachricht für iOS mit SwiftUI hinzufügen
Artikelautor:Hong C.
Veröffentlichungszeit:Jan 18, 2024
Copyright 2026
Sitemap
Datenschutzerklärung
Nutzungsbedingungen

Theme von Astro-Yi