Skip to content

Commit 03306d9

Browse files
authored
Merge pull request #3241 from lrusso/master
Fix: Not showing video ads when the PIP mode is enabled on iOS
2 parents ef5c63f + 362f80c commit 03306d9

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

API.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,8 @@ Determine whether the media should played as picture in picture.
667667
* **false (default)** - Don't not play as picture in picture
668668
* **true** - Play the media as picture in picture
669669

670+
NOTE: Video ads cannot start when you are using the PIP on iOS (more info available at [Google IMA SDK Docs](https://developers.google.com/interactive-media-ads/docs/sdks/ios/client-side/picture_in_picture?hl=en#starting_ads)). If you are using custom controls, you must disable your PIP button when you receive the ```STARTED``` event from ```onReceiveAdEvent``` and show it again when you receive the ```ALL_ADS_COMPLETED``` event.
671+
670672
Platforms: iOS
671673

672674
#### playInBackground

ios/Video/Features/RCTIMAAdsManager.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import GoogleInteractiveMediaAds
55
class RCTIMAAdsManager: NSObject, IMAAdsLoaderDelegate, IMAAdsManagerDelegate {
66

77
private weak var _video: RCTVideo?
8+
private var _pipEnabled:() -> Bool
89

910
/* Entry point for the SDK. Used to make ad requests. */
1011
private var adsLoader: IMAAdsLoader!
1112
/* Main point of interaction with the SDK. Created by the SDK as the result of an ad request. */
1213
private var adsManager: IMAAdsManager!
1314

14-
init(video:RCTVideo!) {
15+
init(video:RCTVideo!, pipEnabled:@escaping () -> Bool) {
1516
_video = video
17+
_pipEnabled = pipEnabled
1618

1719
super.init()
1820
}
@@ -86,6 +88,9 @@ class RCTIMAAdsManager: NSObject, IMAAdsLoaderDelegate, IMAAdsManagerDelegate {
8688
}
8789
// Play each ad once it has been loaded
8890
if event.type == IMAAdEventType.LOADED {
91+
if (_pipEnabled()) {
92+
return
93+
}
8994
adsManager.start()
9095
}
9196

ios/Video/RCTVideo.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
6565
private var _filterName:String!
6666
private var _filterEnabled:Bool = false
6767
private var _presentingViewController:UIViewController?
68+
private var _pictureInPictureEnabled = false
6869

6970
/* IMA Ads */
7071
private var _adTagUrl:String?
@@ -120,10 +121,14 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
120121
onPictureInPictureStatusChanged?([ "isActive": NSNumber(value: false)])
121122
}
122123

124+
func isPipEnabled () -> Bool {
125+
return _pictureInPictureEnabled
126+
}
127+
123128
init(eventDispatcher:RCTEventDispatcher!) {
124129
super.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
125130
#if USE_GOOGLE_IMA
126-
_imaAdsManager = RCTIMAAdsManager(video: self)
131+
_imaAdsManager = RCTIMAAdsManager(video: self, pipEnabled: isPipEnabled)
127132
#endif
128133

129134
_eventDispatcher = eventDispatcher
@@ -168,7 +173,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
168173
required init?(coder aDecoder: NSCoder) {
169174
super.init(coder: aDecoder)
170175
#if USE_GOOGLE_IMA
171-
_imaAdsManager = RCTIMAAdsManager(video: self)
176+
_imaAdsManager = RCTIMAAdsManager(video: self, pipEnabled: isPipEnabled)
172177
#endif
173178
}
174179

@@ -459,6 +464,11 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
459464
try audioSession.setActive(true, options: [])
460465
} catch {
461466
}
467+
if (pictureInPicture) {
468+
_pictureInPictureEnabled = true
469+
} else {
470+
_pictureInPictureEnabled = false
471+
}
462472
_pip?.setPictureInPicture(pictureInPicture)
463473
#endif
464474
}

0 commit comments

Comments
 (0)