-
Notifications
You must be signed in to change notification settings - Fork 239
Expand file tree
/
Copy pathCountlyWebViewManager.m
More file actions
959 lines (848 loc) · 43.4 KB
/
Copy pathCountlyWebViewManager.m
File metadata and controls
959 lines (848 loc) · 43.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
// CountlyWebViewManager.m
//
// This code is provided under the MIT License.
//
// Please visit www.count.ly for more information.
#import "CountlyWebViewManager.h"
#import "CountlyCommon.h"
#import "CountlyOverlayWindow.h"
#import "CountlyWebViewController.h"
#import "PassThroughBackgroundView.h"
#import "CountlyContentBuilderInternal.h"
#if (TARGET_OS_IOS)
// Critical-resource load retries: how many times to reload the web view before
// giving up when a critical (JS/CSS) resource fails to load. A single transient
// network hiccup — e.g. a connection stalled under a burst of parallel asset
// requests against an HTTP/1.1 / rate-limited edge — should not tear down otherwise
// valid content. On reload, already-loaded resources are served from the in-session
// cache, so the retry re-fetches only what failed. Mirrors the more tolerant Android
// behavior (which never closes on a transient JS error event).
static const NSInteger kCLYMaxResourceRetries = 2;
static const NSTimeInterval kCLYResourceRetryBaseDelay = 0.6;
// Fallback stall timeout (seconds) used only when reload-on-stall is enabled but no
// timeout was configured (e.g. the SDK was not started via config in a unit test). The
// real value comes from CountlyContentConfig setContentReloadOnStallTimeout: (default
// 1000 ms). Kept short because a manual reload is observed to recover reliably: on reload
// the already-fetched assets come from cache and the rest reuse the warm connection, so
// the parallel-connection burst shrinks below the edge's limit. A stalled load fires no
// JS 'error' event, so this timer is what triggers the reload for that case. When the
// flag is off, the 60s safety-net timeout is used and the view closes on failure.
static const NSTimeInterval kCLYLoadStallTimeout = 1.0;
static const NSTimeInterval kCLYDefaultLoadTimeout = 60.0;
// Absolute deadline (seconds) by which the content must report [CLY]_content_shown, else the
// web view is closed. Armed ONCE when the web view is created and never reset by reloads, so it
// is a hard backstop the reload/stall machinery cannot defeat: no matter how the retry/verify
// logic behaves, a content that never actually shows is torn down within this window. This is
// the guaranteed replacement for the old fixed 60s close (which no longer applies once the
// per-navigation stall timer shrinks under reload-on-stall).
static const NSTimeInterval kCLYContentShownDeadline = 60.0;
// TODO: improve logging, check edge cases
@interface CountlyWebViewManager ()
@property(nonatomic, strong) PassThroughBackgroundView *backgroundView;
@property(nonatomic, copy) void (^dismissBlock)(void);
@property(nonatomic, copy) void (^appearBlock)(void);
@property(nonatomic, strong) NSTimer *loadTimeoutTimer;
@property(nonatomic, strong) NSDate *loadStartDate;
@property(nonatomic) BOOL hasAppeared;
@property(nonatomic) BOOL webViewClosed;
@property(nonatomic) NSInteger resourceRetryCount;
@property(nonatomic) NSTimeInterval loadTimeoutInterval;
// Non-nil exactly while a reload is scheduled but not yet fired. Single source of truth for
// "a retry is in progress"; cancellable so a load that succeeds first can cancel it.
@property(nonatomic, copy) dispatch_block_t pendingReloadBlock;
// Absolute [CLY]_content_shown deadline timer (see kCLYContentShownDeadline). Armed once at
// creation, cancelled when content_shown arrives, fires closeWebView otherwise.
@property(nonatomic, strong) NSTimer *contentShownDeadlineTimer;
@property(nonatomic, strong) CountlyWebViewController *presentingController;
@property(nonatomic, strong) CountlyOverlayWindow *window;
@end
@implementation CountlyWebViewManager
#if (TARGET_OS_IOS)
- (void)createWebViewWithURL:(NSURL *)url
frame:(CGRect)frame
appearBlock:(void(^ __nullable)(void))appearBlock
dismissBlock:(void(^ __nullable)(void))dismissBlock {
self.dismissBlock = dismissBlock;
self.appearBlock = appearBlock;
self.hasAppeared = NO;
self.webViewClosed = NO;
self.resourceRetryCount = 0;
self.pendingReloadBlock = nil;
// TODO: keyWindow deprecation fix
_window = [CountlyOverlayWindow new];
CountlyWebViewController *modal = [CountlyWebViewController new];
modal.modalPresentationStyle = UIModalPresentationOverFullScreen;
modal.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
_window.rootViewController = modal;
UIViewController *rootViewController = UIApplication.sharedApplication.keyWindow.rootViewController;
modal.modalPresentationCapturesStatusBarAppearance = YES;
CGRect backgroundFrame = rootViewController.view.bounds;
self.backgroundView = [[PassThroughBackgroundView alloc] initWithFrame:backgroundFrame];
self.backgroundView.backgroundColor = [UIColor clearColor];
self.backgroundView.hidden = YES;
modal.contentView = self.backgroundView;
_window.hidden = NO;
self.presentingController = modal;
NSString *jsString = @"(function(){"
// 1) Catch network-level failures (connection refused, DNS, etc.) for SCRIPT/LINK
"window.addEventListener('error',function(e){"
"if(!e.target)return;"
"var url=e.target.src||e.target.href;"
"if(!url)return;"
"if(url.includes('favicon.ico'))return;"
"if(e.target.tagName&&(e.target.tagName==='SCRIPT'||e.target.tagName==='LINK')){"
"window.webkit.messageHandlers.resourceLoadError.postMessage({"
"tag:e.target.tagName,"
"url:url"
"});}"
"},true);"
// 2) Catch HTTP errors (4xx/5xx) on CSS/JS via PerformanceObserver
"if(window.PerformanceObserver){"
"var obs=new PerformanceObserver(function(list){"
"list.getEntries().forEach(function(entry){"
"if(entry.responseStatus&&entry.responseStatus>=400){"
"var tag='';"
"if(entry.initiatorType==='link')tag='LINK';"
"else if(entry.initiatorType==='script')tag='SCRIPT';"
"if(tag){"
"window.webkit.messageHandlers.resourceLoadError.postMessage({"
"tag:tag,"
"url:entry.name"
"});}}"
"});"
"});"
"obs.observe({type:'resource',buffered:true});"
"}"
"})();";
WKUserContentController *contentController = [[WKUserContentController alloc] init];
WKUserScript *resourceErrorScript =
[[WKUserScript alloc] initWithSource:jsString
injectionTime:WKUserScriptInjectionTimeAtDocumentStart
forMainFrameOnly:NO];
[contentController addUserScript:resourceErrorScript];
// Opt-in (CountlyContentConfig disableZoom): prevent user zoom (pinch / double-tap) by
// enforcing the no-zoom viewport directives. Injected at document end so document.head
// exists. This PRESERVES the page's own width / initial-scale (only strips and re-adds
// maximum-scale / minimum-scale / user-scalable), so it disables zoom without changing
// the layout the content declared. The scroll-view pinch gesture is also disabled in
// configureWebView: as a native backstop.
if (CountlyContentBuilderInternal.sharedInstance.disableZoom) {
NSString *disableZoomJS =
@"(function(){"
"var m=document.querySelector('meta[name=viewport]');"
"if(m){"
"var kept=(m.content||'').split(',').map(function(s){return s.trim();}).filter(function(s){var l=s.toLowerCase();return s.length&&l.indexOf('maximum-scale')!==0&&l.indexOf('minimum-scale')!==0&&l.indexOf('user-scalable')!==0;});"
"kept.push('maximum-scale=1.0','minimum-scale=1.0','user-scalable=no');"
"m.content=kept.join(', ');"
"}else{"
"m=document.createElement('meta');m.name='viewport';"
"m.content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no';"
"(document.head||document.documentElement).appendChild(m);"
"}"
"})();";
WKUserScript *disableZoomScript =
[[WKUserScript alloc] initWithSource:disableZoomJS
injectionTime:WKUserScriptInjectionTimeAtDocumentEnd
forMainFrameOnly:YES];
[contentController addUserScript:disableZoomScript];
}
[contentController addScriptMessageHandler:self name:@"resourceLoadError"];
[contentController addScriptMessageHandler:self name:@"resourceVerifyResult"];
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.websiteDataStore = [WKWebsiteDataStore nonPersistentDataStore];
configuration.userContentController = contentController;
WKWebView *webView = [[WKWebView alloc] initWithFrame:frame configuration:configuration];
if (@available(iOS 11.0, *)) {
webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
// When SDK debug is enabled, expose the content web view to Safari Web Inspector
// (iOS 16.4+ requires this to be set explicitly). Lets you inspect the Network and
// Console tabs of the content web view live from a Mac. Off in production.
if (@available(iOS 16.4, *)) {
webView.inspectable = CountlyCommon.sharedInstance.enableDebug;
}
[self configureWebView:webView];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
[webView loadRequest:request];
// Arm the absolute content-shown deadline ONCE (not per navigation, so reloads cannot
// extend it). If [CLY]_content_shown is not reported within kCLYContentShownDeadline, the
// web view is closed regardless of retry/stall/verify state.
__weak typeof(self) weakSelf = self;
self.contentShownDeadlineTimer = [NSTimer scheduledTimerWithTimeInterval:kCLYContentShownDeadline repeats:NO block:^(NSTimer * _Nonnull timer) {
[weakSelf contentShownDeadlineReached];
}];
CLYButton *dismissButton = [CLYButton dismissAlertButton:@"X"];
[self configureDismissButton:dismissButton forWebView:webView];
self.backgroundView.webView = webView;
self.backgroundView.dismissButton = dismissButton;
}
- (void)configureWebView:(WKWebView *)webView {
webView.layer.shadowColor = UIColor.blackColor.CGColor;
webView.layer.shadowOpacity = 0.5;
webView.layer.shadowOffset = CGSizeMake(0.0f, 5.0f);
webView.layer.masksToBounds = NO;
webView.opaque = NO;
webView.scrollView.bounces = NO;
// Native backstop for the opt-in viewport zoom disable: turn off the scroll view's pinch
// gesture. Left alone (does not touch zoom scales, which interact with the page's
// initial-scale) unless disableZoom is enabled.
if (CountlyContentBuilderInternal.sharedInstance.disableZoom) {
webView.scrollView.pinchGestureRecognizer.enabled = NO;
}
webView.navigationDelegate = self;
[self.backgroundView addSubview:webView];
}
- (void)configureDismissButton:(CLYButton *)dismissButton forWebView:(WKWebView *)webView {
dismissButton.onClick = ^(id sender) {
if (self.dismissBlock) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.loadTimeoutTimer invalidate];
self.loadTimeoutTimer = nil;
self.loadStartDate = nil;
self.dismissBlock();
[self closeWebView];
});
}
};
[self.backgroundView addSubview:dismissButton];
[dismissButton positionToTopRight];
[self.backgroundView bringSubviewToFront:webView];
[webView bringSubviewToFront:dismissButton];
self.backgroundView.dismissButton = dismissButton;
dismissButton.hidden = YES;
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSString *url = navigationAction.request.URL.absoluteString;
if (!url) {
CLY_LOG_I(@"%s Navigation action with nil URL (possible proxy tunnel), allowing", __FUNCTION__);
decisionHandler(WKNavigationActionPolicyAllow);
return;
}
if ([url containsString:@"cly_x_int=1"]) {
CLY_LOG_I(@"%s Opening external url [%@]", __FUNCTION__, url);
// Routed through the app's content URL handler if one is set, else the system browser.
[self openExternalURL:navigationAction.request.URL];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
if ([url hasPrefix:@"https://countly_action_event"]) {
NSDictionary *queryParameters = [self parseQueryString:url];
if([url containsString:@"cly_x_action_event=1"]){
[self contentURLAction:queryParameters];
} else if([url containsString:@"cly_widget_command=1"]){
[self widgetURLAction:queryParameters];
}
if ([queryParameters[@"close"] boolValue]) {
[self closeWebView];
}
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
// Opens an external URL from the content. If the host app has provided a content URL handler
// (CountlyContentConfig setContentURLHandler:), the URL is offered to it first so the app can
// route its own deep link (custom scheme or https) to the right screen; the handler returns
// YES if it took over. If there is no handler, or it returns NO, the SDK opens the URL in the
// system browser as before.
- (void)openExternalURL:(NSURL *)url {
if (!url) return;
ContentURLHandler handler = CountlyContentBuilderInternal.sharedInstance.contentURLHandler;
if (handler && handler(url)) {
CLY_LOG_I(@"%s URL [%@] handled by the app's content URL handler.", __FUNCTION__, url.absoluteString);
return;
}
[UIApplication.sharedApplication openURL:url options:@{} completionHandler:^(BOOL success) {
CLY_LOG_I(@"%s URL [%@] opened in browser: %@.", __FUNCTION__, url.absoluteString, success ? @"YES" : @"NO");
}];
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
NSURLResponse *response = navigationResponse.response;
NSString *mimeType = response.MIMEType ?: @"(unknown)";
long statusCode = 0;
NSDictionary *headers = nil;
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *http = (NSHTTPURLResponse *)response;
statusCode = http.statusCode;
headers = http.allHeaderFields;
}
CLY_LOG_I(@"%s Navigation response received: URL=%@, MIME=%@, status=%ld, headers=%@", __FUNCTION__, response.URL.absoluteString, mimeType, statusCode, headers);
if (statusCode >= 400) {
CLY_LOG_I(@"%s Cancelling navigation due to HTTP status code: %ld", __FUNCTION__, statusCode);
decisionHandler(WKNavigationResponsePolicyCancel);
dispatch_async(dispatch_get_main_queue(), ^{
[self closeWebView];
});
return;
}
decisionHandler(WKNavigationResponsePolicyAllow);
}
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation {
CLY_LOG_I(@"%s Server redirect received for navigation: %@", __FUNCTION__, navigation);
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
[self.loadTimeoutTimer invalidate];
self.loadTimeoutTimer = nil;
CLY_LOG_I(@"%s Provisional navigation failed: %@ (%ld). Closing web view.", __FUNCTION__, error.localizedDescription, (long)error.code);
[self closeWebView];
}
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
CLY_LOG_I(@"%s Content started arriving (didCommitNavigation).", __FUNCTION__);
}
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
[self.loadTimeoutTimer invalidate];
self.loadTimeoutTimer = nil;
CLY_LOG_I(@"%s Navigation failed after commit: %@ (%ld). Closing web view.", __FUNCTION__, error.localizedDescription, (long)error.code);
[self closeWebView];
}
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
CLY_LOG_I(@"%s Received authentication challenge for host: %@, protectionSpace: %@", __FUNCTION__, challenge.protectionSpace.host, challenge.protectionSpace.authenticationMethod);
// sth custom if needed later?
completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
}
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView {
CLY_LOG_I(@"%s Web content process terminated for URL: %@.", __FUNCTION__, webView.URL.absoluteString);
// reload?
}
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
CLY_LOG_I(@"%s Web view has started loading", __FUNCTION__);
[self.loadTimeoutTimer invalidate];
__weak typeof(self) weakSelf = self;
// Fast stall-detect (reload) when enabled, using the configurable stall timeout;
// otherwise a plain 60s safety-net close.
NSTimeInterval stall = CountlyContentBuilderInternal.sharedInstance.contentReloadOnStallTimeout;
if (stall <= 0) stall = kCLYLoadStallTimeout; // fallback default (e.g. SDK not started via config)
NSTimeInterval timeout = CountlyContentBuilderInternal.sharedInstance.enableContentReloadOnStall ? stall : kCLYDefaultLoadTimeout;
self.loadTimeoutInterval = timeout;
self.loadTimeoutTimer = [NSTimer scheduledTimerWithTimeInterval:timeout repeats:NO block:^(NSTimer * _Nonnull timer) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if (!strongSelf) return;
[strongSelf loadDidTimeout];
}];
self.loadStartDate = [NSDate date];
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
// Don't invalidate the timeout timer here — keep it running until
// the view actually appears or resource verification completes.
// This ensures a 60s safety net even if fetch() calls hang.
if (self.webViewClosed) return;
CLY_LOG_I(@"%s Web view has finished loading", __FUNCTION__);
if (self.loadStartDate) {
NSTimeInterval loadDuration = [[NSDate date] timeIntervalSinceDate:self.loadStartDate];
CLY_LOG_I(@"%s Web view load duration: %.3f seconds", __FUNCTION__, loadDuration);
self.loadStartDate = nil;
}
[self verifyResourceStatuses:webView];
}
// After page load, fetch each CSS/JS URL with HEAD to verify HTTP status.
// Results are sent back via postMessage since evaluateJavaScript can't handle Promises.
- (void)verifyResourceStatuses:(WKWebView *)webView {
if (self.webViewClosed) return;
NSString *js =
@"(function(){"
"var urls=[];"
"document.querySelectorAll('link[rel=\"stylesheet\"]').forEach(function(l){if(l.href)urls.push({tag:'LINK',url:l.href});});"
"document.querySelectorAll('script[src]').forEach(function(s){urls.push({tag:'SCRIPT',url:s.src});});"
"if(urls.length===0){"
"window.webkit.messageHandlers.resourceVerifyResult.postMessage({results:[]});"
"return;"
"}"
"Promise.all(urls.map(function(r){"
"return fetch(r.url,{method:'HEAD'}).then(function(resp){"
"return {tag:r.tag,url:r.url,status:resp.status};"
"}).catch(function(){"
"return {tag:r.tag,url:r.url,status:0};"
"});"
"})).then(function(results){"
"window.webkit.messageHandlers.resourceVerifyResult.postMessage({results:results});"
"});"
"})()";
[webView evaluateJavaScript:js completionHandler:^(id result, NSError *error) {
if (error) {
CLY_LOG_I(@"%s Error injecting verify script: %@", __FUNCTION__, error.localizedDescription);
[self notifyPageLoaded];
}
}];
}
- (void)notifyPageLoaded {
if (self.webViewClosed || self.hasAppeared) return;
// Reaching here means the load verified good (all resources OK, or verification could
// not run). A successful load wins over a retry still pending from an earlier failure
// in this cycle: cancel it, otherwise the stale reload would wipe the now-good page and
// re-fire the page's on-load [CLY]_content_shown.
[self cancelPendingReload];
[self.loadTimeoutTimer invalidate];
self.loadTimeoutTimer = nil;
[self.presentingController updatePlacementRespectToSafeAreas];
self.hasAppeared = YES;
self.backgroundView.hidden = NO;
if (self.appearBlock) {
self.appearBlock();
}
}
// A critical (JS/CSS) resource failed to load. Rather than closing the content
// immediately, reload the web view up to kCLYMaxResourceRetries times before giving
// up. This turns a transient network hiccup (e.g. a connection stalled under a burst
// of parallel asset requests against a rate-limited / HTTP-1.1 edge) into a recoverable
// event instead of a dismissed content. Multiple failures from the same load are
// coalesced into a single reload. Must be called on the main thread.
- (void)retryOrCloseWebViewForReason:(NSString *)reason {
if (self.webViewClosed) return;
// Once the content is visible, a late resource failure must not reload or tear it
// down: the injected error listener / PerformanceObserver stay active for the whole
// page life, so a dynamically-loaded or lazy resource failing after appearance would
// otherwise re-run the page (flashing it, discarding scroll / in-progress survey
// input, and re-firing on-load analytics) or dismiss content the user is using. The
// retry mechanism only recovers failures during the initial load.
if (self.hasAppeared) {
CLY_LOG_I(@"%s %@ — content already visible, treating as non-fatal.", __FUNCTION__, reason);
return;
}
// Reload-on-failure is opt-in. When disabled, keep the original behavior: close on failure.
if (!CountlyContentBuilderInternal.sharedInstance.enableContentReloadOnStall) {
CLY_LOG_I(@"%s %@ — reload-on-stall disabled, closing web view.", __FUNCTION__, reason);
[self closeWebView];
return;
}
// A reload is already scheduled for this load cycle — coalesce further failures.
// pendingReloadBlock being non-nil is the single source of truth for "a reload is pending".
if (self.pendingReloadBlock) {
CLY_LOG_I(@"%s %@ — retry already scheduled, ignoring.", __FUNCTION__, reason);
return;
}
if (self.resourceRetryCount >= kCLYMaxResourceRetries) {
CLY_LOG_I(@"%s %@ — retries exhausted (%ld/%ld). Closing web view.", __FUNCTION__, reason, (long)self.resourceRetryCount, (long)kCLYMaxResourceRetries);
[self closeWebView];
return;
}
self.resourceRetryCount += 1;
// Cancel the in-flight stall timer: we have committed to a reload, and the reload's
// own didStartProvisionalNavigation: will arm a fresh one. Leaving the old timer live
// risks a spurious loadDidTimeout in the reload-delay window.
[self.loadTimeoutTimer invalidate];
self.loadTimeoutTimer = nil;
NSTimeInterval delay = kCLYResourceRetryBaseDelay * self.resourceRetryCount;
CLY_LOG_I(@"%s %@ — retrying load (%ld/%ld) in %.1fs.", __FUNCTION__, reason, (long)self.resourceRetryCount, (long)kCLYMaxResourceRetries, delay);
__weak typeof(self) weakSelf = self;
// Cancellable so a load that succeeds during the delay window can cancel this reload
// (see cancelPendingReload / notifyPageLoaded). Otherwise a stale reload would wipe the
// now-good page and re-fire the page's on-load [CLY]_content_shown.
dispatch_block_t reloadBlock = dispatch_block_create(0, ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
if (!strongSelf) return;
strongSelf.pendingReloadBlock = nil;
// hasAppeared / webViewClosed are belt-and-suspenders: a successful load normally
// cancels this block outright, but never reload content that already loaded/closed.
if (strongSelf.webViewClosed || strongSelf.hasAppeared) return;
WKWebView *webView = strongSelf.backgroundView.webView;
if (!webView) {
[strongSelf closeWebView];
return;
}
CLY_LOG_I(@"%s Reloading web view (retry %ld/%ld).", __FUNCTION__, (long)strongSelf.resourceRetryCount, (long)kCLYMaxResourceRetries);
[webView reload];
});
self.pendingReloadBlock = reloadBlock;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), reloadBlock);
}
// Cancel a reload scheduled by retryOrCloseWebViewForReason: that has not fired yet. Called
// when a load succeeds (notifyPageLoaded) or the view closes, so a stale reload can't reload a
// page that already loaded.
- (void)cancelPendingReload {
if (self.pendingReloadBlock) {
dispatch_block_cancel(self.pendingReloadBlock);
self.pendingReloadBlock = nil;
}
}
// The content never reported [CLY]_content_shown within kCLYContentShownDeadline. Close it,
// regardless of hasAppeared: if content_shown had arrived this timer would have been cancelled,
// so reaching here means nothing was ever actually shown (e.g. a blank-but-HTTP-200 page).
- (void)contentShownDeadlineReached {
if (self.webViewClosed) return;
CLY_LOG_I(@"%s [CLY]_content_shown not received within %.0fs; closing web view.", __FUNCTION__, kCLYContentShownDeadline);
[self closeWebView];
}
- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message
{
if (self.webViewClosed) return;
if ([message.name isEqualToString:@"resourceLoadError"]) {
NSDictionary *body = message.body;
NSString *tag = body[@"tag"];
NSString *url = body[@"url"];
CLY_LOG_I(@"%s Critical resource (%@) failed to load: [%@].", __FUNCTION__, tag, url);
NSString *reason = [NSString stringWithFormat:@"Critical resource (%@) failed to load: [%@]", tag, url];
dispatch_async(dispatch_get_main_queue(), ^{
[self retryOrCloseWebViewForReason:reason];
});
}
else if ([message.name isEqualToString:@"resourceVerifyResult"]) {
NSDictionary *body = message.body;
NSArray *results = body[@"results"];
if ([results isKindOfClass:[NSArray class]]) {
BOOL anyUnreachable = NO;
for (NSDictionary *entry in results) {
NSInteger status = [entry[@"status"] integerValue];
if (status >= 400) {
CLY_LOG_I(@"%s Critical resource (%@) returned HTTP %ld: [%@].",
__FUNCTION__, entry[@"tag"], (long)status, entry[@"url"]);
// This is the post-load HEAD verification (runs on didFinishNavigation):
// the page has already finished loading and run its on-load JS, so a
// reload from here would re-fire any analytics it recorded. Do NOT retry
// from this path. Retries are driven only by the during-load
// resourceLoadError path. Defer to an in-flight retry if one is already
// scheduled, never tear down already-visible content, otherwise close.
if (self.hasAppeared || self.pendingReloadBlock) {
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
[self closeWebView];
});
return;
}
if (status == 0) {
// HEAD could not reach the resource (network error, not an HTTP status).
anyUnreachable = YES;
}
}
// A critical resource is unreachable and reload-on-stall is enabled: the content is
// NOT verified good, so do NOT appear here. Appearing would also cancel a pending
// reload (see notifyPageLoaded), yet an unreachable resource is exactly what a
// reload recovers over the warm connection. Defer: an in-flight reload, or the
// still-running load-timeout timer, will drive the retry/close. When reload-on-stall
// is off there is no reload to protect, so keep the original show-anyway behavior.
if (anyUnreachable && !self.hasAppeared && CountlyContentBuilderInternal.sharedInstance.enableContentReloadOnStall) {
CLY_LOG_I(@"%s A critical resource is unreachable (status 0); deferring to reload instead of appearing.", __FUNCTION__);
return;
}
}
[self notifyPageLoaded];
}
}
- (void)animateView:(UIView *)view withAnimationType:(AnimationType)animationType {
NSTimeInterval animationDuration = 0;
CGAffineTransform initialTransform = CGAffineTransformIdentity;
switch (animationType) {
case AnimationTypeSlideInFromBottom:
initialTransform = CGAffineTransformMakeTranslation(0, view.superview.frame.size.height);
break;
case AnimationTypeSlideInFromTop:
initialTransform = CGAffineTransformMakeTranslation(0, -view.superview.frame.size.height);
break;
case AnimationTypeSlideInFromLeft:
initialTransform = CGAffineTransformMakeTranslation(-view.superview.frame.size.width, 0);
break;
case AnimationTypeSlideInFromRight:
initialTransform = CGAffineTransformMakeTranslation(view.superview.frame.size.width, 0);
break;
case AnimationTypeIncreaseHeight: {
CGRect originalFrame = view.frame;
view.frame = CGRectMake(originalFrame.origin.x, originalFrame.origin.y, originalFrame.size.width, 0);
[UIView animateWithDuration:animationDuration animations:^{
view.frame = originalFrame;
}];
return;
}
default:
return;
}
view.transform = initialTransform;
[UIView animateWithDuration:animationDuration animations:^{
view.transform = CGAffineTransformIdentity;
}];
}
- (void)contentURLAction:(NSDictionary *)queryParameters {
NSString *action = queryParameters[@"action"];
if(action) {
if ([action isEqualToString:@"event"]) {
NSString *eventsJson = queryParameters[@"event"];
if(eventsJson) {
[self recordEventsWithJSONString:eventsJson];
}
} else if ([action isEqualToString:@"link"]) {
NSString *link = queryParameters[@"link"];
if(link) {
[self openExternalLink:link];
}
} else if ([action isEqualToString:@"resize_me"]) {
NSString *resize = queryParameters[@"resize_me"];
if(resize) {
[self resizeWebViewWithJSONString:resize];
}
}
}
}
- (void)widgetURLAction:(NSDictionary *)queryParameters {
// none action yet
}
// The action URL is percent-decoded once here (a malformed escape falls back to the raw string so
// the action is not dropped). Two params can carry a literal '&' in their value: "link" (sent
// unencoded, may hold its own query string) and a decoded "event"/"resize_me" JSON (segmentation
// strings can contain '&'). A plain '&' split would therefore mis-slice them, and the params can
// appear in any order. Instead we span the query from the END: at each step we take the right-most
// reserved marker ("&<key>=") whose value VALIDATES for that key (event/resize_me = JSON, close =
// 0/1, action = a known verb, link = has a URI scheme), record it, and shrink the span to its left.
// A marker whose value does NOT validate is treated as ordinary text inside an enclosing value (so
// it is skipped and absorbed by an outer param). What remains at the front is the comm-url-adjacent
// identifier ("cly_x_action_event=1" / "cly_widget_command=1"), parsed verbatim. Reserved-name
// limitation (documented for integrators): a value that literally contains "&<key>=<valid-value>"
// may be mis-split.
- (NSDictionary *)parseQueryString:(NSString *)url {
NSMutableDictionary *queryDict = [NSMutableDictionary dictionary];
NSString *decodedUrl = [url stringByRemovingPercentEncoding] ?: url;
NSRange qMark = [decodedUrl rangeOfString:@"?"];
if (qMark.location == NSNotFound) {
return queryDict;
}
NSString *query = [decodedUrl substringFromIndex:qMark.location + 1];
NSArray<NSString *> *reservedKeys = @[@"action", @"event", @"resize_me", @"close", @"link"];
NSInteger end = (NSInteger)query.length;
while (end > 0) {
NSInteger chosenIdx = -1;
NSString *chosenKey = nil;
NSString *chosenValue = nil;
// Right-to-left, pick the first reserved marker whose value validates. Scanning from the
// right lets an inner (invalid) marker be absorbed into an outer, valid value.
NSInteger searchFrom = end;
while (searchFrom > 0) {
NSInteger marker = -1;
NSString *markerKey = nil;
for (NSString *key in reservedKeys) {
NSString *needle = [NSString stringWithFormat:@"&%@=", key];
NSRange r = [query rangeOfString:needle options:NSBackwardsSearch range:NSMakeRange(0, searchFrom)];
if (r.location != NSNotFound && (NSInteger)r.location > marker && (NSInteger)(r.location + needle.length) <= end) {
marker = (NSInteger)r.location;
markerKey = key;
}
}
if (marker < 0) {
break;
}
NSInteger valueStart = marker + (NSInteger)markerKey.length + 2; // "&" + key + "="
NSString *value = [query substringWithRange:NSMakeRange(valueStart, end - valueStart)];
if ([self isReservedValue:value validForKey:markerKey]) {
chosenIdx = marker;
chosenKey = markerKey;
chosenValue = value;
break;
}
// Not a real param -> ordinary text; keep looking further left.
searchFrom = marker;
}
if (chosenIdx < 0) {
break;
}
queryDict[chosenKey] = chosenValue;
end = chosenIdx;
}
// Remaining prefix is the identifier param(s) adjacent to the comm URL.
NSString *head = [query substringToIndex:end];
for (NSString *pair in [head componentsSeparatedByString:@"&"]) {
NSRange eq = [pair rangeOfString:@"="];
if (eq.location != NSNotFound) {
queryDict[[pair substringToIndex:eq.location]] = [pair substringFromIndex:eq.location + 1];
}
}
return queryDict;
}
// Validates a reserved param value. Returns NO if it does not validate (meaning the "&<key>=" was
// actually text inside an enclosing value, not a real parameter).
- (BOOL)isReservedValue:(NSString *)value validForKey:(NSString *)key {
if ([key isEqualToString:@"event"]) {
return [self isValidJSONOfClass:[NSArray class] string:value];
} else if ([key isEqualToString:@"resize_me"]) {
return [self isValidJSONOfClass:[NSDictionary class] string:value];
} else if ([key isEqualToString:@"close"]) {
return [value isEqualToString:@"0"] || [value isEqualToString:@"1"];
} else if ([key isEqualToString:@"action"]) {
return [value isEqualToString:@"event"] || [value isEqualToString:@"link"] || [value isEqualToString:@"resize_me"];
} else if ([key isEqualToString:@"link"]) {
return [self hasURIScheme:value];
}
return NO;
}
- (BOOL)isValidJSONOfClass:(Class)klass string:(NSString *)value {
NSData *data = [value dataUsingEncoding:NSUTF8StringEncoding];
if (!data) {
return NO;
}
id obj = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
return [obj isKindOfClass:klass];
}
// YES if value begins with a URI scheme (ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) ":"), covering
// http(s) URLs and custom-scheme deeplinks. The server prepends "https://" to schemeless links, so
// a valid link value always carries a scheme.
- (BOOL)hasURIScheme:(NSString *)value {
NSRange colon = [value rangeOfString:@":"];
if (colon.location == NSNotFound || colon.location == 0) {
return NO;
}
for (NSUInteger i = 0; i < colon.location; i++) {
unichar c = [value characterAtIndex:i];
BOOL ok;
if (i == 0) {
ok = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
} else {
ok = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.';
}
if (!ok) {
return NO;
}
}
return YES;
}
- (void)recordEventsWithJSONString:(NSString *)jsonString {
// The value is already percent-decoded by parseQueryString; use it directly.
NSString *decodedString = jsonString;
// Convert the decoded string to NSData
NSData *data = [decodedString dataUsingEncoding:NSUTF8StringEncoding];
// Parse the JSON data
NSError *error = nil;
NSArray *events = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (error) {
CLY_LOG_I(@"%s Error parsing JSON: %@", __FUNCTION__, error);
} else {
CLY_LOG_I(@"%s Parsed JSON: %@", __FUNCTION__, events);
}
if (!events || ![events isKindOfClass:[NSArray class]]) {
CLY_LOG_I(@"Events array should not be empty or nil, and should be of type NSArray");
return;
}
for (NSDictionary *event in events) {
NSString *key = event[@"key"];
NSDictionary *segmentation = event[@"segmentation"];
NSDictionary *sg = event[@"sg"];
if(!key) {
CLY_LOG_I(@"Skipping the event due to key is empty or nil");
continue;
}
if(sg) {
segmentation = sg;
}
if(!segmentation) {
CLY_LOG_I(@"Skipping the event due to missing segmentation");
continue;
}
// The page reported it is actually showing content: cancel the absolute
// content-shown deadline so a genuinely-displayed content is never torn down.
if ([key isEqualToString:@"[CLY]_content_shown"]) {
[self.contentShownDeadlineTimer invalidate];
self.contentShownDeadlineTimer = nil;
}
[Countly.sharedInstance recordEvent:key segmentation:segmentation];
}
[CountlyConnectionManager.sharedInstance attemptToSendStoredRequests];
}
- (void)openExternalLink:(NSString *)urlString {
NSURL *url = [NSURL URLWithString:urlString];
if (!url) {
// The decoded link may contain characters NSURL rejects (e.g. a space from a decoded '%20').
// Re-encode the illegal characters while preserving URL structure, then retry.
NSString *encoded = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
url = encoded ? [NSURL URLWithString:encoded] : nil;
}
// Prefers the app (Universal Link) when enableUniversalLinkHandling is on, else browser.
[self openExternalURL:url];
}
- (void)resizeWebViewWithJSONString:(NSString *)jsonString {
// The value is already percent-decoded by parseQueryString; use it directly.
NSString *decodedString = jsonString;
// Convert the decoded string to NSData
NSData *data = [decodedString dataUsingEncoding:NSUTF8StringEncoding];
// Parse the JSON data
NSError *error = nil;
NSDictionary *resizeDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (!resizeDict) {
CLY_LOG_I(@"Resize dictionary should not be empty or nil. Error: %@", error);
return;
}
// Ensure resizeDict is a dictionary
if (![resizeDict isKindOfClass:[NSDictionary class]]) {
CLY_LOG_I(@"Resize dictionary should be of type NSDictionary");
return;
}
// Retrieve portrait and landscape dimensions
NSDictionary *portraitDimensions = resizeDict[@"p"];
NSDictionary *landscapeDimensions = resizeDict[@"l"];
if (!portraitDimensions && !landscapeDimensions) {
CLY_LOG_I(@"Resize dimensions should not be empty or nil");
return;
}
// Determine the current orientation
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
BOOL isLandscape = UIInterfaceOrientationIsLandscape(orientation);
// Select the appropriate dimensions based on orientation
NSDictionary *dimensions = isLandscape ? landscapeDimensions : portraitDimensions;
// Get the dimension values
CGFloat x = [dimensions[@"x"] floatValue];
CGFloat y = [dimensions[@"y"] floatValue];
CGFloat width = [dimensions[@"w"] floatValue];
CGFloat height = [dimensions[@"h"] floatValue];
// Animate the resizing of the web view
[UIView animateWithDuration:0.3 animations:^{
CGRect frame = self.backgroundView.webView.frame;
frame.origin.x = x;
frame.origin.y = y;
frame.size.width = width;
frame.size.height = height;
self.backgroundView.webView.frame = frame;
[self.presentingController updatePlacementRespectToSafeAreas];
} completion:^(BOOL finished) {
CLY_LOG_I(@"%s, Resized web view to width: %f, height: %f", __FUNCTION__, width, height);
}];
}
- (void)closeWebView
{
dispatch_async(dispatch_get_main_queue(), ^{
if (!self.backgroundView.webView) {
return;
}
self.webViewClosed = YES;
self.window.hidden = YES;
self.loadStartDate = nil;
[self.loadTimeoutTimer invalidate];
self.loadTimeoutTimer = nil;
[self.contentShownDeadlineTimer invalidate];
self.contentShownDeadlineTimer = nil;
[self cancelPendingReload];
if (self.backgroundView) {
[self.backgroundView.webView stopLoading];
self.backgroundView.webView.navigationDelegate = nil;
self.backgroundView.webView.UIDelegate = nil;
[self.backgroundView removeFromSuperview];
WKUserContentController *controller = self.backgroundView.webView.configuration.userContentController;
[controller removeScriptMessageHandlerForName:@"resourceLoadError"];
[controller removeScriptMessageHandlerForName:@"resourceVerifyResult"];
}
if (self.dismissBlock) {
self.dismissBlock();
}
if(self.presentingController) {
[self.presentingController dismissViewControllerAnimated:NO completion:nil];
}
if(self.window) {
if(self.window.rootViewController) {
[self.window.rootViewController.view removeFromSuperview];
}
self.window.rootViewController = nil;
if (@available(iOS 13.0, *)) {
self.window.windowScene = nil;
}
}
self.backgroundView = nil;
self.presentingController = nil;
self.window = nil;
});
}
- (void)loadDidTimeout {
if (self.hasAppeared || self.webViewClosed) return;
CLY_LOG_I(@"%s Web view load stalled after %.1fs.", __FUNCTION__, self.loadTimeoutInterval);
// A stalled load fires no JS 'error' event, so it never reaches the resource-error
// retry path. Route it through the same retry here: reload (observed to recover)
// up to the retry cap, then close. Do NOT set webViewClosed first — that would make
// retryOrCloseWebViewForReason: bail out before it can retry.
[self retryOrCloseWebViewForReason:@"load stalled (no appearance)"];
}
#endif
@end
#endif