site-icon Hong Cheung

SwiftUI를 사용하여 iOS용 AdMob GDPR 동의 메시지 추가하기

Jan 18, 2024
Programming iOSSwiftUIAdsEEAAdMob
6 분
1014 단어
SwiftUI를 사용하여 iOS용 AdMob GDPR 동의 메시지 추가하기

왜?

2024년 1월 16일 이후부터는 유럽 사용자에게 광고를 표시하기 위한 EEA 요구 사항 때문에 앱에 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로 만든 거라 못생겼지만, 요점은 이해하실 겁니다. 저는 이를 수정해서 모든 것을 다른 뷰에 넣었습니다.

그래서 제가 만드는 모든 앱에서 그 뷰를 복사해서 붙여넣기만 합니다. 제 메인 뷰에서 사용하는 방법은 다음과 같습니다:

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