@@ -71,6 +71,8 @@ import WebKit
71
71
private var slotOpener : ( ( ) -> Void ) ?
72
72
private var slotOpportunity : ( ( WKWebView , SlotPosition ) -> Void ) ?
73
73
74
+ private static let urlKeyPath = NSExpression ( forKeyPath: \WKWebView . url) . keyPath
75
+
74
76
/// Init the Teads webView helper
75
77
///
76
78
/// - Note: handles slot position injection from TeadsSDK
@@ -87,6 +89,9 @@ import WebKit
87
89
self . delegate = delegate
88
90
super. init ( )
89
91
92
+ // URL Change observer
93
+ webView. addObserver ( self , forKeyPath: Self . urlKeyPath, options: . new, context: nil )
94
+
90
95
// add message handler method name to communicate with the wkwebview
91
96
JSBootstrapOutput . allCases. map ( \. rawValue) . forEach {
92
97
webView. configuration. userContentController. add ( WKWeakScriptHandler ( delegate: self ) , name: $0)
@@ -104,6 +109,15 @@ import WebKit
104
109
NotificationCenter . default. removeObserver ( self )
105
110
}
106
111
112
+ override public func observeValue( forKeyPath keyPath: String ? , of _: Any ? , change: [ NSKeyValueChangeKey : Any ] ? , context _: UnsafeMutableRawPointer ? ) {
113
+ if keyPath == Self . urlKeyPath,
114
+ let _ = change ? [ NSKeyValueChangeKey . newKey] {
115
+ adView? . removeFromSuperview ( )
116
+ containerView? . removeFromSuperview ( )
117
+ slotPosition = nil
118
+ }
119
+ }
120
+
107
121
/// Inject Teads' bootstrap in the webview
108
122
/// make sure bootstrap.js file is present in same the bundle of this file
109
123
@objc public func injectJS( ) {
@@ -176,6 +190,7 @@ import WebKit
176
190
177
191
// in case openSlot is called multiple times
178
192
self . adView? . removeFromSuperview ( )
193
+ self . containerView? . removeFromSuperview ( )
179
194
180
195
adView. isHidden = true
181
196
adView. translatesAutoresizingMaskIntoConstraints = false
@@ -198,13 +213,14 @@ import WebKit
198
213
let container = PassthroughView ( )
199
214
container. clipsToBounds = true
200
215
container. translatesAutoresizingMaskIntoConstraints = false
201
- if let webView = webView {
216
+ if let webView = webView,
217
+ let slotPosition = slotPosition {
202
218
webView. scrollView. addSubview ( container)
203
219
NSLayoutConstraint . activate ( [
204
220
container. topAnchor. constraint ( equalTo: webView. topAnchor, constant: topOffset) ,
205
221
container. bottomAnchor. constraint ( equalTo: webView. bottomAnchor, constant: - bottomOffset) ,
206
- container. leadingAnchor. constraint ( equalTo: webView. leadingAnchor) ,
207
- container. trailingAnchor . constraint ( equalTo : webView . trailingAnchor ) ,
222
+ container. leadingAnchor. constraint ( equalTo: webView. leadingAnchor, constant : slotPosition . left ) ,
223
+ container. widthAnchor . constraint ( equalToConstant : slotPosition . right - slotPosition . left ) ,
208
224
] )
209
225
}
210
226
return container
@@ -473,7 +489,10 @@ extension TeadsWebViewHelper {
473
489
class PassthroughView : UIView {
474
490
override func point( inside point: CGPoint , with event: UIEvent ? ) -> Bool {
475
491
for view in subviews {
476
- if view. isUserInteractionEnabled, view. point ( inside: convert ( point, to: view) , with: event) {
492
+ if point. y > 0 ,
493
+ point. y < bounds. size. height,
494
+ view. isUserInteractionEnabled,
495
+ view. point ( inside: convert ( point, to: view) , with: event) {
477
496
return true
478
497
}
479
498
}
0 commit comments