Skip to content

Commit c66481a

Browse files
committed
update setAdOpportunityTrackerView
1 parent 2f2fbd8 commit c66481a

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ extension InReadDirectWebViewController: TeadsInReadAdPlacementDelegate {
7171
}
7272

7373
func adOpportunityTrackerView(trackerView: TeadsAdOpportunityTrackerView) {
74-
self.webViewHelper?.adOpportunityTrackerView = trackerView
74+
self.webViewHelper?.setAdOpportunityTrackerView(trackerView)
7575
}
7676
}
7777

TeadsSampleApp/WebViewHelper/Resources/bootstrap.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @module Teads JS Utils for inRead
33
* @author RonanDrouglazet <[email protected]>
4-
* @date 10-2014
4+
* @date 08-2021
55
* @copyright Teads <http://www.teads.tv>
66
*
77
* ⚠️ This bootstrap has been provided to give you a hand in your integration webview.
@@ -17,7 +17,7 @@
1717
var opened = false;
1818
var bridge, teadsContainer, finalSize, intervalPosition, offset, heightSup, ratio, maxHeight;
1919
var transitionType = "height [DURATION]ms ease-in-out";
20-
// command use to communicate with WebViewController JS Bridge
20+
// command use to communicate with TeadsWebViewHelper JS Bridge
2121
var command = {
2222
trigger: {
2323
ready: "onTeadsJsLibReady",
@@ -45,10 +45,10 @@
4545
var platformType;
4646

4747
/*****************************************
48-
* Method called by WebViewController (through bridge) *
48+
* Method called by TeadsWebViewHelper (through bridge) *
4949
*****************************************/
5050

51-
// Check if WebViewController JS Bridge is present, set handler, and say JS Ready for WebViewController
51+
// Check if TeadsWebViewHelper JS Bridge is present, set handler, and say JS Ready for TeadsWebViewHelper
5252
var sendJsLibReady = function() {
5353
platformType = getPlatformType();
5454
if (platformType === IOS_OS) {
@@ -134,7 +134,7 @@
134134
}, 'hidePlaceholder')
135135
};
136136

137-
// send placeholder's coordinate to WebViewController for player positioning
137+
// send placeholder's coordinate to TeadsWebViewHelper for player positioning
138138
var sendTargetGeometry = function() {
139139
tryOrLog(function() {
140140
if (teadsContainer) {
@@ -184,7 +184,7 @@
184184
}, 'getPlatformType')
185185
};
186186

187-
// register handler on the WebViewController JS Bridge
187+
// register handler on the TeadsWebViewHelper JS Bridge
188188
var setBridgeHandler = function(wvBridge) {
189189
tryOrLog(function() {
190190
bridge = wvBridge;
@@ -224,7 +224,7 @@
224224
}, 'setBridgeHandler')
225225
};
226226

227-
// set placeholder size, create it, and put it before "element" on document, then send coordinates to WebViewController
227+
// set placeholder size, create it, and put it before "element" on document, then send coordinates to TeadsWebViewHelper
228228
var setPlaceholderDiv = function(element) {
229229
tryOrLog(function() {
230230
var parent = element.parentNode;
@@ -273,7 +273,7 @@
273273
}, 'getDocumentScroll')
274274
};
275275

276-
// find a slot in document with a CSS selector given by WebViewController (or automatic if no selector provided)
276+
// find a slot in document with a CSS selector given by TeadsWebViewHelper (or automatic if no selector provided)
277277
var findSlot = function(selector) {
278278
return tryOrLog(function() {
279279

TeadsSampleApp/WebViewHelper/TeadsWebViewHelper.swift

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ public class TeadsWebViewHelper: NSObject, WKScriptMessageHandler {
4848
// only webView.scrollView should retain adView as subView
4949
weak var adView: UIView?
5050

51-
var adOpportunityTrackerView: TeadsAdOpportunityTrackerView?
52-
5351
private var adViewConstraints = [NSLayoutConstraint]()
5452

53+
// latest slot position updated
54+
private var slotPosition: SlotPosition?
55+
5556
// width of element in Web content, needed to compute ratio
5657
public var adViewHTMLElementWidth: CGFloat = 0
5758

@@ -61,6 +62,7 @@ public class TeadsWebViewHelper: NSObject, WKScriptMessageHandler {
6162
private var isJsReady = false
6263

6364
private var slotOpenner: (() -> Void)?
65+
private var slotPositionAvailable: ((WKWebView, SlotPosition) -> Void)?
6466

6567
/// Init the Teads webView helper
6668
///
@@ -141,7 +143,6 @@ public class TeadsWebViewHelper: NSObject, WKScriptMessageHandler {
141143
self?.noSlotTimer?.invalidate()
142144
}
143145
self?.slotOpenner?()
144-
self?.slotOpenner = nil
145146
}
146147
}
147148

@@ -167,9 +168,6 @@ public class TeadsWebViewHelper: NSObject, WKScriptMessageHandler {
167168
self.adView?.removeFromSuperview()
168169

169170
self.adView = adView
170-
if let adOpportunityTrackerView = self.adOpportunityTrackerView {
171-
self.adView?.addSubview(adOpportunityTrackerView)
172-
}
173171
self.webView?.scrollView.addSubview(adView)
174172
self.adView?.translatesAutoresizingMaskIntoConstraints = false
175173

@@ -286,8 +284,14 @@ public class TeadsWebViewHelper: NSObject, WKScriptMessageHandler {
286284
/// - position: top/bottom/right/left position of the slot
287285
private func updateAdViewPosition(position: SlotPosition) {
288286
adViewHTMLElementWidth = position.right - position.left
289-
guard let adView = self.adView,
290-
let webView = self.webView else {
287+
slotPosition = position
288+
289+
guard let webView = self.webView else {
290+
return
291+
}
292+
slotPositionAvailable?(webView, position)
293+
294+
guard let adView = self.adView else {
291295
return
292296
}
293297
let adViewHTMLElementHeight = position.bottom - position.top
@@ -309,12 +313,36 @@ public class TeadsWebViewHelper: NSObject, WKScriptMessageHandler {
309313
adViewConstraints.append(adView.topAnchor.constraint(equalTo: webView.scrollView.topAnchor, constant: position.top))
310314

311315
if shouldUpdateAdViewFrame {
312-
adViewConstraints.append(adView.widthAnchor.constraint(equalToConstant: adViewHTMLElementWidth))
316+
adViewConstraints.append(adView.widthAnchor.constraint(equalToConstant: adViewHTMLElementWidth))
313317
adViewConstraints.append(adView.heightAnchor.constraint(equalToConstant: adViewHTMLElementHeight))
314318
}
315319

316320
NSLayoutConstraint.activate(adViewConstraints)
317-
321+
}
322+
323+
/// `adOpportunity` is a key metrics to evaluate the performance of your inventory. It builds the visibility score of your placement in publisher dashboards.
324+
///
325+
/// - Parameters:
326+
/// - adOpportunityTrackerView: the view that will monitor your inventory
327+
///
328+
@objc public func setAdOpportunityTrackerView(_ adOpportunityTrackerView: TeadsAdOpportunityTrackerView) {
329+
slotPositionAvailable = { [weak self] webView, position in
330+
adOpportunityTrackerView.frame = CGRect(x: position.left, y: position.top, width: 1, height: 1)
331+
webView.scrollView.addSubview(adOpportunityTrackerView)
332+
333+
adOpportunityTrackerView.translatesAutoresizingMaskIntoConstraints = false
334+
335+
adOpportunityTrackerView.topAnchor.constraint(equalTo: webView.scrollView.topAnchor, constant: position.top).isActive = true
336+
adOpportunityTrackerView.leadingAnchor.constraint(equalTo: webView.scrollView.leadingAnchor, constant: position.left).isActive = true
337+
adOpportunityTrackerView.widthAnchor.constraint(equalToConstant: 1).isActive = true
338+
adOpportunityTrackerView.heightAnchor.constraint(equalToConstant: 1).isActive = true
339+
self?.slotPositionAvailable = nil
340+
}
341+
guard let position = slotPosition,
342+
let webView = webView else {
343+
return
344+
}
345+
slotPositionAvailable?(webView, position)
318346
}
319347
}
320348

0 commit comments

Comments
 (0)