Skip to content

Commit 3c1f39f

Browse files
nventimiglimaddevrelgithubbot
authored andcommitted
Added Ad Choices Icon to Swift Custom Native Samples.
PiperOrigin-RevId: 634033693
1 parent 5a2dfc6 commit 3c1f39f

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

Swift/admanager/AdManagerCustomRenderingExample/AdManagerCustomRenderingExample/Base.lproj/SimpleCustomNativeAdView.xib

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" colorMatched="YES">
3-
<device id="retina4_7" orientation="portrait">
4-
<adaptation id="fullscreen"/>
5-
</device>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="22504" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" colorMatched="YES">
3+
<device id="retina4_7" orientation="portrait" appearance="light"/>
64
<dependencies>
75
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
6+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22504"/>
97
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
108
</dependencies>
119
<objects>
@@ -15,10 +13,14 @@
1513
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
1614
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1715
<subviews>
16+
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="LKB-aH-scG">
17+
<rect key="frame" x="360" y="652" width="15" height="15"/>
18+
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" flexibleMinY="YES" heightSizable="YES"/>
19+
</imageView>
1820
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" placeholderIntrinsicWidth="375" placeholderIntrinsicHeight="24" text="Headline" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="EJ7-Y2-nrO">
1921
<rect key="frame" x="5" y="5" width="365" height="24"/>
2022
<fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
21-
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
23+
<color key="textColor" systemColor="darkTextColor"/>
2224
<nil key="highlightedColor"/>
2325
</label>
2426
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="hoj-tc-K51">
@@ -32,7 +34,7 @@
3234
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Caption" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="2" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Opb-BT-xiP">
3335
<rect key="frame" x="5" y="224" width="365" height="17"/>
3436
<fontDescription key="fontDescription" type="system" pointSize="14"/>
35-
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
37+
<color key="textColor" systemColor="darkTextColor"/>
3638
<nil key="highlightedColor"/>
3739
</label>
3840
</subviews>
@@ -48,11 +50,17 @@
4850
<constraint firstAttribute="trailing" secondItem="Opb-BT-xiP" secondAttribute="trailing" constant="5" id="WYZ-v6-Hph"/>
4951
</constraints>
5052
<connections>
53+
<outlet property="adChoicesView" destination="LKB-aH-scG" id="LGX-TD-3Op"/>
5154
<outlet property="captionView" destination="Opb-BT-xiP" id="HdX-mv-amk"/>
5255
<outlet property="headlineView" destination="EJ7-Y2-nrO" id="JLY-1X-hqV"/>
5356
<outlet property="mainPlaceholder" destination="hoj-tc-K51" id="9Sv-RO-bpk"/>
5457
</connections>
55-
<point key="canvasLocation" x="20.800000000000001" y="24.287856071964018"/>
58+
<point key="canvasLocation" x="20" y="23.838080959520241"/>
5659
</view>
5760
</objects>
61+
<resources>
62+
<systemColor name="darkTextColor">
63+
<color white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
64+
</systemColor>
65+
</resources>
5866
</document>

Swift/admanager/AdManagerCustomRenderingExample/AdManagerCustomRenderingExample/MySimpleNativeAdView.swift

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@ import UIKit
2121
enum MySimpleNativeAdViewTypeProperties {
2222

2323
/// The headline asset key.
24-
static let MySimpleNativeAdViewHeadlineKey = "Headline"
24+
static let mySimpleNativeAdViewHeadlineKey = "Headline"
2525

2626
/// The main image asset key.
27-
static let MySimpleNativeAdViewMainImageKey = "MainImage"
27+
static let mySimpleNativeAdViewMainImageKey = "MainImage"
2828

2929
/// The caption asset key.
30-
static let MySimpleNativeAdViewCaptionKey = "Caption"
30+
static let mySimpleNativeAdViewCaptionKey = "Caption"
3131
}
3232

3333
/// Custom native ad view class with format ID 10063170.
3434
class MySimpleNativeAdView: UIView {
3535

3636
/// Weak references to this ad's asset views.
3737
@IBOutlet weak var headlineView: UILabel!
38-
3938
@IBOutlet weak var mainPlaceholder: UIView!
4039
@IBOutlet weak var captionView: UILabel!
40+
@IBOutlet weak var adChoicesView: UIImageView!
4141

4242
/// The custom native ad that populated this view.
4343
var customNativeAd: GADCustomNativeAd!
@@ -49,18 +49,37 @@ class MySimpleNativeAdView: UIView {
4949
mainPlaceholder.addGestureRecognizer(
5050
UITapGestureRecognizer(
5151
target: self,
52-
action: #selector(MySimpleNativeAdView.performClickOnMainImage(_:))))
52+
action: #selector(performClickOnMainImage(_:))))
5353
mainPlaceholder.isUserInteractionEnabled = true
54+
55+
// Enable clicks on AdChoices.
56+
adChoicesView.addGestureRecognizer(
57+
UITapGestureRecognizer(
58+
target: self,
59+
action: #selector(performClickOnAdChoices(_:))))
60+
adChoicesView.isUserInteractionEnabled = true
61+
}
62+
63+
@objc func performClickOnMainImage(_ sender: UITapGestureRecognizer!) {
64+
customNativeAd.performClickOnAsset(
65+
withKey: MySimpleNativeAdViewTypeProperties.mySimpleNativeAdViewMainImageKey)
5466
}
5567

56-
@objc func performClickOnMainImage(_ sender: UIImage!) {
68+
@objc func performClickOnAdChoices(_ sender: UITapGestureRecognizer!) {
5769
customNativeAd.performClickOnAsset(
58-
withKey: MySimpleNativeAdViewTypeProperties.MySimpleNativeAdViewMainImageKey)
70+
withKey: GADNativeAssetIdentifier.adChoicesViewAsset.rawValue)
5971
}
6072

6173
/// Populates the ad view with the custom native ad object.
6274
func populate(withCustomNativeAd customNativeAd: GADCustomNativeAd) {
6375
self.customNativeAd = customNativeAd
76+
77+
// Render the AdChoices image.
78+
let adChoicesKey = GADNativeAssetIdentifier.adChoicesViewAsset.rawValue
79+
let adChoicesImage = customNativeAd.image(forKey: adChoicesKey)?.image
80+
adChoicesView.image = adChoicesImage
81+
adChoicesView.isHidden = adChoicesImage == nil
82+
6483
// The custom click handler closure overrides the normal click action defined by the ad.
6584
customNativeAd.customClickHandler = { assetID in
6685
let alert = UIAlertController(
@@ -77,9 +96,9 @@ class MySimpleNativeAdView: UIView {
7796
}
7897

7998
// Populate the custom native ad assets.
80-
let headlineKey = MySimpleNativeAdViewTypeProperties.MySimpleNativeAdViewHeadlineKey
99+
let headlineKey = MySimpleNativeAdViewTypeProperties.mySimpleNativeAdViewHeadlineKey
81100
headlineView.text = customNativeAd.string(forKey: headlineKey)
82-
let captionKey = MySimpleNativeAdViewTypeProperties.MySimpleNativeAdViewCaptionKey
101+
let captionKey = MySimpleNativeAdViewTypeProperties.mySimpleNativeAdViewCaptionKey
83102
captionView.text = customNativeAd.string(forKey: captionKey)
84103

85104
let mainView: UIView = self.mainView(forCustomNativeAd: customNativeAd)
@@ -94,7 +113,7 @@ class MySimpleNativeAdView: UIView {
94113
mediaView.mediaContent = customNativeAd.mediaContent
95114
return mediaView
96115
} else {
97-
let imageKey = MySimpleNativeAdViewTypeProperties.MySimpleNativeAdViewMainImageKey
116+
let imageKey = MySimpleNativeAdViewTypeProperties.mySimpleNativeAdViewMainImageKey
98117
let image: UIImage? = customNativeAd.image(forKey: imageKey)?.image
99118
return UIImageView(image: image)
100119
}
@@ -118,5 +137,4 @@ class MySimpleNativeAdView: UIView {
118137
withVisualFormat: "V:|[mainView]|", options: [], metrics: nil,
119138
views: viewDictionary as? [String: Any] ?? [String: Any]()))
120139
}
121-
122140
}

Swift/admanager/AdManagerCustomRenderingExample/AdManagerCustomRenderingExample/ViewController.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class ViewController: UIViewController {
5757
let adUnitID = "/6499/example/native"
5858

5959
/// The native custom format id
60-
let nativeCustomFormatId = "10104090"
60+
let nativeCustomFormatId = "10063170"
6161

6262
@IBAction func privacySettingsTapped(_ sender: UIBarButtonItem) {
6363
GoogleMobileAdsConsentManager.shared.presentPrivacyOptionsForm(from: self) {
@@ -115,7 +115,7 @@ class ViewController: UIViewController {
115115
}
116116

117117
func setAdView(_ view: UIView) {
118-
nativeAdView.removeFromSuperview()
118+
nativeAdView?.removeFromSuperview()
119119
nativeAdView = view
120120
nativeAdPlaceholder.addSubview(nativeAdView)
121121
nativeAdView.translatesAutoresizingMaskIntoConstraints = false
@@ -242,7 +242,8 @@ extension ViewController: GADNativeAdLoaderDelegate {
242242
// Some native ads will include a video asset, while others do not. Apps can use the
243243
// GADVideoController's hasVideoContent property to determine if one is present, and adjust their
244244
// UI accordingly.
245-
let hasVideoContent = nativeAd.mediaContent.hasVideoContent // Update the ViewController for video content.
245+
// Update the ViewController for video content.
246+
let hasVideoContent = nativeAd.mediaContent.hasVideoContent
246247
updateVideoStatusLabel(hasVideoContent: hasVideoContent)
247248
if hasVideoContent {
248249
// By acting as the delegate to the GADVideoController, this ViewController receives messages

0 commit comments

Comments
 (0)