पिक्चर में पिक्चर वाले विज्ञापन (बीटा वर्शन) लागू करना

प्लैटफ़ॉर्म चुनें: Android iOS

पिक्चर में पिक्चर

पिक्चर में पिक्चर (पीआईपी) वाले विज्ञापन, फ़्लोटिंग विंडो में दिखते हैं. यह विंडो, स्क्रीन पर मौजूद कॉन्टेंट के ऊपर दिखती है. जैसे, लेख, फ़ीड या गेमप्ले. इस फ़ॉर्मैट की मदद से, उपयोगकर्ता आपके ऐप्लिकेशन के साथ इंटरैक्ट कर सकते हैं. इस दौरान, विज्ञापन दिखता रहता है. अगर आपको ऐसे विज्ञापन दिखाने हैं जो पूरी स्क्रीन पर नहीं दिखते हैं, तो इस फ़ॉर्मैट को चुनें. पिक्चर-इन-पिक्चर मोड में दिखने वाले विज्ञापनों के बारे में ज़्यादा जानें.

इस गाइड में, Google Mobile Ads SDK का इस्तेमाल करके, अपने ऐप्लिकेशन में पिक्चर में पिक्चर वाले विज्ञापन दिखाने और उन्हें दिखाने का तरीका बताया गया है.

शुरू करने से पहले

जारी रखने से पहले, यह काम करें:

विज्ञापन लोड करना

GADPictureInPictureAd ऑब्जेक्ट लोड करने के लिए, विज्ञापन दिखाने का अनुरोध बनाएं और load तरीके को कॉल करें:

Swift

private func loadPictureInPictureAd() async {
  do {
    // Capture the PictureInPictureAd reference for later use.
    pipAd = try await PictureInPictureAd.load(
      with: adUnitID, request: Request())
    // Set the delegate to be notified of ad events.
    pipAd?.delegate = self
  } catch {
    print(
      "Picture-in-Picture ad failed to load: \(error.localizedDescription)")
  }
}

adUnitID की जगह अपना विज्ञापन यूनिट का आईडी डालें.

Objective-C

- (void)loadPictureInPictureAd {
  GADRequest *request = [GADRequest request];

  [GADPictureInPictureAd
       loadWithAdUnitID:kAdUnitID
                request:request
      completionHandler:^(GADPictureInPictureAd *_Nullable ad, NSError *_Nullable error) {
        if (error) {
          NSLog(@"Picture-in-Picture ad failed to load: %@", error);
          return;
        }
        // Capture the PictureInPictureAd reference for later use.
        self.pipAd = ad;
        // Set the delegate to be notified of ad events.
        self.pipAd.delegate = self;
      }];
}

kAdUnitID की जगह अपना विज्ञापन यूनिट का आईडी डालें.

विज्ञापन दिखाएं

स्क्रीन पर पिक्चर में पिक्चर वाला विज्ञापन दिखाने के लिए, पिक्चर में पिक्चर वाले विकल्प कॉन्फ़िगर करें और show तरीके को कॉल करें. यहां दिए गए उदाहरण में, विज्ञापन की डिफ़ॉल्ट जगह और प्रज़ेंटेशन स्कोप को स्क्रीन पर सेट किया गया है:

Swift

private func showPictureInPictureAd() {
  // Use the loaded PictureInPictureAd instance.
  guard let pipAd else {
    print("No ad to show.")
    return
  }
  let options = PictureInPictureAdOptions()
  // Uses the Google Mobile Ads SDK's default screen position.
  options.position = .default
  // Binds the ad lifecycle to the host screen.
  options.presentationScope = .screen

  pipAd.show(with: options)
}

Objective-C

- (void)showPictureInPictureAd {
  // Use the loaded PictureInPictureAd instance.
  if (!self.pipAd) {
    NSLog(@"No ad to show.");
    return;
  }
  GADPictureInPictureAdOptions *options =
      [[GADPictureInPictureAdOptions alloc] init];
  // Uses the Google Mobile Ads SDK's default screen position.
  options.position = GADPictureInPictureAdPositionDefault;
  // Binds the ad lifecycle to the host screen.
  options.presentationScope = GADPictureInPictureAdPresentationScopeScreen;

  [self.pipAd showWithOptions:options];
}

जगह सेट करना

डिफ़ॉल्ट रूप से, Google Mobile Ads SDK पहली बार दिखाए जाने पर, स्क्रीन के नीचे दाएं कोने में पिक्चर-इन-पिक्चर विज्ञापन दिखाता है. अगर इसे पहले दिखाया गया था, तो यह पिछली बार की जगह पर दिखता है. विज्ञापन दिखाने की जगह को पसंद के मुताबिक बनाने के लिए, पिक्चर-इन-पिक्चर के विकल्पों में जाकर विज्ञापन की जगह सेट करें. यहां दिए गए उदाहरण में, विज्ञापन को स्क्रीन पर सबसे ऊपर बाईं ओर मौजूद कॉन्टेंट के ऊपर दिखाने के लिए पोज़िशन सेट की गई है:

Swift

private func createTopLeftPositionOptions() -> PictureInPictureAdOptions {
  let options = PictureInPictureAdOptions()
  // Sets the ad position to the top-left corner of the screen.
  options.position = .topLeft
  return options
}

Objective-C

- (GADPictureInPictureAdOptions *)createTopLeftPositionOptions {
  GADPictureInPictureAdOptions *options =
      [[GADPictureInPictureAdOptions alloc] init];
  // Sets the ad position to the top-left corner of the screen.
  options.position = GADPictureInPictureAdPositionTopLeft;
  return options;
}

सभी उपलब्ध पोज़िशन देखने के लिए, GADPictureInPictureAdPosition पर जाएं.

प्रज़ेंटेशन का स्कोप सेट करना

डिफ़ॉल्ट रूप से, Google Mobile Ads SDK किसी पिक्चर-इन-पिक्चर विज्ञापन को मौजूदा होस्ट स्क्रीन से बाइंड करता है. Google Mobile Ads SDK विज्ञापन को तब खारिज करता है, जब होस्ट स्क्रीन की व्यू हैरारकी अब मेमोरी में नहीं होती. होस्ट स्क्रीन को मेमोरी से हटाने के बाद भी विज्ञापन को दिखने के लिए, प्रज़ेंटेशन स्कोप को ऐप्लिकेशन पर सेट करें:

Swift

private func createApplicationScopedOptions() -> PictureInPictureAdOptions {
  let options = PictureInPictureAdOptions()
  // Keeps the ad visible beyond the host screen's lifecycle.
  options.presentationScope = .application
  return options
}

Objective-C

- (GADPictureInPictureAdOptions *)createApplicationScopedOptions {
  GADPictureInPictureAdOptions *options =
      [[GADPictureInPictureAdOptions alloc] init];
  // Keeps the ad visible beyond the host screen's lifecycle.
  options.presentationScope = GADPictureInPictureAdPresentationScopeApplication;
  return options;
}

ज़्यादा जानकारी के लिए, विज्ञापन को सभी स्क्रीन पर दिखने की सुविधा चालू रखना लेख पढ़ें.

विज्ञापन इवेंट कॉलबैक सेट करना

पिक्चर में पिक्चर वाले विज्ञापन के लाइफ़साइकल इवेंट मैनेज करने के लिए, विज्ञापन दिखाने से पहले उस पर इवेंट कॉलबैक सेट करें. यह कॉलबैक, क्लिक और इंप्रेशन जैसे स्टैंडर्ड इवेंट की रिपोर्ट करता है. यह कॉलबैक, पिक्चर में पिक्चर मोड से जुड़े इवेंट की जानकारी भी देता है. जैसे, विज्ञापन कब दिखाया जाता है या कब छिपाया जाता है:

Swift

func pictureInPictureAdDidShow(_ pictureInPictureAd: PictureInPictureAd) {
  print("Picture-in-Picture ad shown.")
}

func pictureInPictureAdDidHide(_ pictureInPictureAd: PictureInPictureAd) {
  print("Picture-in-Picture ad hidden.")
}

func pictureInPictureAdDidFailToShow(
  _ pictureInPictureAd: PictureInPictureAd, error: Error
) {
  print("Picture-in-Picture ad failed to show: \(error.localizedDescription)")
}

func pictureInPictureAdDidRecordImpression(
  _ pictureInPictureAd: PictureInPictureAd
) {
  print("Picture-in-Picture ad recorded an impression.")
}

func pictureInPictureAdDidRecordClick(
  _ pictureInPictureAd: PictureInPictureAd
) {
  print("Picture-in-Picture ad recorded a click.")
}

func pictureInPictureAdWillPresentScreen(
  _ pictureInPictureAd: PictureInPictureAd
) {
  print("Picture-in-Picture ad will present screen.")
}

func pictureInPictureAdWillDismissScreen(
  _ pictureInPictureAd: PictureInPictureAd
) {
  print("Picture-in-Picture ad will dismiss screen.")
}

func pictureInPictureAdDidDismissScreen(
  _ pictureInPictureAd: PictureInPictureAd
) {
  print("Picture-in-Picture ad dismissed screen.")
}

Objective-C

- (void)pictureInPictureAdDidShow:(GADPictureInPictureAd *)pictureInPictureAd {
  NSLog(@"Picture-in-Picture ad shown.");
}

- (void)pictureInPictureAdDidHide:(GADPictureInPictureAd *)pictureInPictureAd {
  NSLog(@"Picture-in-Picture ad hidden.");
}

- (void)pictureInPictureAdDidFailToShow:(GADPictureInPictureAd *)pictureInPictureAd
                              withError:(NSError *)error {
  NSLog(@"Picture-in-Picture ad failed to show: %@", error);
}

- (void)pictureInPictureAdDidRecordImpression:(GADPictureInPictureAd *)pictureInPictureAd {
  NSLog(@"Picture-in-Picture ad recorded an impression.");
}

- (void)pictureInPictureAdDidRecordClick:(GADPictureInPictureAd *)pictureInPictureAd {
  NSLog(@"Picture-in-Picture ad recorded a click.");
}

- (void)pictureInPictureAdWillPresentScreen:(GADPictureInPictureAd *)pictureInPictureAd {
  NSLog(@"Picture-in-Picture ad will present screen.");
}

- (void)pictureInPictureAdWillDismissScreen:(GADPictureInPictureAd *)pictureInPictureAd {
  NSLog(@"Picture-in-Picture ad will dismiss screen.");
}

- (void)pictureInPictureAdDidDismissScreen:(GADPictureInPictureAd *)pictureInPictureAd {
  NSLog(@"Picture-in-Picture ad dismissed screen.");
}

विज्ञापन छिपाना

स्क्रीन से फ़्लोटिंग विज्ञापन हटाने के लिए, hide तरीके को कॉल करें. यह तरीका, विज्ञापन छिपाए जाने के इवेंट के कॉलबैक को शुरू करता है:

Swift

private func hidePictureInPictureAd() {
  // Use the loaded PictureInPictureAd instance.
  guard let pipAd else {
    print("No ad to hide.")
    return
  }
  pipAd.hide()
}

Objective-C

- (void)hidePictureInPictureAd {
  // Use the loaded PictureInPictureAd instance.
  if (!self.pipAd) {
    NSLog(@"No ad to hide.");
    return;
  }
  [self.pipAd hide];
}

विज्ञापन ऐसेट मिटाना

मेमोरी लीक से बचने के लिए, जब आपका ऐप्लिकेशन विज्ञापन का इस्तेमाल करना बंद कर दे, तब विज्ञापन ऑब्जेक्ट का रेफ़रंस हटा दें. उदाहरण के लिए, जब आपका ऐप्लिकेशन विज्ञापन को फिर से नहीं दिखाता या उससे इंटरैक्ट नहीं करता. स्क्रीन के दायरे वाले विज्ञापनों के लिए, जब आपका ऐप्लिकेशन होस्ट स्क्रीन को मेमोरी से हटाता है, तब रेफ़रंस छोड़ें. ऐप्लिकेशन के स्कोप वाले विज्ञापनों के लिए, उपयोगकर्ता के एक स्क्रीन से दूसरी स्क्रीन पर जाने के दौरान विज्ञापन का रेफ़रंस बनाए रखें. साथ ही, जब उपयोगकर्ता विज्ञापन को खारिज कर दे, तब रेफ़रंस हटा दें:

Swift

private func cleanUpPictureInPictureAd() {
  pipAd?.hide()
  pipAd = nil
}

Objective-C

- (void)cleanUpPictureInPictureAd {
  [self.pipAd hide];
  self.pipAd = nil;
}

विज्ञापन को सभी स्क्रीन पर दिखना चाहिए

प्रज़ेंटेशन के स्कोप को ऐप्लिकेशन पर सेट करने पर, पिक्चर-इन-पिक्चर विज्ञापन तब भी दिखता रहता है, जब आपका ऐप्लिकेशन होस्टिंग स्क्रीन को मेमोरी से हटा देता है. जब उपयोगकर्ता होस्ट स्क्रीन से दूर चला जाता है, तब विज्ञापन से इंटरैक्ट करने या उसे खारिज करने के लिए, आपके ऐप्लिकेशन के पास पिक्चर-इन-पिक्चर वाले विज्ञापन को ऐक्सेस करने की अनुमति होनी चाहिए. हमारा सुझाव है कि विज्ञापन को किसी ऐप्लिकेशन-लेवल के सिंगलटन या शेयर किए गए स्टेट मैनेजर में रखें. इसे किसी एक स्क्रीन के इंस्टेंस वैरिएबल में न रखें.