ادغام SDK با SwiftUI

این راهنما نحوه درخواست اطلاعات رضایت کاربر را از طریق فرم رضایت و سپس بررسی رضایت کاربر هنگام درخواست تبلیغات با پلت فرم پیام‌رسانی کاربر (UMP) SDK در برنامه 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 فرمی را بارگیری می‌کند و بلافاصله آن را از نمای کنترل‌کننده ارائه شده ارائه می‌کند. کنترل کننده تکمیل پس از رد شدن فرم فراخوانی می شود. اگر رضایت لازم نباشد، کنترل کننده تکمیل فوراً فراخوانی می شود.

یک UIViewControllerRepresentable ایجاد کنید

از آنجایی که 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 شما نباید هیچ اهمیتی برای محتوای روی صفحه داشته باشد، اما همچنان باید به سلسله مراتب view اضافه شود. آن را در 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(...)
  }
}