Skip to content

Commit 97bc63d

Browse files
committed
setup native bindable
1 parent 5bd8a28 commit 97bc63d

File tree

4 files changed

+59
-60
lines changed

4 files changed

+59
-60
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// AppLovinTeadsNativeAd.swift
3+
// TeadsAppLovinAdapter
4+
//
5+
// Created by Paul Nicolas on 14/03/2022.
6+
//
7+
8+
import AppLovinSDK
9+
10+
@objc final class AppLovinTeadsNativeAd: MANativeAd {
11+
weak var parentAdatper: TeadsMediationAdapter?
12+
13+
@objc init(parent: TeadsMediationAdapter, builderBlock: MANativeAdBuilderBlock) {
14+
super.init(format: .native, builderBlock: builderBlock)
15+
self.parentAdatper = parent
16+
}
17+
18+
@objc override func prepareView(forInteraction maxNativeAdView: MANativeAdView) {
19+
guard let teadsNativeAd = parentAdatper?.nativeAd else {
20+
parentAdatper?.e("Failed to register native ad views: native ad is nil.", becauseOf: nil)
21+
return
22+
}
23+
teadsNativeAd.register(containerView: maxNativeAdView)
24+
maxNativeAdView.titleLabel?.bind(component: teadsNativeAd.title)
25+
maxNativeAdView.bodyLabel?.bind(component: teadsNativeAd.content)
26+
maxNativeAdView.callToActionButton?.bind(component: teadsNativeAd.callToAction)
27+
maxNativeAdView.iconImageView?.bind(component: teadsNativeAd.icon)
28+
maxNativeAdView.advertiserLabel?.bind(component: teadsNativeAd.sponsored)
29+
30+
// mediaContentView and optionsContentView are already binded (subview)
31+
}
32+
}

MediationAdapters/TeadsAppLovinAdapter/TeadsMediationAdapter.swift

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import TeadsSDK
1212
@objc(TeadsMediationAdapter)
1313
final class TeadsMediationAdapter: ALMediationAdapter {
1414
var currentNativePlacement: TeadsNativeAdPlacement?
15-
weak var nativeAd: TeadsNativeAd?
15+
var nativeAd: TeadsNativeAd?
1616
weak var nativeDelegate: MANativeAdAdapterDelegate?
1717

1818
var currentInReadPlacement: TeadsInReadAdPlacement?
@@ -37,7 +37,9 @@ final class TeadsMediationAdapter: ALMediationAdapter {
3737
}
3838

3939
override func destroy() {
40+
currentNativePlacement = nil
4041

42+
currentInReadPlacement = nil
4143
}
4244
}
4345

@@ -100,21 +102,20 @@ extension TeadsMediationAdapter: TeadsNativeAdPlacementDelegate {
100102
if let title = ad.title?.text {
101103
builder.title = title
102104
}
103-
104-
builder.body = ad.content?.text
105105
builder.advertiser = ad.sponsored?.text
106+
builder.body = ad.content?.text
106107
builder.callToAction = ad.callToAction?.text
108+
109+
ad.icon?.loadImage { icon in
110+
builder.icon = MANativeAdImage(image: icon)
111+
}
112+
113+
// TODO: add public signatures on TeadsAdChoicesView
114+
//builder.optionsView = TeadsAdChoicesView(binding: ad.adChoices)
107115

108116
let mediaView = TeadsMediaView()
109117
mediaView.bind(component: mediaComponent)
110118
builder.mediaView = mediaView
111-
112-
ad.icon?.loadImage { icon in
113-
builder.icon = MANativeAdImage(image: icon)
114-
}
115-
if let adChoices = ad.adChoices {
116-
builder.optionsView = TeadsAdChoicesView(binding: adChoices)
117-
}
118119
}
119120

120121
nativeDelegate?.didLoadAd(for: nativeAd, withExtraInfo: nil)
@@ -161,27 +162,3 @@ extension TeadsMediationAdapter: TeadsAdDelegate {
161162
bannerDelegate?.didCollapseAdViewAd()
162163
}
163164
}
164-
165-
166-
final class AppLovinTeadsNativeAd: MANativeAd {
167-
weak var parentAdatper: TeadsMediationAdapter?
168-
169-
init(parent: TeadsMediationAdapter, builderBlock: MANativeAdBuilderBlock) {
170-
super.init(format: .native, builderBlock: builderBlock)
171-
self.parentAdatper = parent
172-
}
173-
174-
override func prepareView(forInteraction nativeAdView: MANativeAdView) {
175-
guard let nativeAd = parentAdatper?.nativeAd else {
176-
parentAdatper?.e("Failed to register native ad views: native ad is nil.", becauseOf: nil)
177-
return
178-
}
179-
nativeAd.register(containerView: nativeAdView)
180-
nativeAdView.titleLabel?.bind(component: nativeAd.title)
181-
nativeAdView.bodyLabel?.bind(component: nativeAd.content)
182-
nativeAdView.callToActionButton?.bind(component: nativeAd.callToAction)
183-
nativeAdView.iconImageView?.bind(component: nativeAd.icon)
184-
nativeAdView.mediaContentView?.bind(component: nativeAd.video ?? nativeAd.image)
185-
nativeAdView.advertiserLabel?.bind(component: nativeAd.adChoices)
186-
}
187-
}

TeadsSampleApp/Controllers/Native/AppLovin/TableView/AppLovinNativeAdView.xib

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
1616
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1717
<subviews>
18+
<view tag="5" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="KUL-GF-oLV" userLabel="AdChoices">
19+
<rect key="frame" x="335" y="20" width="20" height="20"/>
20+
<constraints>
21+
<constraint firstAttribute="width" constant="20" id="8vl-sv-ZXZ"/>
22+
<constraint firstAttribute="height" constant="20" id="VyO-uo-qwG"/>
23+
</constraints>
24+
</view>
1825
<imageView clipsSubviews="YES" userInteractionEnabled="NO" tag="4" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="Ddf-dS-jO7" userLabel="IconView">
1926
<rect key="frame" x="24" y="24" width="60" height="60"/>
2027
<constraints>
@@ -23,29 +30,23 @@
2330
</constraints>
2431
</imageView>
2532
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" alignment="top" translatesAutoresizingMaskIntoConstraints="NO" id="5q7-di-aL4">
26-
<rect key="frame" x="108" y="24" width="243" height="39.5"/>
33+
<rect key="frame" x="108" y="24" width="227" height="36.5"/>
2734
<subviews>
2835
<label opaque="NO" userInteractionEnabled="NO" tag="1" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Headline" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="2" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="LAQ-A6-bjb">
29-
<rect key="frame" x="0.0" y="0.0" width="243" height="19.5"/>
36+
<rect key="frame" x="0.0" y="0.0" width="227" height="19.5"/>
3037
<fontDescription key="fontDescription" type="boldSystem" pointSize="16"/>
3138
<nil key="textColor"/>
3239
<nil key="highlightedColor"/>
3340
</label>
3441
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillProportionally" alignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="Vjf-ZJ-Mib">
35-
<rect key="frame" x="0.0" y="19.5" width="243" height="20"/>
42+
<rect key="frame" x="0.0" y="19.5" width="66.5" height="17"/>
3643
<subviews>
3744
<label opaque="NO" userInteractionEnabled="NO" tag="2" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Advertiser" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Sbg-e0-Bds">
38-
<rect key="frame" x="0.0" y="1.5" width="66.5" height="17"/>
45+
<rect key="frame" x="0.0" y="0.0" width="66.5" height="17"/>
3946
<fontDescription key="fontDescription" type="system" pointSize="14"/>
4047
<nil key="textColor"/>
4148
<nil key="highlightedColor"/>
4249
</label>
43-
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="qWk-qa-nfZ">
44-
<rect key="frame" x="66.5" y="0.0" width="176.5" height="20"/>
45-
<constraints>
46-
<constraint firstAttribute="height" constant="20" id="f0N-pN-bJN"/>
47-
</constraints>
48-
</imageView>
4950
</subviews>
5051
</stackView>
5152
</subviews>
@@ -59,17 +60,7 @@
5960
<nil key="textColor"/>
6061
<nil key="highlightedColor"/>
6162
</label>
62-
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Ad" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="q25-37-4MT">
63-
<rect key="frame" x="0.0" y="0.0" width="15" height="15"/>
64-
<color key="backgroundColor" red="1" green="0.80000001190000003" blue="0.40000000600000002" alpha="1" colorSpace="calibratedRGB"/>
65-
<constraints>
66-
<constraint firstAttribute="height" relation="greaterThanOrEqual" constant="15" id="iy9-wp-Lrq"/>
67-
</constraints>
68-
<fontDescription key="fontDescription" type="system" weight="semibold" pointSize="11"/>
69-
<color key="textColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
70-
<nil key="highlightedColor"/>
71-
</label>
72-
<view tag="6" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="9dO-Pv-Qh0" userLabel="ContentView">
63+
<view tag="6" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="9dO-Pv-Qh0" userLabel="MediaView">
7364
<rect key="frame" x="24" y="100" width="327" height="200"/>
7465
<constraints>
7566
<constraint firstAttribute="height" constant="200" id="eHR-dH-zuo"/>
@@ -85,7 +76,6 @@
8576
<color key="backgroundColor" systemColor="systemGray6Color"/>
8677
<constraints>
8778
<constraint firstItem="MOU-oS-ZgE" firstAttribute="trailing" secondItem="naI-wO-42r" secondAttribute="trailing" constant="24" id="15R-1h-VGA"/>
88-
<constraint firstItem="q25-37-4MT" firstAttribute="top" secondItem="Yr2-r6-oZu" secondAttribute="top" id="31P-0d-5CA"/>
8979
<constraint firstItem="Ddf-dS-jO7" firstAttribute="top" secondItem="Yr2-r6-oZu" secondAttribute="top" constant="24" id="3TD-Fl-tUs"/>
9080
<constraint firstItem="IHI-TG-ptk" firstAttribute="leading" secondItem="MOU-oS-ZgE" secondAttribute="leading" constant="20" id="4ua-S6-zNr"/>
9181
<constraint firstItem="MOU-oS-ZgE" firstAttribute="trailing" secondItem="9dO-Pv-Qh0" secondAttribute="trailing" constant="24" id="5Fe-4G-wJj"/>
@@ -95,10 +85,10 @@
9585
<constraint firstItem="5q7-di-aL4" firstAttribute="leading" secondItem="Ddf-dS-jO7" secondAttribute="trailing" constant="24" id="FyT-2m-v00"/>
9686
<constraint firstItem="9dO-Pv-Qh0" firstAttribute="top" secondItem="Ddf-dS-jO7" secondAttribute="bottom" constant="16" id="Gu1-sh-iyp"/>
9787
<constraint firstItem="naI-wO-42r" firstAttribute="top" secondItem="IHI-TG-ptk" secondAttribute="bottom" constant="14" id="JTx-19-1ek"/>
88+
<constraint firstItem="KUL-GF-oLV" firstAttribute="top" secondItem="MOU-oS-ZgE" secondAttribute="top" constant="20" id="Zb5-jC-BV6"/>
9889
<constraint firstItem="Ddf-dS-jO7" firstAttribute="leading" secondItem="MOU-oS-ZgE" secondAttribute="leading" constant="24" id="nGo-Xv-oFc"/>
99-
<constraint firstAttribute="trailing" relation="greaterThanOrEqual" secondItem="q25-37-4MT" secondAttribute="trailing" constant="20" symbolic="YES" id="njk-1L-eYa"/>
100-
<constraint firstItem="q25-37-4MT" firstAttribute="leading" secondItem="Yr2-r6-oZu" secondAttribute="leading" id="o9P-mb-jkZ"/>
101-
<constraint firstItem="MOU-oS-ZgE" firstAttribute="trailing" secondItem="5q7-di-aL4" secondAttribute="trailing" constant="24" id="vaI-N9-hRD"/>
90+
<constraint firstItem="MOU-oS-ZgE" firstAttribute="trailing" secondItem="KUL-GF-oLV" secondAttribute="trailing" constant="20" id="uAG-Mg-UDP"/>
91+
<constraint firstItem="MOU-oS-ZgE" firstAttribute="trailing" secondItem="5q7-di-aL4" secondAttribute="trailing" constant="40" id="vaI-N9-hRD"/>
10292
<constraint firstItem="IHI-TG-ptk" firstAttribute="top" secondItem="9dO-Pv-Qh0" secondAttribute="bottom" constant="23" id="wZW-a0-Dgd"/>
10393
</constraints>
10494
<point key="canvasLocation" x="32.799999999999997" y="296.40179910044981"/>

TeadsSampleApp/Controllers/Native/AppLovin/TableView/NativeAppLovinTableViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class NativeAppLovinTableViewController: TeadsViewController {
4545
// Setting the modal parent view controller.
4646
let teadsAdSettings = TeadsAdapterSettings { (settings) in
4747
settings.enableDebug()
48-
settings.pageUrl("https://toto.com")
48+
settings.pageUrl("https://www.teads.tv")
4949
}
5050

5151
nativeAdLoader.register(teadsAdSettings: teadsAdSettings)

0 commit comments

Comments
 (0)