@@ -76,8 +76,6 @@ import WebKit
76
76
private var slotOpener : ( ( ) -> Void ) ?
77
77
private var slotOpportunity : ( ( WKWebView , SlotPosition ) -> Void ) ?
78
78
79
- private static let urlKeyPath = NSExpression ( forKeyPath: \WKWebView . url) . keyPath
80
-
81
79
/// Init the Teads webView helper
82
80
///
83
81
/// Handles slot position injection from TeadsSDK
@@ -95,8 +93,15 @@ import WebKit
95
93
self . delegate = delegate
96
94
super. init ( )
97
95
98
- // URL Change observer
99
- webView. addObserver ( self , forKeyPath: Self . urlKeyPath, options: . new, context: nil )
96
+ /// Track url requests change in order to reset adView and position tracking
97
+ /// When position is located a new time, alert through ``TeadsWebViewHelperDelegate/webViewHelperSlotFoundSuccessfully()``
98
+ webViewObservation = webView. observe ( \. url, options: [ . old, . new] ) { [ weak self] _, changes in
99
+ if changes. oldValue != changes. newValue {
100
+ self ? . adView? . removeFromSuperview ( )
101
+ self ? . containerView? . removeFromSuperview ( )
102
+ self ? . slotPosition = nil
103
+ }
104
+ }
100
105
101
106
// add message handler method name to communicate with the wkwebview
102
107
JSBootstrapOutput . allCases. map ( \. rawValue) . forEach {
@@ -120,18 +125,6 @@ import WebKit
120
125
}
121
126
}
122
127
webViewObservation = nil
123
- NotificationCenter . default. removeObserver ( self )
124
- }
125
-
126
- /// Track url requests change in order to reset adView and position tracking
127
- /// When position is located a new time, alert through ``TeadsWebViewHelperDelegate/webViewHelperSlotFoundSuccessfully()``
128
- override public func observeValue( forKeyPath keyPath: String ? , of _: Any ? , change: [ NSKeyValueChangeKey : Any ] ? , context _: UnsafeMutableRawPointer ? ) {
129
- if keyPath == Self . urlKeyPath,
130
- let _ = change ? [ NSKeyValueChangeKey . newKey] {
131
- adView? . removeFromSuperview ( )
132
- containerView? . removeFromSuperview ( )
133
- slotPosition = nil
134
- }
135
128
}
136
129
137
130
/// Inject Teads' bootstrap in the webview
@@ -473,7 +466,7 @@ extension TeadsWebViewHelper {
473
466
/// When registering WKWeakScriptHandler, WKWebView create a strong reference to handler
474
467
/// This leads to retain cycle between webView <-> owner and webView <-> scriptHandler
475
468
/// In order to avoid retain cycle
476
- class WKWeakScriptHandler : NSObject , WKScriptMessageHandler {
469
+ final class WKWeakScriptHandler : NSObject , WKScriptMessageHandler {
477
470
weak var delegate : WKScriptMessageHandler ?
478
471
479
472
init ( delegate: WKScriptMessageHandler ) {
0 commit comments