Panduan ini menunjukkan cara meminta informasi izin pengguna melalui formulir izin, lalu memeriksa izin pengguna saat meminta iklan dengan User Messaging Platform (UMP) SDK di aplikasi SwiftUI.
Prasyarat
- Baca Panduan Memulai.
Meminta informasi izin
Anda harus meminta pembaruan informasi izin pengguna setiap kali aplikasi diluncurkan, menggunakan requestConsentInfoUpdateWithParameters:completionHandler:
. Hal ini menentukan apakah pengguna Anda perlu memberikan izin jika mereka belum melakukannya, atau jika masa berlaku izin mereka telah habis.
Berikut adalah contoh cara memeriksa status dari
View
dalam
metode
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.
}
}
}
}
Memuat dan menampilkan formulir izin jika diperlukan
Setelah Anda menerima status izin terbaru, panggilloadAndPresentIfRequiredFromViewController:completionHandler:
pada classUMPConsentForm
untuk memuat formulir izin. Jika
status izin diperlukan, SDK akan memuat formulir dan langsung menampilkannya
dari pengontrol tampilan yang disediakan. Pengendali penyelesaian dipanggil setelah
formulir ditutup. Jika izin tidak diperlukan, pengendali penyelesaian akan segera dipanggil.
Membuat UIViewControllerRepresentable
Karena loadAndPresentIfRequiredFromViewController:completionHandler:
memerlukan objek UIViewController
, buat jenis yang sesuai dengan protokol
UIViewControllerRepresentable
. Tugas utamanya adalah mengekspos referensi UIViewController
untuk diakses di SwiftUI.
struct FormViewControllerRepresentable: UIViewControllerRepresentable {
let viewController = UIViewController()
func makeUIViewController(context: Context) -> some UIViewController {
return viewController
}
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {}
}
Jenis ViewControllerRepresentable
Anda tidak boleh memiliki signifikansi terhadap
konten di layar, tetapi masih perlu ditambahkan ke hierarki tampilan. Tambahkan
dalam pengubah tampilan
background(alignment:content:)
agar tidak menggunakan ruang layar.
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.
}
}
}
}
}
Permintaan iklan
Sebelum meminta iklan di aplikasi, periksa apakah Anda telah memperoleh izin dari pengguna menggunakan UMPConsentInformation.sharedInstance.canRequestAds
. Ada dua instance yang harus diperiksa saat mengumpulkan izin:
- Setelah izin dikumpulkan pada sesi saat ini
- Segera setelah Anda menelepon
requestConsentInfoUpdateWithParameters:completionHandler:
Ada kemungkinan bahwa izin diperoleh pada sesi sebelumnya. Sebagai praktik terbaik tentang latensi, sebaiknya jangan menunggu callback selesai agar Anda dapat mulai memuat iklan sesegera mungkin setelah aplikasi diluncurkan.
Jika terjadi error selama proses pengumpulan izin, Anda tetap harus mencoba meminta iklan. UMP SDK menggunakan status izin dari sesi sebelumnya.
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(...)
}
}