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.
      }
    }
  }
}

প্রয়োজনে একটি সম্মতি ফর্ম লোড করুন এবং উপস্থাপন করুন

আপনি সবচেয়ে আপ-টু-ডেট সম্মতির স্ট্যাটাস পাওয়ার পরে, একটি সম্মতি ফর্ম লোড করতে l10n-placeholder14 ক্লাসেUMPConsentFormloadAndPresentIfRequiredFromViewController:completionHandler: এ কল করুন। সম্মতির স্থিতি প্রয়োজন হলে, SDK একটি ফর্ম লোড করে এবং প্রদত্ত ভিউ কন্ট্রোলার থেকে অবিলম্বে উপস্থাপন করে। ফর্মটি খারিজ হওয়ার পরে সমাপ্তি হ্যান্ডলারকে ডাকা হয়। যদি সম্মতির প্রয়োজন না হয়, সমাপ্তি হ্যান্ডলারকে অবিলম্বে ডাকা হয়।

একটি UIViewController Representable তৈরি করুন

যেহেতু 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(...)
  }
}