이 가이드에서는 동의 양식을 통해 사용자 동의 정보를 요청하고, SwiftUI 앱에서 사용자 메시지 플랫폼 (UMP) SDK를 사용하여 광고를 요청할 때 사용자 동의를 확인하는 방법을 설명합니다.
기본 요건
- 시작 가이드 읽기
동의 정보 요청
앱을 실행할 때마다 requestConsentInfoUpdateWithParameters:completionHandler:
를 사용하여 사용자 동의 정보 업데이트를 요청해야 합니다. 이를 통해 사용자가 아직 동의를 제공하지 않았는지 또는 동의가 만료되었는지 여부를 판단합니다.
다음은 onAppear(perform:)
메서드의 View
에서 상태를 확인하는 방법을 보여주는 예입니다.
struct ContentView: View {
@State private var hasViewAppeared = false
var body: some View {
VStack {
...
}
.onAppear {
// Gather consent only the first time the view appears.
guard !hasViewAppeared else { return }
hasViewAppeared = true
// Create a UMPRequestParameters object.
let parameters = UMPRequestParameters()
// Set tag for under age of consent. false means users are not under age
// of consent.
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)")
}
// TODO: Load and present the consent form.
}
}
}
}
필요한 경우 동의 양식 로드 및 표시
최신 동의 상태를 수신했으면
UMPConsentForm
클래스에서
loadAndPresentIfRequiredFromViewController:completionHandler:
를 호출하여 동의 양식을 로드하세요. 동의 상태가 필요한 경우 SDK가 양식을 로드하고 제공된 뷰 컨트롤러에서 즉시 표시합니다. 양식을 닫은 후에 완료 핸들러가
호출됩니다. 동의가 필요하지 않으면 완료 핸들러가 즉시 호출됩니다.
UIViewControllerRepresentable 만들기
loadAndPresentIfRequiredFromViewController:completionHandler:
에는 UIViewController
객체가 필요하므로 UIViewControllerRepresentable
프로토콜을 준수하는 유형을 만듭니다. 기본 작업은 SwiftUI에서 액세스할 수 있도록 UIViewController
참조를 노출하는 것입니다.
struct FormViewControllerRepresentable: UIViewControllerRepresentable {
let viewController = UIViewController()
func makeUIViewController(context: Context) -> some UIViewController {
return viewController
}
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {}
}
ViewControllerRepresentable
유형은 화면의 콘텐츠에 중요하지 않지만 뷰 계층 구조에 추가해야 합니다. 뷰 수정자 background(alignment:content:)
내에 추가하여 화면 공간을 차지하지 않도록 합니다.
struct ContentView: View {
@State private var hasViewAppeared = false
private let formViewControllerRepresentable = FormViewControllerRepresentable()
var body: some View {
VStack {
...
}
.background {
// Add the ViewControllerRepresentable to the background so it
// doesn't influence the placement of other views in the view hierarchy.
formViewControllerRepresentable
.frame(width: .zero, height: .zero)
}
.onAppear {
// Gather consent only the first time the view appears.
guard !hasViewAppeared else { return }
hasViewAppeared = true
// Create a UMPRequestParameters object.
let parameters = UMPRequestParameters()
// Set tag for under age of consent. false means users are not under age
// of consent.
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)")
}
UMPConsentForm.loadAndPresentIfRequired(from:
formViewControllerRepresentable.viewController) { loadAndPresentError in
if let consentError = loadAndPresentError {
// Consent gathering failed.
return print("Error: \(consentError.localizedDescription)")
}
// Consent gathering completed.
}
}
}
}
}
광고 요청
앱에서 광고를 요청하기 전에 UMPConsentInformation.sharedInstance.canRequestAds
를 사용하여
사용자의 동의를 얻었는지 확인하세요. 동의를 수집하는 동안
확인할 수 있는 두 가지 경우가 있습니다.
- 현재 세션에서 동의를 수집한 후
-
requestConsentInfoUpdateWithParameters:completionHandler:
에 전화한 직후
이전 세션에서 동의를 받았을 수 있습니다. 지연 시간 권장사항에 따라 앱 실행 후 최대한 빨리 광고 로드를 시작할 수 있도록 콜백이 완료될 때까지 기다리지 않는 것이 좋습니다.
동의 수집 프로세스 중에 오류가 발생해도 광고를 요청해야 합니다. UMP SDK는 이전 세션의 동의 상태를 사용합니다.
struct ContentView: View {
@State private var isMobileAdsStartCalled = false
@State private var hasViewAppeared = false
private let formViewControllerRepresentable = FormViewControllerRepresentable()
var body: some View {
VStack {
...
}
.background {
// Add the ViewControllerRepresentable to the background so it
// doesn't influence the placement of other views in the view hierarchy.
formViewControllerRepresentable
.frame(width: .zero, height: .zero)
}
.onAppear {
// Gather consent only the first time the view appears.
guard !hasViewAppeared else { return }
hasViewAppeared = true
// Create a UMPRequestParameters object.
let parameters = UMPRequestParameters()
// Set tag for under age of consent. false means users are not under age
// of consent.
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)")
}
UMPConsentForm.loadAndPresentIfRequired(from:
formViewControllerRepresentable.viewController) { (loadAndPresentError) in
if let consentError = loadAndPresentError {
// Consent gathering failed.
return print("Error: \(consentError.localizedDescription)")
}
// Consent gathering completed.
if UMPConsentInformation.sharedInstance.canRequestAds {
startGoogleMobileAdsSDK()
}
}
}
// Check if you can initialize the Google Mobile Ads SDK in parallel
// while checking for new consent information. Consent obtained in
// the previous session can be used to request ads.
if UMPConsentInformation.sharedInstance.canRequestAds {
startGoogleMobileAdsSDK()
}
}
}
private func startGoogleMobileAdsSDK() {
guard !isMobileAdsStartCalled else { return }
isMobileAdsStartCalled = true
// Initialize the Google Mobile Ads SDK.
GADMobileAds.sharedInstance().start()
// TODO: Request an ad.
// GADInterstitialAd.load(...)
}
}