@@ -21,23 +21,23 @@ import UIKit
21
21
enum MySimpleNativeAdViewTypeProperties {
22
22
23
23
/// The headline asset key.
24
- static let MySimpleNativeAdViewHeadlineKey = " Headline "
24
+ static let mySimpleNativeAdViewHeadlineKey = " Headline "
25
25
26
26
/// The main image asset key.
27
- static let MySimpleNativeAdViewMainImageKey = " MainImage "
27
+ static let mySimpleNativeAdViewMainImageKey = " MainImage "
28
28
29
29
/// The caption asset key.
30
- static let MySimpleNativeAdViewCaptionKey = " Caption "
30
+ static let mySimpleNativeAdViewCaptionKey = " Caption "
31
31
}
32
32
33
33
/// Custom native ad view class with format ID 10063170.
34
34
class MySimpleNativeAdView : UIView {
35
35
36
36
/// Weak references to this ad's asset views.
37
37
@IBOutlet weak var headlineView : UILabel !
38
-
39
38
@IBOutlet weak var mainPlaceholder : UIView !
40
39
@IBOutlet weak var captionView : UILabel !
40
+ @IBOutlet weak var adChoicesView : UIImageView !
41
41
42
42
/// The custom native ad that populated this view.
43
43
var customNativeAd : GADCustomNativeAd !
@@ -49,18 +49,37 @@ class MySimpleNativeAdView: UIView {
49
49
mainPlaceholder. addGestureRecognizer (
50
50
UITapGestureRecognizer (
51
51
target: self ,
52
- action: #selector( MySimpleNativeAdView . performClickOnMainImage ( _: ) ) ) )
52
+ action: #selector( performClickOnMainImage ( _: ) ) ) )
53
53
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)
54
66
}
55
67
56
- @objc func performClickOnMainImage ( _ sender: UIImage ! ) {
68
+ @objc func performClickOnAdChoices ( _ sender: UITapGestureRecognizer ! ) {
57
69
customNativeAd. performClickOnAsset (
58
- withKey: MySimpleNativeAdViewTypeProperties . MySimpleNativeAdViewMainImageKey )
70
+ withKey: GADNativeAssetIdentifier . adChoicesViewAsset . rawValue )
59
71
}
60
72
61
73
/// Populates the ad view with the custom native ad object.
62
74
func populate( withCustomNativeAd customNativeAd: GADCustomNativeAd ) {
63
75
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
+
64
83
// The custom click handler closure overrides the normal click action defined by the ad.
65
84
customNativeAd. customClickHandler = { assetID in
66
85
let alert = UIAlertController (
@@ -77,9 +96,9 @@ class MySimpleNativeAdView: UIView {
77
96
}
78
97
79
98
// Populate the custom native ad assets.
80
- let headlineKey = MySimpleNativeAdViewTypeProperties . MySimpleNativeAdViewHeadlineKey
99
+ let headlineKey = MySimpleNativeAdViewTypeProperties . mySimpleNativeAdViewHeadlineKey
81
100
headlineView. text = customNativeAd. string ( forKey: headlineKey)
82
- let captionKey = MySimpleNativeAdViewTypeProperties . MySimpleNativeAdViewCaptionKey
101
+ let captionKey = MySimpleNativeAdViewTypeProperties . mySimpleNativeAdViewCaptionKey
83
102
captionView. text = customNativeAd. string ( forKey: captionKey)
84
103
85
104
let mainView : UIView = self . mainView ( forCustomNativeAd: customNativeAd)
@@ -94,7 +113,7 @@ class MySimpleNativeAdView: UIView {
94
113
mediaView. mediaContent = customNativeAd. mediaContent
95
114
return mediaView
96
115
} else {
97
- let imageKey = MySimpleNativeAdViewTypeProperties . MySimpleNativeAdViewMainImageKey
116
+ let imageKey = MySimpleNativeAdViewTypeProperties . mySimpleNativeAdViewMainImageKey
98
117
let image : UIImage ? = customNativeAd. image ( forKey: imageKey) ? . image
99
118
return UIImageView ( image: image)
100
119
}
@@ -118,5 +137,4 @@ class MySimpleNativeAdView: UIView {
118
137
withVisualFormat: " V:|[mainView]| " , options: [ ] , metrics: nil ,
119
138
views: viewDictionary as? [ String : Any ] ?? [ String: Any] ( ) ) )
120
139
}
121
-
122
140
}
0 commit comments