Skip to content

Commit

Permalink
Fixed Swift APIDemo custom playback controls issue.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 689094925
  • Loading branch information
nventimigli authored and copybara-github committed Oct 23, 2024
1 parent 21db421 commit fbaffc2
Show file tree
Hide file tree
Showing 16 changed files with 274 additions and 165 deletions.
4 changes: 4 additions & 0 deletions Swift/advanced/APIDemo/APIDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

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

/* Begin PBXFileReference section */
15CB61211C7D0256000212DE /* APIDemo-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "APIDemo-Bridging-Header.h"; sourceTree = "<group>"; };
1CD352672C9CC97200534FCC /* CustomControls.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = CustomControls.xib; sourceTree = "<group>"; };
4A7A6CD61C76237500FB1A32 /* Constants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
4AA7D68D1C625A1200DFD2EB /* APIDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = APIDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
4AA7D6901C625A1200DFD2EB /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -125,6 +127,7 @@
AED11115203213DF00EA4BEE /* AdManagerCustomVideoControls */ = {
isa = PBXGroup;
children = (
1CD352672C9CC97200534FCC /* CustomControls.xib */,
507818F1219A417F00E5A44A /* AdManagerCustomVideoControlsController.swift */,
AED11117203213F300EA4BEE /* SimpleNativeAdView.xib */,
AED11118203213F500EA4BEE /* UnifiedNativeAdView.xib */,
Expand Down Expand Up @@ -205,6 +208,7 @@
4AA7D69B1C625A1200DFD2EB /* LaunchScreen.storyboard in Resources */,
4AA7D6981C625A1200DFD2EB /* Assets.xcassets in Resources */,
4AA7D6961C625A1200DFD2EB /* Main.storyboard in Resources */,
1CD352682C9CC97200534FCC /* CustomControls.xib in Resources */,
AED11120203213FC00EA4BEE /* UnifiedNativeAdView.xib in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
import GoogleMobileAds
import UIKit

private let testAdUnit = "/21775744923/example/native-video"
private let testNativeCustomFormatID = "12406343"
private let testAdUnit = "/6499/example/native-video"
private let testNativeCustomFormatID = "10104090"

class AdManagerCustomVideoControlsController: UIViewController {

/// Switch to indicate if video ads should start muted.
@IBOutlet weak var startMutedSwitch: UISwitch!
/// Switch to indicate if video ads should request custom controls.
Expand All @@ -33,8 +34,6 @@ class AdManagerCustomVideoControlsController: UIViewController {
@IBOutlet weak var unifiedNativeAdSwitch: UISwitch!
/// Switch to custom native ads.
@IBOutlet weak var customNativeAdSwitch: UISwitch!
/// View containing information about video and custom controls.
@IBOutlet weak var customControlsView: CustomControlsView!
/// Refresh the native ad.
@IBOutlet weak var refreshButton: UIButton!
/// The Google Mobile Ads SDK version number label.
Expand All @@ -47,9 +46,10 @@ class AdManagerCustomVideoControlsController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
versionLabel.text = "\(GADMobileAds.sharedInstance().versionNumber)"
versionLabel.text = GADGetStringFromVersionNumber(GADMobileAds.sharedInstance().versionNumber)
refreshAd(nil)
}

@IBAction func refreshAd(_ sender: Any?) {
// Loads an ad for any of unified native or custom native ads.
var adTypes = [GADAdLoaderAdType]()
Expand All @@ -69,10 +69,10 @@ class AdManagerCustomVideoControlsController: UIViewController {
refreshButton.isEnabled = false
adLoader = GADAdLoader(
adUnitID: testAdUnit, rootViewController: self, adTypes: adTypes, options: [videoOptions])
customControlsView.reset(withStartMuted: videoOptions.startMuted)
adLoader?.delegate = self
adLoader?.load(GAMRequest())
}

func setAdView(_ view: UIView) {
// Remove previous ad view.
nativeAdView?.removeFromSuperview()
Expand Down Expand Up @@ -147,8 +147,6 @@ extension AdManagerCustomVideoControlsController: GADNativeAdLoaderDelegate {
heightConstraint.isActive = true
}

customControlsView.mediaContent = nativeAd.mediaContent

// These assets are not guaranteed to be present. Check that they are before
// showing or hiding them.
(nativeAdView.bodyView as? UILabel)?.text = nativeAd.body
Expand Down Expand Up @@ -180,7 +178,30 @@ extension AdManagerCustomVideoControlsController: GADNativeAdLoaderDelegate {
// required to make the ad clickable.
// Note: this should always be done after populating the ad views.
nativeAdView.nativeAd = nativeAd

// [START set_custom_video_controls]
// Add custom video controls to replace default video controls.
if nativeAd.mediaContent.hasVideoContent,
nativeAd.mediaContent.videoController.customControlsEnabled(),
let mediaView = nativeAdView.mediaView,
let customControlsView = Bundle.main.loadNibNamed(
"CustomControls", owner: nil, options: nil
)?.first as? CustomControlsView
{

customControlsView.mediaContent = mediaView.mediaContent
customControlsView.isMuted = startMutedSwitch.isOn
mediaView.addSubview(customControlsView)

NSLayoutConstraint.activate([
customControlsView.leadingAnchor.constraint(equalTo: mediaView.leadingAnchor),
customControlsView.bottomAnchor.constraint(equalTo: mediaView.bottomAnchor),
])

mediaView.bringSubviewToFront(customControlsView)
}
}
// [END set_custom_video_controls]
}

extension AdManagerCustomVideoControlsController: GADCustomNativeAdLoaderDelegate {
Expand All @@ -196,8 +217,8 @@ extension AdManagerCustomVideoControlsController: GADCustomNativeAdLoaderDelegat
as! SimpleNativeAdView
setAdView(simpleNativeAdView)
// Populate the custom native ad view with its assets.
simpleNativeAdView.populate(withCustomNativeAd: customNativeAd)
customControlsView.mediaContent = customNativeAd.mediaContent
simpleNativeAdView.populate(
withCustomNativeAd: customNativeAd, startMuted: self.startMutedSwitch.isOn)
}

func customNativeAdFormatIDs(for adLoader: GADAdLoader) -> [String] {
Expand Down
6 changes: 3 additions & 3 deletions Swift/advanced/APIDemo/APIDemo/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
"author" : "xcode",
"version" : 1
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "video_mute.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "video_pause.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "video_play.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "video_unmute.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit fbaffc2

Please sign in to comment.