@@ -48,10 +48,11 @@ public class TeadsWebViewHelper: NSObject, WKScriptMessageHandler {
48
48
// only webView.scrollView should retain adView as subView
49
49
weak var adView : UIView ?
50
50
51
- var adOpportunityTrackerView : TeadsAdOpportunityTrackerView ?
52
-
53
51
private var adViewConstraints = [ NSLayoutConstraint] ( )
54
52
53
+ // latest slot position updated
54
+ private var slotPosition : SlotPosition ?
55
+
55
56
// width of element in Web content, needed to compute ratio
56
57
public var adViewHTMLElementWidth : CGFloat = 0
57
58
@@ -61,6 +62,7 @@ public class TeadsWebViewHelper: NSObject, WKScriptMessageHandler {
61
62
private var isJsReady = false
62
63
63
64
private var slotOpenner : ( ( ) -> Void ) ?
65
+ private var slotPositionAvailable : ( ( WKWebView , SlotPosition ) -> Void ) ?
64
66
65
67
/// Init the Teads webView helper
66
68
///
@@ -141,7 +143,6 @@ public class TeadsWebViewHelper: NSObject, WKScriptMessageHandler {
141
143
self ? . noSlotTimer? . invalidate ( )
142
144
}
143
145
self ? . slotOpenner ? ( )
144
- self ? . slotOpenner = nil
145
146
}
146
147
}
147
148
@@ -167,9 +168,6 @@ public class TeadsWebViewHelper: NSObject, WKScriptMessageHandler {
167
168
self . adView? . removeFromSuperview ( )
168
169
169
170
self . adView = adView
170
- if let adOpportunityTrackerView = self . adOpportunityTrackerView {
171
- self . adView? . addSubview ( adOpportunityTrackerView)
172
- }
173
171
self . webView? . scrollView. addSubview ( adView)
174
172
self . adView? . translatesAutoresizingMaskIntoConstraints = false
175
173
@@ -286,8 +284,14 @@ public class TeadsWebViewHelper: NSObject, WKScriptMessageHandler {
286
284
/// - position: top/bottom/right/left position of the slot
287
285
private func updateAdViewPosition( position: SlotPosition ) {
288
286
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 {
291
295
return
292
296
}
293
297
let adViewHTMLElementHeight = position. bottom - position. top
@@ -309,12 +313,36 @@ public class TeadsWebViewHelper: NSObject, WKScriptMessageHandler {
309
313
adViewConstraints. append ( adView. topAnchor. constraint ( equalTo: webView. scrollView. topAnchor, constant: position. top) )
310
314
311
315
if shouldUpdateAdViewFrame {
312
- adViewConstraints. append ( adView. widthAnchor. constraint ( equalToConstant: adViewHTMLElementWidth) )
316
+ adViewConstraints. append ( adView. widthAnchor. constraint ( equalToConstant: adViewHTMLElementWidth) )
313
317
adViewConstraints. append ( adView. heightAnchor. constraint ( equalToConstant: adViewHTMLElementHeight) )
314
318
}
315
319
316
320
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)
318
346
}
319
347
}
320
348
0 commit comments