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.
Questa guida illustra come richiedere e visualizzare 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:
/21775744923/example/picture-in-picture
Carica un annuncio
Per caricare un oggetto GADPictureInPictureAd, 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. Il seguente esempio 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 quando viene visualizzato per la prima volta o nell'ultima posizione nota se è stato visualizzato in precedenza. Per personalizzare la posizione in cui viene visualizzato l'annuncio, specifica una posizione quando mostri l'annuncio. 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 della 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 saperne di più, consulta l'articolo Mantieni l'annuncio visibile su tutti gli schermi.
Ascolta gli eventi degli annunci
Per gestire gli eventi del ciclo di vita degli annunci Picture in picture, registrati agli eventi relativi agli annunci prima di visualizzare l'annuncio. Questi eventi registrano le interazioni con gli annunci, come i clic e le 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 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 pubblicitarie
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 mostra più l'annuncio o non interagisce più con questo. Per gli annunci con ambito schermata, elimina il riferimento quando l'app rimuove la schermata host dalla memoria. Per gli annunci con ambito app, conserva il riferimento all'annuncio mentre l'utente naviga tra le schermate ed eliminalo quando l'utente chiude l'annuncio:
Swift
private func cleanUpPictureInPictureAd() { pipAd?.hide() pipAd = nil }
Objective-C
- (void)cleanUpPictureInPictureAd { [self.pipAd hide]; self.pipAd = nil; }
Mantieni l'annuncio visibile su tutti gli schermi
Quando imposti l'ambito della presentazione sull'applicazione, l'annuncio picture in picture rimane visibile anche quando l'app rimuove dalla memoria la schermata di hosting. Per interagire con l'annuncio o chiuderlo 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 dello stato condiviso, anziché in una variabile di istanza di una singola schermata.
Per esempi su come mantenere un annuncio visibile su più schermate, consulta le seguenti app di esempio: