Skip to content

Commit 242e2c9

Browse files
Create a new WebViewHelperDelegate method for slot found (#183)
1 parent ea74ef7 commit 242e2c9

File tree

3 files changed

+68
-50
lines changed

3 files changed

+68
-50
lines changed

TeadsSampleApp/Controllers/InRead/Admob/WebView/InReadAdmobWebViewController.swift

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,6 @@ class InReadAdmobWebViewController: TeadsViewController {
3838

3939
/// init helper
4040
webViewHelper = TeadsWebViewHelper(webView: webView, selector: "#teads-placement-slot", delegate: self)
41-
42-
let adSettings = TeadsAdapterSettings { settings in
43-
settings.enableDebug()
44-
settings.disableLocation()
45-
try? settings.registerAdView(bannerView, delegate: self)
46-
47-
// Needed by european regulation
48-
// See https://mobile.teads.tv/sdk/documentation/ios/gdpr-consent
49-
// settings.userConsent(subjectToGDPR: "1", consent: "0001100101010101")
50-
51-
// The article url if you are a news publisher
52-
// settings.pageUrl("http://page.com/article1")
53-
}
54-
55-
let customEventExtras = GADMAdapterTeads.customEventExtra(with: adSettings)
56-
57-
let request = GADRequest()
58-
request.register(customEventExtras)
59-
60-
bannerView.load(request)
6141
}
6242
}
6343

@@ -110,6 +90,28 @@ extension InReadAdmobWebViewController: TeadsWebViewHelperDelegate {
11090
print("webViewHelperSlotStartToHide")
11191
}
11292

93+
func webViewHelperSlotFoundSuccessfully() {
94+
print("webViewHelperSlotFoundSuccessfully")
95+
let adSettings = TeadsAdapterSettings { settings in
96+
settings.enableDebug()
97+
try? settings.registerAdView(bannerView, delegate: self)
98+
99+
// Needed by european regulation
100+
// See https://mobile.teads.tv/sdk/documentation/ios/gdpr-consent
101+
// settings.userConsent(subjectToGDPR: "1", consent: "0001100101010101")
102+
103+
// The article url if you are a news publisher
104+
// settings.pageUrl("http://page.com/article1")
105+
}
106+
107+
let customEventExtras = GADMAdapterTeads.customEventExtra(with: adSettings)
108+
109+
let request = GADRequest()
110+
request.register(customEventExtras)
111+
112+
bannerView.load(request)
113+
}
114+
113115
func webViewHelperSlotNotFound() {
114116
print("webViewHelperSlotNotFound")
115117
}

TeadsSampleApp/Controllers/InRead/Direct/WebView/InReadDirectWebViewController.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ class InReadDirectWebViewController: TeadsViewController, WKNavigationDelegate {
3434
// settings.enableDebug()
3535
}
3636
placement = Teads.createInReadPlacement(pid: Int(pid) ?? 0, settings: pSettings, delegate: self)
37-
38-
placement?.requestAd(requestSettings: TeadsAdRequestSettings { settings in
39-
settings.pageUrl("https://www.teads.tv")
40-
})
4137
}
4238

4339
// MARK: WKNavigationDelegate
@@ -110,6 +106,13 @@ extension InReadDirectWebViewController: TeadsWebViewHelperDelegate {
110106
print("webViewHelperSlotStartToHide")
111107
}
112108

109+
func webViewHelperSlotFoundSuccessfully() {
110+
print("webViewHelperSlotFoundSuccessfully")
111+
placement?.requestAd(requestSettings: TeadsAdRequestSettings { settings in
112+
settings.pageUrl("https://www.teads.tv")
113+
})
114+
}
115+
113116
func webViewHelperSlotNotFound() {
114117
print("webViewHelperSlotNotFound")
115118
}

TeadsSampleApp/WebViewHelper/TeadsWebViewHelper.swift

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,26 @@ import WebKit
1717
/// - Note:
1818
/// method will be triggered on mainThread or thead from original call
1919
@objc public protocol TeadsWebViewHelperDelegate {
20-
/// is called when the teads slot is shown
21-
func webViewHelperSlotStartToShow()
20+
/// This is called when the teads slot is shown.
21+
@objc optional func webViewHelperSlotStartToShow()
2222

23-
/// is called when the teads slot is hidden
24-
func webViewHelperSlotStartToHide()
23+
/// This is called when the teads slot is hidden.
24+
@objc optional func webViewHelperSlotStartToHide()
2525

26-
/// is called when no slot is found
26+
/// This is called when the html element slot has been found.
2727
/// - Note
28-
/// this indicates slot specified with `selector` on `TeadsWebViewHelper` init has not been found in webview html DOM
29-
func webViewHelperSlotNotFound()
28+
/// This indicates slot specified with `selector` on `TeadsWebViewHelper` init has been found in webview html DOM.
29+
@objc optional func webViewHelperSlotFoundSuccessfully()
3030

31-
/// is called when an error occured with the reason
31+
/// This is called when no slot is found.
32+
/// - Note
33+
/// This indicates slot specified with `selector` on `TeadsWebViewHelper` init has not been found in webview html DOM.
34+
@objc optional func webViewHelperSlotNotFound()
35+
36+
/// This is called when an error occured with the reason.
3237
/// - Parameters:
33-
/// - error: description of issue encountered
34-
func webViewHelperOnError(error: String)
38+
/// - error: Description of issue encountered.
39+
@objc optional func webViewHelperOnError(error: String)
3540
}
3641

3742
/// Helper to add TeadsInReadAd inside your webView
@@ -51,6 +56,9 @@ import WebKit
5156

5257
// latest slot position updated
5358
private var slotPosition: SlotPosition?
59+
private var isSlotFound: Bool {
60+
return slotPosition != nil
61+
}
5462

5563
// width of element in Web content, needed to compute ratio
5664
public var adViewHTMLElementWidth: CGFloat = 0
@@ -102,7 +110,7 @@ import WebKit
102110
guard let webView = webView,
103111
let bootStrapURL = Bundle(for: Self.self).url(forResource: "bootstrap", withExtension: "js"),
104112
let data = try? Data(contentsOf: bootStrapURL) else {
105-
delegate?.webViewHelperOnError(error: "Unable to load bootstrap.js file, make sure to append to bundle")
113+
delegate?.webViewHelperOnError?(error: "Unable to load bootstrap.js file, make sure to append to bundle")
106114
return
107115
}
108116

@@ -118,7 +126,7 @@ import WebKit
118126
"""
119127
webView.evaluateJavaScript(javascript) { [weak delegate, weak self] _, error in
120128
if error != nil {
121-
delegate?.webViewHelperOnError(error: "injection of JS failed")
129+
delegate?.webViewHelperOnError?(error: "injection of JS failed")
122130
}
123131
self?.isJsReady = true
124132
}
@@ -130,16 +138,16 @@ import WebKit
130138
// add a timeout in case we are not able to find the slot
131139
let timer = Timer(timeInterval: 4, repeats: false) { [weak self] _ in
132140
self?.slotOpener = nil
133-
self?.delegate?.webViewHelperSlotNotFound()
141+
self?.delegate?.webViewHelperSlotNotFound?()
134142
}
135143
noSlotTimer = timer
136144
RunLoop.main.add(timer, forMode: .common)
137145

138146
evaluateBootstrapInput(JSBootstrapInput.insertPlaceholder(selector)) { [weak self] _, error in
139147
if error != nil {
140148
self?.slotOpener = nil
141-
self?.delegate?.webViewHelperOnError(error: "insertSlot failed")
142-
self?.delegate?.webViewHelperSlotNotFound()
149+
self?.delegate?.webViewHelperOnError?(error: "insertSlot failed")
150+
self?.delegate?.webViewHelperSlotNotFound?()
143151
self?.noSlotTimer?.invalidate()
144152
} else {
145153
self?.slotOpener?()
@@ -169,18 +177,19 @@ import WebKit
169177
// in case openSlot is called multiple times
170178
self.adView?.removeFromSuperview()
171179

180+
adView.isHidden = true
172181
adView.translatesAutoresizingMaskIntoConstraints = false
173182
self.containerView = createContainerView(topOffset: topOffset, bottomOffset: bottomOffset)
174183
self.containerView?.addSubview(adView)
175184
self.adView = adView
176185
self.evaluateBootstrapInput(JSBootstrapInput.showPlaceholder(0)) { [weak delegate] _, error in
177186
if error != nil {
178-
delegate?.webViewHelperOnError(error: "openSlot failed")
187+
delegate?.webViewHelperOnError?(error: "openSlot failed")
179188
}
180189
}
181190
self.slotOpener = nil
182191
}
183-
if isJsReady {
192+
if isSlotFound {
184193
slotOpener?()
185194
}
186195
}
@@ -214,7 +223,7 @@ import WebKit
214223
}
215224
guard let webView = webView,
216225
adRatio != .zero else {
217-
delegate?.webViewHelperOnError(error: "Webview can't be nil, ratio can't be equals to zero")
226+
delegate?.webViewHelperOnError?(error: "Webview can't be nil, ratio can't be equals to zero")
218227
return
219228
}
220229

@@ -229,7 +238,7 @@ import WebKit
229238

230239
evaluateBootstrapInput(JSBootstrapInput.updatePlaceholder(offsetHeight: 0, ratioVideo: ratio)) { [delegate] _, error in
231240
if error != nil {
232-
delegate?.webViewHelperOnError(error: "updateSlot failed")
241+
delegate?.webViewHelperOnError?(error: "updateSlot failed")
233242
}
234243
}
235244
}
@@ -246,7 +255,7 @@ import WebKit
246255
noSlotTimer?.invalidate()
247256
evaluateBootstrapInput(JSBootstrapInput.hidePlaceholder(0.25)) { [weak delegate] _, error in
248257
if error != nil {
249-
delegate?.webViewHelperOnError(error: "closeSlot failed")
258+
delegate?.webViewHelperOnError?(error: "closeSlot failed")
250259
}
251260
}
252261
}
@@ -262,15 +271,15 @@ import WebKit
262271
noSlotTimer?.invalidate()
263272
updateAdViewPosition(position: slotPosition)
264273
} else {
265-
delegate?.webViewHelperOnError(error: "The json is malformed")
274+
delegate?.webViewHelperOnError?(error: "The json is malformed")
266275
}
267276
}
268277

269278
// MARK: WKScriptMessageHandler
270279

271280
public func userContentController(_: WKUserContentController, didReceive message: WKScriptMessage) {
272281
guard let interface = JSBootstrapOutput(rawValue: message.name) else {
273-
delegate?.webViewHelperOnError(error: "WKMessage not supported")
282+
delegate?.webViewHelperOnError?(error: "WKMessage not supported")
274283
return
275284
}
276285
switch interface {
@@ -279,19 +288,19 @@ import WebKit
279288
case .onSlotStartShow:
280289
adView?.isHidden = false
281290
// the bootstrap calls this when the slot starts to show
282-
delegate?.webViewHelperSlotStartToShow()
291+
delegate?.webViewHelperSlotStartToShow?()
283292
case .onSlotUpdated:
284293
onSlotUpdated(position: message.body as? String)
285294
case .onSlotStartHide:
286295
adView?.isHidden = true
287296
// the bootstrap calls this when the slot starts to hide
288-
delegate?.webViewHelperSlotStartToHide()
297+
delegate?.webViewHelperSlotStartToHide?()
289298
case .handleError:
290299
guard let errorString = message.body as? String else {
291-
delegate?.webViewHelperOnError(error: "Unknown error occured")
300+
delegate?.webViewHelperOnError?(error: "Unknown error occured")
292301
return
293302
}
294-
delegate?.webViewHelperOnError(error: errorString)
303+
delegate?.webViewHelperOnError?(error: errorString)
295304
}
296305
}
297306

@@ -300,6 +309,10 @@ import WebKit
300309
/// - Parameters:
301310
/// - position: top/bottom/right/left position of the slot
302311
private func updateAdViewPosition(position: SlotPosition) {
312+
if !isSlotFound {
313+
delegate?.webViewHelperSlotFoundSuccessfully?()
314+
}
315+
303316
adViewHTMLElementWidth = position.right - position.left
304317
slotPosition = position
305318

0 commit comments

Comments
 (0)