Skip to content

Commit 4d41c82

Browse files
Add URL observer on WebViewHelper (#184)
1 parent 1477e64 commit 4d41c82

File tree

4 files changed

+35
-21
lines changed

4 files changed

+35
-21
lines changed

Gemfile.lock

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ GEM
8888
zeitwerk (2.5.1)
8989

9090
PLATFORMS
91+
arm64-darwin-21
9192
x86_64-darwin-19
9293
x86_64-darwin-20
9394
x86_64-linux
@@ -96,4 +97,4 @@ DEPENDENCIES
9697
cocoapods (>= 1.10.2)
9798

9899
BUNDLED WITH
99-
2.2.21
100+
2.3.13

Podfile.lock

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ PODS:
6666

6767
DEPENDENCIES:
6868
- SwiftFormat/CLI
69-
- TeadsAdMobAdapter (from `./`)
70-
- TeadsAppLovinAdapter (from `./`)
71-
- TeadsSASAdapter (from `./`)
72-
- TeadsSDK (from `./`)
69+
- TeadsAdMobAdapter (~> 5.0)
70+
- TeadsAppLovinAdapter (~> 5.0)
71+
- TeadsSASAdapter (~> 5.0)
72+
- TeadsSDK (~> 5.0)
7373

7474
SPEC REPOS:
7575
trunk:
@@ -83,16 +83,10 @@ SPEC REPOS:
8383
- Smart-Core-SDK
8484
- Smart-Display-SDK
8585
- SwiftFormat
86-
87-
EXTERNAL SOURCES:
88-
TeadsAdMobAdapter:
89-
:path: "./"
90-
TeadsAppLovinAdapter:
91-
:path: "./"
92-
TeadsSASAdapter:
93-
:path: "./"
94-
TeadsSDK:
95-
:path: "./"
86+
- TeadsAdMobAdapter
87+
- TeadsAppLovinAdapter
88+
- TeadsSASAdapter
89+
- TeadsSDK
9690

9791
SPEC CHECKSUMS:
9892
AppLovinSDK: 6ed446d284d8852520a786ad5a91796f1730c3f3
@@ -110,6 +104,6 @@ SPEC CHECKSUMS:
110104
TeadsSASAdapter: 4b9712a8e94722578fb38feffcdff7ed53f67afb
111105
TeadsSDK: 63cd22c0351854eee577ac0960fe5cba4b2b1bc6
112106

113-
PODFILE CHECKSUM: 55210a42754a193fd1cc6ffc7f79bbae342aea73
107+
PODFILE CHECKSUM: 9003282ee893b30b3d0c343b3a7cf4667732b291
114108

115109
COCOAPODS: 1.11.2

TeadsSampleApp/WebViewHelper/Resources/bootstrap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
var verticalSpacer = 10;
1515
var showHideTimerDuration = 100;
16-
var intervalCheckPosition = 500;
16+
var intervalCheckPosition = 250;
1717
var opened = false;
1818
var bridge, teadsContainer, finalSize, intervalPosition, offset, heightSup, ratio, maxHeight;
1919
var transitionType = "height [DURATION]ms ease-in-out";

TeadsSampleApp/WebViewHelper/TeadsWebViewHelper.swift

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ import WebKit
7171
private var slotOpener: (() -> Void)?
7272
private var slotOpportunity: ((WKWebView, SlotPosition) -> Void)?
7373

74+
private static let urlKeyPath = NSExpression(forKeyPath: \WKWebView.url).keyPath
75+
7476
/// Init the Teads webView helper
7577
///
7678
/// - Note: handles slot position injection from TeadsSDK
@@ -87,6 +89,9 @@ import WebKit
8789
self.delegate = delegate
8890
super.init()
8991

92+
// URL Change observer
93+
webView.addObserver(self, forKeyPath: Self.urlKeyPath, options: .new, context: nil)
94+
9095
// add message handler method name to communicate with the wkwebview
9196
JSBootstrapOutput.allCases.map(\.rawValue).forEach {
9297
webView.configuration.userContentController.add(WKWeakScriptHandler(delegate: self), name: $0)
@@ -104,6 +109,15 @@ import WebKit
104109
NotificationCenter.default.removeObserver(self)
105110
}
106111

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+
107121
/// Inject Teads' bootstrap in the webview
108122
/// make sure bootstrap.js file is present in same the bundle of this file
109123
@objc public func injectJS() {
@@ -176,6 +190,7 @@ import WebKit
176190

177191
// in case openSlot is called multiple times
178192
self.adView?.removeFromSuperview()
193+
self.containerView?.removeFromSuperview()
179194

180195
adView.isHidden = true
181196
adView.translatesAutoresizingMaskIntoConstraints = false
@@ -198,13 +213,14 @@ import WebKit
198213
let container = PassthroughView()
199214
container.clipsToBounds = true
200215
container.translatesAutoresizingMaskIntoConstraints = false
201-
if let webView = webView {
216+
if let webView = webView,
217+
let slotPosition = slotPosition {
202218
webView.scrollView.addSubview(container)
203219
NSLayoutConstraint.activate([
204220
container.topAnchor.constraint(equalTo: webView.topAnchor, constant: topOffset),
205221
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),
208224
])
209225
}
210226
return container
@@ -473,7 +489,10 @@ extension TeadsWebViewHelper {
473489
class PassthroughView: UIView {
474490
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
475491
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) {
477496
return true
478497
}
479498
}

0 commit comments

Comments
 (0)