Skip to content

Commit 5040f77

Browse files
nventimiglicopybara-github
authored andcommitted
Fixed Swift APIDemo custom playback controls issue.
PiperOrigin-RevId: 676514146
1 parent 308e8a3 commit 5040f77

File tree

16 files changed

+259
-162
lines changed

16 files changed

+259
-162
lines changed

Swift/advanced/APIDemo/APIDemo.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
1CD352682C9CC97200534FCC /* CustomControls.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1CD352672C9CC97200534FCC /* CustomControls.xib */; };
1011
4A7A6CD71C76237500FB1A32 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A7A6CD61C76237500FB1A32 /* Constants.swift */; };
1112
4AA7D6911C625A1200DFD2EB /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AA7D6901C625A1200DFD2EB /* AppDelegate.swift */; };
1213
4AA7D6961C625A1200DFD2EB /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4AA7D6941C625A1200DFD2EB /* Main.storyboard */; };
@@ -33,6 +34,7 @@
3334

3435
/* Begin PBXFileReference section */
3536
15CB61211C7D0256000212DE /* APIDemo-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "APIDemo-Bridging-Header.h"; sourceTree = "<group>"; };
37+
1CD352672C9CC97200534FCC /* CustomControls.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = CustomControls.xib; sourceTree = "<group>"; };
3638
4A7A6CD61C76237500FB1A32 /* Constants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
3739
4AA7D68D1C625A1200DFD2EB /* APIDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = APIDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
3840
4AA7D6901C625A1200DFD2EB /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@@ -125,6 +127,7 @@
125127
AED11115203213DF00EA4BEE /* AdManagerCustomVideoControls */ = {
126128
isa = PBXGroup;
127129
children = (
130+
1CD352672C9CC97200534FCC /* CustomControls.xib */,
128131
507818F1219A417F00E5A44A /* AdManagerCustomVideoControlsController.swift */,
129132
AED11117203213F300EA4BEE /* SimpleNativeAdView.xib */,
130133
AED11118203213F500EA4BEE /* UnifiedNativeAdView.xib */,
@@ -205,6 +208,7 @@
205208
4AA7D69B1C625A1200DFD2EB /* LaunchScreen.storyboard in Resources */,
206209
4AA7D6981C625A1200DFD2EB /* Assets.xcassets in Resources */,
207210
4AA7D6961C625A1200DFD2EB /* Main.storyboard in Resources */,
211+
1CD352682C9CC97200534FCC /* CustomControls.xib in Resources */,
208212
AED11120203213FC00EA4BEE /* UnifiedNativeAdView.xib in Resources */,
209213
);
210214
runOnlyForDeploymentPostprocessing = 0;

Swift/advanced/APIDemo/APIDemo/AdManagerCustomVideoControlsController.swift

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import GoogleMobileAds
2020
import UIKit
2121

22-
private let testAdUnit = "/21775744923/example/native-video"
23-
private let testNativeCustomFormatID = "12406343"
22+
private let testAdUnit = "/6499/example/native-video"
23+
private let testNativeCustomFormatID = "10104090"
2424

2525
class AdManagerCustomVideoControlsController: UIViewController {
2626
/// Switch to indicate if video ads should start muted.
@@ -33,8 +33,6 @@ class AdManagerCustomVideoControlsController: UIViewController {
3333
@IBOutlet weak var unifiedNativeAdSwitch: UISwitch!
3434
/// Switch to custom native ads.
3535
@IBOutlet weak var customNativeAdSwitch: UISwitch!
36-
/// View containing information about video and custom controls.
37-
@IBOutlet weak var customControlsView: CustomControlsView!
3836
/// Refresh the native ad.
3937
@IBOutlet weak var refreshButton: UIButton!
4038
/// The Google Mobile Ads SDK version number label.
@@ -47,7 +45,7 @@ class AdManagerCustomVideoControlsController: UIViewController {
4745

4846
override func viewDidLoad() {
4947
super.viewDidLoad()
50-
versionLabel.text = "\(GADMobileAds.sharedInstance().versionNumber)"
48+
versionLabel.text = GADGetStringFromVersionNumber(GADMobileAds.sharedInstance().versionNumber)
5149
refreshAd(nil)
5250
}
5351
@IBAction func refreshAd(_ sender: Any?) {
@@ -69,7 +67,6 @@ class AdManagerCustomVideoControlsController: UIViewController {
6967
refreshButton.isEnabled = false
7068
adLoader = GADAdLoader(
7169
adUnitID: testAdUnit, rootViewController: self, adTypes: adTypes, options: [videoOptions])
72-
customControlsView.reset(withStartMuted: videoOptions.startMuted)
7370
adLoader?.delegate = self
7471
adLoader?.load(GAMRequest())
7572
}
@@ -147,8 +144,6 @@ extension AdManagerCustomVideoControlsController: GADNativeAdLoaderDelegate {
147144
heightConstraint.isActive = true
148145
}
149146

150-
customControlsView.mediaContent = nativeAd.mediaContent
151-
152147
// These assets are not guaranteed to be present. Check that they are before
153148
// showing or hiding them.
154149
(nativeAdView.bodyView as? UILabel)?.text = nativeAd.body
@@ -180,6 +175,26 @@ extension AdManagerCustomVideoControlsController: GADNativeAdLoaderDelegate {
180175
// required to make the ad clickable.
181176
// Note: this should always be done after populating the ad views.
182177
nativeAdView.nativeAd = nativeAd
178+
179+
// [START set_custom_video_controls]
180+
// Add custom video controls to replace default video controls.
181+
if nativeAd.mediaContent.hasVideoContent {
182+
if let mediaView = nativeAdView.mediaView {
183+
if let customControlsView = Bundle.main.loadNibNamed(
184+
"CustomControls", owner: nil, options: nil)?.first as? CustomControlsView
185+
{
186+
customControlsView.mediaContent = mediaView.mediaContent
187+
customControlsView.isMuted = startMutedSwitch.isOn
188+
mediaView.addSubview(customControlsView)
189+
NSLayoutConstraint.activate([
190+
customControlsView.leadingAnchor.constraint(equalTo: mediaView.leadingAnchor),
191+
customControlsView.bottomAnchor.constraint(equalTo: mediaView.bottomAnchor),
192+
])
193+
mediaView.bringSubviewToFront(customControlsView)
194+
}
195+
}
196+
}
197+
// [END set_custom_video_controls]
183198
}
184199
}
185200

@@ -196,8 +211,8 @@ extension AdManagerCustomVideoControlsController: GADCustomNativeAdLoaderDelegat
196211
as! SimpleNativeAdView
197212
setAdView(simpleNativeAdView)
198213
// Populate the custom native ad view with its assets.
199-
simpleNativeAdView.populate(withCustomNativeAd: customNativeAd)
200-
customControlsView.mediaContent = customNativeAd.mediaContent
214+
simpleNativeAdView.populate(
215+
withCustomNativeAd: customNativeAd, startMuted: self.startMutedSwitch.isOn)
201216
}
202217

203218
func customNativeAdFormatIDs(for adLoader: GADAdLoader) -> [String] {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"info" : {
3-
"version" : 1,
4-
"author" : "xcode"
3+
"author" : "xcode",
4+
"version" : 1
55
}
6-
}
6+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "video_mute.png",
5+
"idiom" : "universal",
6+
"scale" : "1x"
7+
},
8+
{
9+
"idiom" : "universal",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"author" : "xcode",
19+
"version" : 1
20+
}
21+
}
Loading
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "video_pause.png",
5+
"idiom" : "universal",
6+
"scale" : "1x"
7+
},
8+
{
9+
"idiom" : "universal",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"author" : "xcode",
19+
"version" : 1
20+
}
21+
}
Loading
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "video_play.png",
5+
"idiom" : "universal",
6+
"scale" : "1x"
7+
},
8+
{
9+
"idiom" : "universal",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"author" : "xcode",
19+
"version" : 1
20+
}
21+
}
Loading
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "video_unmute.png",
5+
"idiom" : "universal",
6+
"scale" : "1x"
7+
},
8+
{
9+
"idiom" : "universal",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"author" : "xcode",
19+
"version" : 1
20+
}
21+
}

0 commit comments

Comments
 (0)