SwiftUI के साथ SDK टूल का इंटिग्रेशन

इस गाइड में, सहमति वाले फ़ॉर्म की मदद से उपयोगकर्ता की सहमति की जानकारी का अनुरोध करने का तरीका बताया गया है. इसके बाद, 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का इस्तेमाल करने वाले उपयोगकर्ता से सहमति ली है या नहीं. सहमति लेते समय दो चीज़ों की जांच की जा सकती है:

  1. मौजूदा सेशन में सहमति लेने के बाद
  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(...)
  }
}