Gli annunci Picture in picture (PIP) vengono visualizzati in una finestra mobile che rimane sopra i contenuti sullo schermo, come articoli, feed o gameplay. Questo formato consente agli utenti di interagire con la tua app mentre l'annuncio rimane visibile. Scegli questo formato per mostrare annunci display che non occupano l'intero schermo. Scopri di più sugli annunci Picture in picture.
Questa guida spiega come richiedere e visualizzare gli annunci Picture in picture nella tua app utilizzando Google Mobile Ads SDK.
Prima di iniziare
Prima di continuare, completa queste operazioni:
Installa Google Mobile Ads SDK versione
13.10.0o successive.
Attiva gli annunci di prova e utilizza il seguente ID unità pubblicitaria di test:
ca-app-pub-3940256099942544/8810945611
Carica un annuncio
Per caricare un
GADPictureInPictureAd
oggetto, crea una richiesta di annuncio e chiama il metodo di caricamento:
Swift
Sostituisci adUnitID con l'ID unità pubblicitaria.
Objective-C
Sostituisci kAdUnitID con l'ID unità pubblicitaria.
Mostra l'annuncio
Per visualizzare l'annuncio Picture in picture sullo schermo, chiama il metodo show. L'esempio seguente mostra l'annuncio nella posizione predefinita e imposta l'ambito di presentazione sullo schermo:
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]; }
Imposta la posizione
Per impostazione predefinita, Google Mobile Ads SDK mostra un annuncio Picture in picture nell'angolo in basso a destra dello schermo alla prima visualizzazione o nell'ultima posizione nota se è stato mostrato in precedenza. Per personalizzare la posizione dell'annuncio, specifica una posizione quando lo mostri. L'esempio seguente imposta la posizione sopra i contenuti nell'angolo in alto a sinistra dello schermo:
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; }
Per tutte le posizioni disponibili, consulta
GADPictureInPictureAdPosition.
Imposta l'ambito di presentazione
Per impostazione predefinita, Google Mobile Ads SDK associa un annuncio Picture in picture alla schermata host corrente. Google Mobile Ads SDK ignora l'annuncio quando la gerarchia di oggetti View della schermata host non è più in memoria. Per mantenere l'annuncio visibile dopo che la schermata host è stata rimossa dalla memoria, imposta l'ambito di presentazione sull'applicazione:
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; }
Per ulteriori informazioni, consulta Mantenere l'annuncio visibile su più schermi.
Ascolta gli eventi degli annunci
Per gestire gli eventi del ciclo di vita degli annunci Picture in picture, registrati per gli eventi degli annunci prima di visualizzare l'annuncio. Questi report Eventi segnalano le interazioni con l'annuncio, come clic e impressioni. Questi eventi segnalano anche eventi specifici di Picture in picture, ad esempio quando l'annuncio viene mostrato o nascosto:
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."); }
Nascondi l'annuncio
Per rimuovere l'annuncio floating dallo schermo, chiama il metodo hide. Questo metodo richiama l'evento di annuncio nascosto:
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]; }
Esegui la pulizia delle risorse degli annunci
Per evitare perdite di memoria, elimina il riferimento all'oggetto annuncio quando l'app termina di utilizzare l'annuncio. Ad esempio, quando l'app non visualizza o interagisce più con l'annuncio. Per gli annunci con ambito schermo, elimina il riferimento quando l'app rimuove la schermata host dalla memoria. Per gli annunci con ambito app, mantieni il riferimento all'annuncio mentre l'utente naviga tra gli schermi ed eliminalo quando l'utente ignora l'annuncio:
Swift
private func cleanUpPictureInPictureAd() { pipAd?.hide() pipAd = nil }
Objective-C
- (void)cleanUpPictureInPictureAd { [self.pipAd hide]; self.pipAd = nil; }
Mantenere l'annuncio visibile su più schermi
Quando imposti l'ambito di presentazione sull'applicazione, l'annuncio Picture in picture rimane visibile anche quando l'app rimuove la schermata di hosting dalla memoria. Per interagire con l'annuncio o ignorarlo quando l'utente esce dalla schermata host, l'app deve mantenere l'accesso all'annuncio Picture in picture. Ti consigliamo di conservare l'annuncio in un singleton a livello di app o in un gestore di stato condiviso anziché nella variabile di istanza di una singola schermata.
Per esempi su come mantenere un annuncio visibile su più schermi, consulta le seguenti app di esempio: