@@ -17,21 +17,26 @@ import WebKit
17
17
/// - Note:
18
18
/// method will be triggered on mainThread or thead from original call
19
19
@objc public protocol TeadsWebViewHelperDelegate {
20
- /// is called when the teads slot is shown
21
- func webViewHelperSlotStartToShow( )
20
+ /// This is called when the teads slot is shown.
21
+ @ objc optional func webViewHelperSlotStartToShow( )
22
22
23
- /// is called when the teads slot is hidden
24
- func webViewHelperSlotStartToHide( )
23
+ /// This is called when the teads slot is hidden.
24
+ @ objc optional func webViewHelperSlotStartToHide( )
25
25
26
- /// is called when no slot is found
26
+ /// This is called when the html element slot has been found.
27
27
/// - Note
28
- /// this indicates slot specified with `selector` on `TeadsWebViewHelper` init has not been found in webview html DOM
29
- func webViewHelperSlotNotFound ( )
28
+ /// This indicates slot specified with `selector` on `TeadsWebViewHelper` init has been found in webview html DOM.
29
+ @ objc optional func webViewHelperSlotFoundSuccessfully ( )
30
30
31
- /// is called when an error occured with the reason
31
+ /// This is called when no slot is found.
32
+ /// - Note
33
+ /// This indicates slot specified with `selector` on `TeadsWebViewHelper` init has not been found in webview html DOM.
34
+ @objc optional func webViewHelperSlotNotFound( )
35
+
36
+ /// This is called when an error occured with the reason.
32
37
/// - Parameters:
33
- /// - error: description of issue encountered
34
- func webViewHelperOnError( error: String )
38
+ /// - error: Description of issue encountered.
39
+ @ objc optional func webViewHelperOnError( error: String )
35
40
}
36
41
37
42
/// Helper to add TeadsInReadAd inside your webView
@@ -51,6 +56,9 @@ import WebKit
51
56
52
57
// latest slot position updated
53
58
private var slotPosition : SlotPosition ?
59
+ private var isSlotFound : Bool {
60
+ return slotPosition != nil
61
+ }
54
62
55
63
// width of element in Web content, needed to compute ratio
56
64
public var adViewHTMLElementWidth : CGFloat = 0
@@ -102,7 +110,7 @@ import WebKit
102
110
guard let webView = webView,
103
111
let bootStrapURL = Bundle ( for: Self . self) . url ( forResource: " bootstrap " , withExtension: " js " ) ,
104
112
let data = try ? Data ( contentsOf: bootStrapURL) else {
105
- delegate? . webViewHelperOnError ( error: " Unable to load bootstrap.js file, make sure to append to bundle " )
113
+ delegate? . webViewHelperOnError ? ( error: " Unable to load bootstrap.js file, make sure to append to bundle " )
106
114
return
107
115
}
108
116
@@ -118,7 +126,7 @@ import WebKit
118
126
"""
119
127
webView. evaluateJavaScript ( javascript) { [ weak delegate, weak self] _, error in
120
128
if error != nil {
121
- delegate? . webViewHelperOnError ( error: " injection of JS failed " )
129
+ delegate? . webViewHelperOnError ? ( error: " injection of JS failed " )
122
130
}
123
131
self ? . isJsReady = true
124
132
}
@@ -130,16 +138,16 @@ import WebKit
130
138
// add a timeout in case we are not able to find the slot
131
139
let timer = Timer ( timeInterval: 4 , repeats: false ) { [ weak self] _ in
132
140
self ? . slotOpener = nil
133
- self ? . delegate? . webViewHelperSlotNotFound ( )
141
+ self ? . delegate? . webViewHelperSlotNotFound ? ( )
134
142
}
135
143
noSlotTimer = timer
136
144
RunLoop . main. add ( timer, forMode: . common)
137
145
138
146
evaluateBootstrapInput ( JSBootstrapInput . insertPlaceholder ( selector) ) { [ weak self] _, error in
139
147
if error != nil {
140
148
self ? . slotOpener = nil
141
- self ? . delegate? . webViewHelperOnError ( error: " insertSlot failed " )
142
- self ? . delegate? . webViewHelperSlotNotFound ( )
149
+ self ? . delegate? . webViewHelperOnError ? ( error: " insertSlot failed " )
150
+ self ? . delegate? . webViewHelperSlotNotFound ? ( )
143
151
self ? . noSlotTimer? . invalidate ( )
144
152
} else {
145
153
self ? . slotOpener ? ( )
@@ -169,18 +177,19 @@ import WebKit
169
177
// in case openSlot is called multiple times
170
178
self . adView? . removeFromSuperview ( )
171
179
180
+ adView. isHidden = true
172
181
adView. translatesAutoresizingMaskIntoConstraints = false
173
182
self . containerView = createContainerView ( topOffset: topOffset, bottomOffset: bottomOffset)
174
183
self . containerView? . addSubview ( adView)
175
184
self . adView = adView
176
185
self . evaluateBootstrapInput ( JSBootstrapInput . showPlaceholder ( 0 ) ) { [ weak delegate] _, error in
177
186
if error != nil {
178
- delegate? . webViewHelperOnError ( error: " openSlot failed " )
187
+ delegate? . webViewHelperOnError ? ( error: " openSlot failed " )
179
188
}
180
189
}
181
190
self . slotOpener = nil
182
191
}
183
- if isJsReady {
192
+ if isSlotFound {
184
193
slotOpener ? ( )
185
194
}
186
195
}
@@ -214,7 +223,7 @@ import WebKit
214
223
}
215
224
guard let webView = webView,
216
225
adRatio != . zero else {
217
- delegate? . webViewHelperOnError ( error: " Webview can't be nil, ratio can't be equals to zero " )
226
+ delegate? . webViewHelperOnError ? ( error: " Webview can't be nil, ratio can't be equals to zero " )
218
227
return
219
228
}
220
229
@@ -229,7 +238,7 @@ import WebKit
229
238
230
239
evaluateBootstrapInput ( JSBootstrapInput . updatePlaceholder ( offsetHeight: 0 , ratioVideo: ratio) ) { [ delegate] _, error in
231
240
if error != nil {
232
- delegate? . webViewHelperOnError ( error: " updateSlot failed " )
241
+ delegate? . webViewHelperOnError ? ( error: " updateSlot failed " )
233
242
}
234
243
}
235
244
}
@@ -246,7 +255,7 @@ import WebKit
246
255
noSlotTimer? . invalidate ( )
247
256
evaluateBootstrapInput ( JSBootstrapInput . hidePlaceholder ( 0.25 ) ) { [ weak delegate] _, error in
248
257
if error != nil {
249
- delegate? . webViewHelperOnError ( error: " closeSlot failed " )
258
+ delegate? . webViewHelperOnError ? ( error: " closeSlot failed " )
250
259
}
251
260
}
252
261
}
@@ -262,15 +271,15 @@ import WebKit
262
271
noSlotTimer? . invalidate ( )
263
272
updateAdViewPosition ( position: slotPosition)
264
273
} else {
265
- delegate? . webViewHelperOnError ( error: " The json is malformed " )
274
+ delegate? . webViewHelperOnError ? ( error: " The json is malformed " )
266
275
}
267
276
}
268
277
269
278
// MARK: WKScriptMessageHandler
270
279
271
280
public func userContentController( _: WKUserContentController , didReceive message: WKScriptMessage ) {
272
281
guard let interface = JSBootstrapOutput ( rawValue: message. name) else {
273
- delegate? . webViewHelperOnError ( error: " WKMessage not supported " )
282
+ delegate? . webViewHelperOnError ? ( error: " WKMessage not supported " )
274
283
return
275
284
}
276
285
switch interface {
@@ -279,19 +288,19 @@ import WebKit
279
288
case . onSlotStartShow:
280
289
adView? . isHidden = false
281
290
// the bootstrap calls this when the slot starts to show
282
- delegate? . webViewHelperSlotStartToShow ( )
291
+ delegate? . webViewHelperSlotStartToShow ? ( )
283
292
case . onSlotUpdated:
284
293
onSlotUpdated ( position: message. body as? String )
285
294
case . onSlotStartHide:
286
295
adView? . isHidden = true
287
296
// the bootstrap calls this when the slot starts to hide
288
- delegate? . webViewHelperSlotStartToHide ( )
297
+ delegate? . webViewHelperSlotStartToHide ? ( )
289
298
case . handleError:
290
299
guard let errorString = message. body as? String else {
291
- delegate? . webViewHelperOnError ( error: " Unknown error occured " )
300
+ delegate? . webViewHelperOnError ? ( error: " Unknown error occured " )
292
301
return
293
302
}
294
- delegate? . webViewHelperOnError ( error: errorString)
303
+ delegate? . webViewHelperOnError ? ( error: errorString)
295
304
}
296
305
}
297
306
@@ -300,6 +309,10 @@ import WebKit
300
309
/// - Parameters:
301
310
/// - position: top/bottom/right/left position of the slot
302
311
private func updateAdViewPosition( position: SlotPosition ) {
312
+ if !isSlotFound {
313
+ delegate? . webViewHelperSlotFoundSuccessfully ? ( )
314
+ }
315
+
303
316
adViewHTMLElementWidth = position. right - position. left
304
317
slotPosition = position
305
318
0 commit comments