คู่มือนี้แสดงวิธีขอข้อมูลความยินยอมของผู้ใช้ผ่านแบบฟอร์มความยินยอม จากนั้นตรวจสอบความยินยอมของผู้ใช้เมื่อขอโฆษณาด้วย SDK สำหรับ User Messaging Platform (UMP) ในแอป SwiftUI
ข้อกำหนดเบื้องต้น
คำขอข้อมูลความยินยอม
คุณควรขออัปเดตข้อมูลความยินยอมของผู้ใช้ทุกครั้งที่เปิดแอปโดยใช้ requestConsentInfoUpdateWithParameters:completionHandler:
ซึ่งจะเป็นตัวกำหนดว่าผู้ใช้จะต้องให้ความยินยอมหากยังไม่ได้ดำเนินการ หรือเมื่อคำยินยอมหมดอายุแล้ว
ต่อไปนี้คือตัวอย่างวิธีตรวจสอบสถานะจาก View
ในเมธอด onAppear(perform:)
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.
}
}
}
}
โหลดและแสดงแบบฟอร์มความยินยอมหากจำเป็น
หลังจากได้รับสถานะความยินยอมเวอร์ชันล่าสุดแล้ว โปรดเรียกใช้loadAndPresentIfRequiredFromViewController:completionHandler:
ในชั้นเรียนUMPConsentForm
เพื่อโหลดแบบฟอร์มความยินยอม หากต้องระบุสถานะความยินยอม SDK จะโหลดแบบฟอร์มและแสดงจากตัวควบคุมข้อมูลพร็อพเพอร์ตี้ที่ระบุทันที ระบบจะเรียกเครื่องจัดการการเสร็จสมบูรณ์
หลังจากที่ปิดแบบฟอร์ม หากไม่จำเป็นต้องได้รับความยินยอม ระบบจะเรียกใช้เครื่องจัดการการเสร็จสมบูรณ์ทันที
สร้าง UIViewControllerMonitorable
เนื่องจาก loadAndPresentIfRequiredFromViewController:completionHandler:
ต้องใช้ออบเจ็กต์ UIViewController
โปรดสร้างประเภทที่สอดคล้องกับโปรโตคอล UIViewControllerRepresentable
งานหลักคือการเปิดเผยการอ้างอิง UIViewController
ให้เข้าถึงได้ใน SwiftUI
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
หรือไม่ มี 2 อินสแตนซ์ให้ตรวจสอบขณะรวบรวมความยินยอม ได้แก่
- หลังจากรวบรวมความยินยอมในเซสชันปัจจุบันแล้ว
- ทันทีหลังจากที่คุณโทรหา
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(...)
}
}