-
Notifications
You must be signed in to change notification settings - Fork 394
/
Copy pathCDVWKWebViewEngine.m
838 lines (699 loc) · 33.6 KB
/
CDVWKWebViewEngine.m
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
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
#import <Cordova/NSDictionary+CordovaPreferences.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>
#import <objc/message.h>
#import <objc/runtime.h>
#import "CDVWKWebViewEngine.h"
#import "CDVWKWebViewUIDelegate.h"
#import "CDVWKProcessPoolFactory.h"
#import "IONAssetHandler.h"
#define CDV_BRIDGE_NAME @"cordova"
#define CDV_IONIC_STOP_SCROLL @"stopScroll"
#define CDV_SERVER_PATH @"serverBasePath"
#define LAST_BINARY_VERSION_CODE @"lastBinaryVersionCode"
#define LAST_BINARY_VERSION_NAME @"lastBinaryVersionName"
@implementation UIScrollView (BugIOS11)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL originalSelector = @selector(init);
SEL swizzledSelector = @selector(xxx_init);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod =
class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
#pragma mark - Method Swizzling
- (id)xxx_init {
id a = [self xxx_init];
NSArray *stack = [NSThread callStackSymbols];
for(NSString *trace in stack) {
if([trace containsString:@"WebKit"]) {
[a setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
break;
}
}
return a;
}
@end
@interface CDVWKWeakScriptMessageHandler : NSObject <WKScriptMessageHandler>
@property (nonatomic, weak, readonly) id<WKScriptMessageHandler>scriptMessageHandler;
- (instancetype)initWithScriptMessageHandler:(id<WKScriptMessageHandler>)scriptMessageHandler;
@end
@interface CDVWKWebViewEngine ()
@property (nonatomic, strong, readwrite) UIView* engineWebView;
@property (nonatomic, strong, readwrite) id <WKUIDelegate> uiDelegate;
@property (nonatomic, weak) id <WKScriptMessageHandler> weakScriptMessageHandler;
@property (nonatomic, readwrite) CGRect frame;
@property (nonatomic, strong) NSString *userAgentCreds;
@property (nonatomic, strong) IONAssetHandler * handler;
@property (nonatomic, readwrite) NSString *CDV_LOCAL_SERVER;
@end
// expose private configuration value required for background operation
@interface WKWebViewConfiguration ()
@end
// see forwardingTargetForSelector: selector comment for the reason for this pragma
#pragma clang diagnostic ignored "-Wprotocol"
@implementation CDVWKWebViewEngine
@synthesize engineWebView = _engineWebView;
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super init];
if (self) {
if (NSClassFromString(@"WKWebView") == nil) {
return nil;
}
// add to keyWindow to ensure it is 'active'
[UIApplication.sharedApplication.keyWindow addSubview:self.engineWebView];
self.frame = frame;
}
return self;
}
-(NSString *) getStartPath {
NSString * wwwPath = [[NSBundle mainBundle] pathForResource:@"www" ofType: nil];
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSString * persistedPath = [userDefaults objectForKey:CDV_SERVER_PATH];
if (![self isDeployDisabled] && ![self isNewBinary] && persistedPath && ![persistedPath isEqualToString:@""]) {
NSString *libPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString * cordovaDataDirectory = [libPath stringByAppendingPathComponent:@"NoCloud"];
NSString * snapshots = [cordovaDataDirectory stringByAppendingPathComponent:@"ionic_built_snapshots"];
wwwPath = [snapshots stringByAppendingPathComponent:[persistedPath lastPathComponent]];
}
self.basePath = wwwPath;
return wwwPath;
}
-(BOOL) isNewBinary
{
NSString * versionCode = [[NSBundle mainBundle] infoDictionary][@"CFBundleVersion"];
NSString * versionName = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults];
NSString * lastVersionCode = [prefs stringForKey:LAST_BINARY_VERSION_CODE];
NSString * lastVersionName = [prefs stringForKey:LAST_BINARY_VERSION_NAME];
if (![versionCode isEqualToString:lastVersionCode] || ![versionName isEqualToString:lastVersionName]) {
[prefs setObject:versionCode forKey:LAST_BINARY_VERSION_CODE];
[prefs setObject:versionName forKey:LAST_BINARY_VERSION_NAME];
[prefs setObject:@"" forKey:CDV_SERVER_PATH];
[prefs synchronize];
return YES;
}
return NO;
}
-(BOOL) isDeployDisabled {
return [[self.commandDelegate.settings objectForKey:[@"DisableDeploy" lowercaseString]] boolValue];
}
- (WKWebViewConfiguration*) createConfigurationFromSettings:(NSDictionary*)settings
{
WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init];
configuration.processPool = [[CDVWKProcessPoolFactory sharedFactory] sharedProcessPool];
configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
if (settings == nil) {
return configuration;
}
if(![settings cordovaBoolSettingForKey:@"WKSuspendInBackground" defaultValue:YES]){
NSString* _BGStatus;
if (@available(iOS 12.2, *)) {
// do stuff for iOS 12.2 and newer
NSLog(@"iOS 12.2+ detected");
NSString* str = @"YWx3YXlzUnVuc0F0Rm9yZWdyb3VuZFByaW9yaXR5";
NSData* data = [[NSData alloc] initWithBase64EncodedString:str options:0];
_BGStatus = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
} else {
// do stuff for iOS 12.1 and older
NSLog(@"iOS Below 12.2 detected");
NSString* str = @"X2Fsd2F5c1J1bnNBdEZvcmVncm91bmRQcmlvcml0eQ==";
NSData* data = [[NSData alloc] initWithBase64EncodedString:str options:0];
_BGStatus = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
[configuration setValue:[NSNumber numberWithBool:YES]
forKey:_BGStatus];
}
NSString *userAgent = configuration.applicationNameForUserAgent;
if (
[settings cordovaSettingForKey:@"OverrideUserAgent"] == nil &&
[settings cordovaSettingForKey:@"AppendUserAgent"] != nil
) {
userAgent = [NSString stringWithFormat:@"%@ %@", userAgent, [settings cordovaSettingForKey:@"AppendUserAgent"]];
}
configuration.applicationNameForUserAgent = userAgent;
configuration.allowsInlineMediaPlayback = [settings cordovaBoolSettingForKey:@"AllowInlineMediaPlayback" defaultValue:YES];
configuration.suppressesIncrementalRendering = [settings cordovaBoolSettingForKey:@"SuppressesIncrementalRendering" defaultValue:NO];
configuration.allowsAirPlayForMediaPlayback = [settings cordovaBoolSettingForKey:@"MediaPlaybackAllowsAirPlay" defaultValue:YES];
return configuration;
}
- (void)pluginInitialize
{
// viewController would be available now. we attempt to set all possible delegates to it, by default
NSDictionary* settings = self.commandDelegate.settings;
NSString *bind = [settings cordovaSettingForKey:@"Hostname"];
if(bind == nil){
bind = @"localhost";
}
NSString *scheme = [settings cordovaSettingForKey:@"iosScheme"];
if(scheme == nil || [scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"] || [scheme isEqualToString:@"file"]){
scheme = @"ionic";
}
self.CDV_LOCAL_SERVER = [NSString stringWithFormat:@"%@://%@", scheme, bind];
self.uiDelegate = [[CDVWKWebViewUIDelegate alloc] initWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]];
CDVWKWeakScriptMessageHandler *weakScriptMessageHandler = [[CDVWKWeakScriptMessageHandler alloc] initWithScriptMessageHandler:self];
WKUserContentController* userContentController = [[WKUserContentController alloc] init];
[userContentController addScriptMessageHandler:weakScriptMessageHandler name:CDV_BRIDGE_NAME];
[userContentController addScriptMessageHandler:weakScriptMessageHandler name:CDV_IONIC_STOP_SCROLL];
// Inject XHR Polyfill
NSLog(@"CDVWKWebViewEngine: trying to inject XHR polyfill");
WKUserScript *wkScript = [self wkPluginScript];
if (wkScript) {
[userContentController addUserScript:wkScript];
}
WKUserScript *configScript = [self configScript];
if (configScript) {
[userContentController addUserScript:configScript];
}
BOOL autoCordova = [settings cordovaBoolSettingForKey:@"AutoInjectCordova" defaultValue:NO];
if (autoCordova){
NSLog(@"CDVWKWebViewEngine: trying to inject XHR polyfill");
WKUserScript *cordova = [self autoCordovify];
if (cordova) {
[userContentController addUserScript:cordova];
}
}
BOOL audioCanMix = [settings cordovaBoolSettingForKey:@"AudioCanMix" defaultValue:NO];
if (audioCanMix) {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionMixWithOthers
error:nil];
}
WKWebViewConfiguration* configuration = [self createConfigurationFromSettings:settings];
configuration.userContentController = userContentController;
self.handler = [[IONAssetHandler alloc] initWithBasePath:[self getStartPath] andScheme:scheme];
[configuration setURLSchemeHandler:self.handler forURLScheme:scheme];
// re-create WKWebView, since we need to update configuration
// remove from keyWindow before recreating
[self.engineWebView removeFromSuperview];
WKWebView* wkWebView = [[WKWebView alloc] initWithFrame:self.frame configuration:configuration];
[wkWebView.scrollView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
wkWebView.UIDelegate = self.uiDelegate;
self.engineWebView = wkWebView;
// add to keyWindow to ensure it is 'active'
[UIApplication.sharedApplication.keyWindow addSubview:self.engineWebView];
NSString * overrideUserAgent = [settings cordovaSettingForKey:@"OverrideUserAgent"];
if (overrideUserAgent != nil) {
wkWebView.customUserAgent = overrideUserAgent;
}
if ([self.viewController conformsToProtocol:@protocol(WKUIDelegate)]) {
wkWebView.UIDelegate = (id <WKUIDelegate>)self.viewController;
}
if ([self.viewController conformsToProtocol:@protocol(WKNavigationDelegate)]) {
wkWebView.navigationDelegate = (id <WKNavigationDelegate>)self.viewController;
} else {
wkWebView.navigationDelegate = (id <WKNavigationDelegate>)self;
}
if ([self.viewController conformsToProtocol:@protocol(WKScriptMessageHandler)]) {
[wkWebView.configuration.userContentController addScriptMessageHandler:(id < WKScriptMessageHandler >)self.viewController name:CDV_BRIDGE_NAME];
}
[self keyboardDisplayDoesNotRequireUserAction];
if ([settings cordovaBoolSettingForKey:@"KeyboardAppearanceDark" defaultValue:NO]) {
[self setKeyboardAppearanceDark];
}
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 160400
// With the introduction of iOS 16.4 the webview is no longer inspectable by default.
// We'll honor that change for release builds, but will still allow inspection on debug builds by default.
// We also introduce an override option, so consumers can influence this decision in their own build.
if (@available(iOS 16.4, *)) {
#ifdef DEBUG
BOOL allowWebviewInspectionDefault = YES;
#else
BOOL allowWebviewInspectionDefault = NO;
#endif
wkWebView.inspectable = [settings cordovaBoolSettingForKey:@"InspectableWebview" defaultValue:allowWebviewInspectionDefault];
}
#endif
[self updateSettings:settings];
// check if content thread has died on resume
NSLog(@"%@", @"CDVWKWebViewEngine will reload WKWebView if required on resume");
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onAppWillEnterForeground:)
name:UIApplicationWillEnterForegroundNotification object:nil];
// If less than ios 13.4
if (@available(iOS 13.4, *)) {} else {
// For keyboard dismissal leaving viewport shifted (can potentially be removed when apple releases the fix for the issue discussed here: https://github.com/apache/cordova-ios/issues/417#issuecomment-423340885)
// Apple has released a fix in 13.4, but not in 12.x (as of 12.4.6)
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWillHide)
name:UIKeyboardWillHideNotification object:nil];
}
NSLog(@"Using Ionic WKWebView");
}
// https://github.com/Telerik-Verified-Plugins/WKWebView/commit/04e8296adeb61f289f9c698045c19b62d080c7e3#L609-L620
- (void) keyboardDisplayDoesNotRequireUserAction {
Class class = NSClassFromString(@"WKContentView");
NSOperatingSystemVersion iOS_11_3_0 = (NSOperatingSystemVersion){11, 3, 0};
NSOperatingSystemVersion iOS_12_2_0 = (NSOperatingSystemVersion){12, 2, 0};
NSOperatingSystemVersion iOS_13_0_0 = (NSOperatingSystemVersion){13, 0, 0};
char * methodSignature = "_startAssistingNode:userIsInteracting:blurPreviousNode:changingActivityState:userObject:";
if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: iOS_13_0_0]) {
methodSignature = "_elementDidFocus:userIsInteracting:blurPreviousNode:activityStateChanges:userObject:";
} else if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: iOS_12_2_0]) {
methodSignature = "_elementDidFocus:userIsInteracting:blurPreviousNode:changingActivityState:userObject:";
}
if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: iOS_11_3_0]) {
SEL selector = sel_getUid(methodSignature);
Method method = class_getInstanceMethod(class, selector);
IMP original = method_getImplementation(method);
IMP override = imp_implementationWithBlock(^void(id me, void* arg0, BOOL arg1, BOOL arg2, BOOL arg3, id arg4) {
((void (*)(id, SEL, void*, BOOL, BOOL, BOOL, id))original)(me, selector, arg0, TRUE, arg2, arg3, arg4);
});
method_setImplementation(method, override);
} else {
SEL selector = sel_getUid("_startAssistingNode:userIsInteracting:blurPreviousNode:userObject:");
Method method = class_getInstanceMethod(class, selector);
IMP original = method_getImplementation(method);
IMP override = imp_implementationWithBlock(^void(id me, void* arg0, BOOL arg1, BOOL arg2, id arg3) {
((void (*)(id, SEL, void*, BOOL, BOOL, id))original)(me, selector, arg0, TRUE, arg2, arg3);
});
method_setImplementation(method, override);
}
}
- (void)setKeyboardAppearanceDark
{
IMP darkImp = imp_implementationWithBlock(^(id _s) {
return UIKeyboardAppearanceDark;
});
for (NSString* classString in @[@"WKContentView", @"UITextInputTraits"]) {
Class c = NSClassFromString(classString);
Method m = class_getInstanceMethod(c, @selector(keyboardAppearance));
if (m != NULL) {
method_setImplementation(m, darkImp);
} else {
class_addMethod(c, @selector(keyboardAppearance), darkImp, "l@:");
}
}
}
- (void)onAppWillEnterForeground:(NSNotification *)notification {
if ([self shouldReloadWebView]) {
NSLog(@"%@", @"CDVWKWebViewEngine reloading!");
[(WKWebView*)_engineWebView reload];
}
}
-(void)keyboardWillHide
{
// For keyboard dismissal leaving viewport shifted (can potentially be removed when apple releases the fix for the issue discussed here: https://github.com/apache/cordova-ios/issues/417#issuecomment-423340885)
UIScrollView * scrollView = self.webView.scrollView;
// Calculate some vars for convenience
CGFloat contentLengthWithInsets = scrollView.contentSize.height + scrollView.adjustedContentInset.top + scrollView.adjustedContentInset.bottom;
CGFloat contentOffsetY = scrollView.contentOffset.y;
CGFloat screenHeight = scrollView.frame.size.height;
CGFloat maxAllowedOffsetY = fmax(contentLengthWithInsets - screenHeight, 0); // 0 is for the case where content is shorter than screen
// If the keyboard allowed the user to get to an offset beyond the max
if (contentOffsetY > maxAllowedOffsetY) {
// Reset the scroll to the max allowed so that there is no additional empty white space at the bottom where the keyboard occupied!
CGPoint bottomOfPage = CGPointMake(scrollView.contentOffset.x, maxAllowedOffsetY);
[scrollView setContentOffset:bottomOfPage];
}
}
- (BOOL)shouldReloadWebView
{
WKWebView* wkWebView = (WKWebView*)_engineWebView;
return [self shouldReloadWebView:wkWebView.URL title:wkWebView.title];
}
- (BOOL)shouldReloadWebView:(NSURL *)location title:(NSString*)title
{
BOOL title_is_nil = (title == nil);
BOOL location_is_blank = [[location absoluteString] isEqualToString:@"about:blank"];
BOOL reload = (title_is_nil || location_is_blank);
#ifdef DEBUG
NSLog(@"%@", @"CDVWKWebViewEngine shouldReloadWebView::");
NSLog(@"CDVWKWebViewEngine shouldReloadWebView title: %@", title);
NSLog(@"CDVWKWebViewEngine shouldReloadWebView location: %@", [location absoluteString]);
NSLog(@"CDVWKWebViewEngine shouldReloadWebView reload: %u", reload);
#endif
return reload;
}
- (id)loadRequest:(NSURLRequest *)request
{
if (request.URL.fileURL) {
NSURL* startURL = [NSURL URLWithString:((CDVViewController *)self.viewController).startPage];
NSString* startFilePath = [self.commandDelegate pathForResource:[startURL path]];
NSURL *url = [[NSURL URLWithString:self.CDV_LOCAL_SERVER] URLByAppendingPathComponent:request.URL.path];
if ([request.URL.path isEqualToString:startFilePath]) {
url = [NSURL URLWithString:self.CDV_LOCAL_SERVER];
}
if(request.URL.query) {
url = [NSURL URLWithString:[@"?" stringByAppendingString:request.URL.query] relativeToURL:url];
}
if(request.URL.fragment) {
url = [NSURL URLWithString:[@"#" stringByAppendingString:request.URL.fragment] relativeToURL:url];
}
request = [NSURLRequest requestWithURL:url];
}
return [(WKWebView*)_engineWebView loadRequest:request];
}
- (id)loadHTMLString:(NSString *)string baseURL:(NSURL*)baseURL
{
return [(WKWebView*)_engineWebView loadHTMLString:string baseURL:baseURL];
}
- (NSURL*) URL
{
return [(WKWebView*)_engineWebView URL];
}
- (BOOL)canLoadRequest:(NSURLRequest *)request
{
return TRUE;
}
- (void)updateSettings:(NSDictionary *)settings
{
WKWebView* wkWebView = (WKWebView *)_engineWebView;
// By default, DisallowOverscroll is false (thus bounce is allowed)
BOOL bounceAllowed = !([settings cordovaBoolSettingForKey:@"DisallowOverscroll" defaultValue:NO]);
// prevent webView from bouncing
if (!bounceAllowed) {
if ([wkWebView respondsToSelector:@selector(scrollView)]) {
((UIScrollView*)[wkWebView scrollView]).bounces = NO;
} else {
for (id subview in wkWebView.subviews) {
if ([[subview class] isSubclassOfClass:[UIScrollView class]]) {
((UIScrollView*)subview).bounces = NO;
}
}
}
}
wkWebView.configuration.preferences.minimumFontSize = [settings cordovaFloatSettingForKey:@"MinimumFontSize" defaultValue:0.0];
wkWebView.allowsLinkPreview = [settings cordovaBoolSettingForKey:@"AllowLinkPreview" defaultValue:NO];
wkWebView.scrollView.scrollEnabled = [settings cordovaBoolSettingForKey:@"ScrollEnabled" defaultValue:NO];
wkWebView.allowsBackForwardNavigationGestures = [settings cordovaBoolSettingForKey:@"AllowBackForwardNavigationGestures" defaultValue:NO];
}
- (void)updateWithInfo:(NSDictionary *)info
{
NSDictionary* scriptMessageHandlers = [info objectForKey:kCDVWebViewEngineScriptMessageHandlers];
NSDictionary* settings = [info objectForKey:kCDVWebViewEngineWebViewPreferences];
id navigationDelegate = [info objectForKey:kCDVWebViewEngineWKNavigationDelegate];
id uiDelegate = [info objectForKey:kCDVWebViewEngineWKUIDelegate];
WKWebView* wkWebView = (WKWebView*)_engineWebView;
if (scriptMessageHandlers && [scriptMessageHandlers isKindOfClass:[NSDictionary class]]) {
NSArray* allKeys = [scriptMessageHandlers allKeys];
for (NSString* key in allKeys) {
id object = [scriptMessageHandlers objectForKey:key];
if ([object conformsToProtocol:@protocol(WKScriptMessageHandler)]) {
[wkWebView.configuration.userContentController addScriptMessageHandler:object name:key];
}
}
}
if (navigationDelegate && [navigationDelegate conformsToProtocol:@protocol(WKNavigationDelegate)]) {
wkWebView.navigationDelegate = navigationDelegate;
}
if (uiDelegate && [uiDelegate conformsToProtocol:@protocol(WKUIDelegate)]) {
wkWebView.UIDelegate = uiDelegate;
}
if (settings && [settings isKindOfClass:[NSDictionary class]]) {
[self updateSettings:settings];
}
}
// This forwards the methods that are in the header that are not implemented here.
// loadHTMLString:baseURL:
// loadRequest:
- (id)forwardingTargetForSelector:(SEL)aSelector
{
return _engineWebView;
}
- (UIView *)webView
{
return self.engineWebView;
}
- (WKUserScript *)wkPluginScript
{
NSString *scriptFile = [[NSBundle mainBundle] pathForResource:@"www/wk-plugin" ofType:@"js"];
if (scriptFile == nil) {
NSLog(@"CDVWKWebViewEngine: WK plugin was not found");
return nil;
}
NSError *error = nil;
NSString *source = [NSString stringWithContentsOfFile:scriptFile encoding:NSUTF8StringEncoding error:&error];
if (source == nil || error != nil) {
NSLog(@"CDVWKWebViewEngine: WK plugin can not be loaded: %@", error);
return nil;
}
source = [source stringByAppendingString:[NSString stringWithFormat:@"window.WEBVIEW_SERVER_URL = '%@';", self.CDV_LOCAL_SERVER]];
return [[WKUserScript alloc] initWithSource:source
injectionTime:WKUserScriptInjectionTimeAtDocumentStart
forMainFrameOnly:YES];
}
- (WKUserScript *)configScript
{
Class keyboard = NSClassFromString(@"CDVIonicKeyboard");
BOOL keyboardPlugin = keyboard != nil;
if(!keyboardPlugin) {
return nil;
}
BOOL keyboardResizes = [self.commandDelegate.settings cordovaBoolSettingForKey:@"KeyboardResize" defaultValue:YES];
NSString *source = [NSString stringWithFormat:
@"window.Ionic = window.Ionic || {};"
@"window.Ionic.keyboardPlugin=true;"
@"window.Ionic.keyboardResizes=%@",
keyboardResizes ? @"true" : @"false"];
return [[WKUserScript alloc] initWithSource:source
injectionTime:WKUserScriptInjectionTimeAtDocumentStart
forMainFrameOnly:YES];
}
- (WKUserScript *)autoCordovify
{
NSURL *cordovaURL = [[NSBundle mainBundle] URLForResource:@"www/cordova" withExtension:@"js"];
if (cordovaURL == nil) {
NSLog(@"CDVWKWebViewEngine: cordova.js WAS NOT FOUND");
return nil;
}
NSError *error = nil;
NSString *source = [NSString stringWithContentsOfURL:cordovaURL encoding:NSUTF8StringEncoding error:&error];
if (source == nil || error != nil) {
NSLog(@"CDVWKWebViewEngine: cordova.js can not be loaded: %@", error);
return nil;
}
NSLog(@"CDVWKWebViewEngine: auto injecting cordova");
NSString *cordovaPath = [self.CDV_LOCAL_SERVER stringByAppendingString:cordovaURL.URLByDeletingLastPathComponent.path];
NSString *replacement = [NSString stringWithFormat:@"var pathPrefix = '%@/';", cordovaPath];
source = [source stringByReplacingOccurrencesOfString:@"var pathPrefix = findCordovaPath();" withString:replacement];
return [[WKUserScript alloc] initWithSource:source
injectionTime:WKUserScriptInjectionTimeAtDocumentStart
forMainFrameOnly:YES];
}
#pragma mark WKScriptMessageHandler implementation
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
if ([message.name isEqualToString:CDV_BRIDGE_NAME]) {
[self handleCordovaMessage: message];
} else if ([message.name isEqualToString:CDV_IONIC_STOP_SCROLL]) {
[self handleStopScroll];
}
}
- (void)handleCordovaMessage:(WKScriptMessage*)message
{
CDVViewController *vc = (CDVViewController*)self.viewController;
NSArray *jsonEntry = message.body; // NSString:callbackId, NSString:service, NSString:action, NSArray:args
CDVInvokedUrlCommand* command = [CDVInvokedUrlCommand commandFromJson:jsonEntry];
CDV_EXEC_LOG(@"Exec(%@): Calling %@.%@", command.callbackId, command.className, command.methodName);
if (![vc.commandQueue execute:command]) {
#ifdef DEBUG
NSError* error = nil;
NSString* commandJson = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonEntry
options:0
error:&error];
if (error == nil) {
commandJson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
static NSUInteger maxLogLength = 1024;
NSString* commandString = ([commandJson length] > maxLogLength) ?
[NSString stringWithFormat : @"%@[...]", [commandJson substringToIndex:maxLogLength]] :
commandJson;
NSLog(@"FAILED pluginJSON = %@", commandString);
#endif
}
}
- (void)handleStopScroll
{
WKWebView* wkWebView = (WKWebView*)_engineWebView;
NSLog(@"CDVWKWebViewEngine: handleStopScroll");
[self recursiveStopScroll:[wkWebView scrollView]];
[wkWebView evaluateJavaScript:@"window.IonicStopScroll.fire()" completionHandler:nil];
}
- (void)recursiveStopScroll:(UIView *)node
{
if([node isKindOfClass: [UIScrollView class]]) {
UIScrollView *nodeAsScroll = (UIScrollView *)node;
if([nodeAsScroll isScrollEnabled] && ![nodeAsScroll isHidden]) {
[nodeAsScroll setScrollEnabled: NO];
[nodeAsScroll setScrollEnabled: YES];
}
}
// iterate tree recursivelly
for (UIView *child in [node subviews]) {
[self recursiveStopScroll:child];
}
}
#pragma mark WKNavigationDelegate implementation
- (void)webView:(WKWebView*)webView didStartProvisionalNavigation:(WKNavigation*)navigation
{
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginResetNotification object:webView]];
}
- (void)webView:(WKWebView*)webView didFinishNavigation:(WKNavigation*)navigation
{
#ifndef __CORDOVA_6_0_0
CDVViewController* vc = (CDVViewController*)self.viewController;
[CDVUserAgentUtil releaseLock:vc.userAgentLockToken];
#endif
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPageDidLoadNotification object:webView]];
}
- (void)webView:(WKWebView*)theWebView didFailProvisionalNavigation:(WKNavigation*)navigation withError:(NSError*)error
{
[self webView:theWebView didFailNavigation:navigation withError:error];
}
- (void)webView:(WKWebView*)theWebView didFailNavigation:(WKNavigation*)navigation withError:(NSError*)error
{
CDVViewController* vc = (CDVViewController*)self.viewController;
#ifndef __CORDOVA_6_0_0
[CDVUserAgentUtil releaseLock:vc.userAgentLockToken];
#endif
NSString* message = [NSString stringWithFormat:@"Failed to load webpage with error: %@", [error localizedDescription]];
NSLog(@"%@", message);
NSURL* errorUrl = vc.errorURL;
if (errorUrl) {
NSCharacterSet *charSet = [NSCharacterSet URLFragmentAllowedCharacterSet];
errorUrl = [NSURL URLWithString:[NSString stringWithFormat:@"?error=%@", [message stringByAddingPercentEncodingWithAllowedCharacters:charSet]] relativeToURL:errorUrl];
NSLog(@"%@", [errorUrl absoluteString]);
[theWebView loadRequest:[NSURLRequest requestWithURL:errorUrl]];
}
#ifdef DEBUG
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"] message:message preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:nil]];
[vc presentViewController:alertController animated:YES completion:nil];
#endif
}
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView
{
[webView reload];
}
- (BOOL)defaultResourcePolicyForURL:(NSURL*)url
{
// all file:// urls are allowed
if ([url isFileURL]) {
return YES;
}
return NO;
}
- (void) webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
NSURL* url = [navigationAction.request URL];
CDVViewController* vc = (CDVViewController*)self.viewController;
/*
* Give plugins the chance to handle the url
*/
BOOL anyPluginsResponded = NO;
BOOL shouldAllowRequest = NO;
for (NSString* pluginName in vc.pluginObjects) {
CDVPlugin* plugin = [vc.pluginObjects objectForKey:pluginName];
SEL selector = NSSelectorFromString(@"shouldOverrideLoadWithRequest:navigationType:");
if ([plugin respondsToSelector:selector]) {
anyPluginsResponded = YES;
// https://issues.apache.org/jira/browse/CB-12497
int navType = (int)navigationAction.navigationType;
if (WKNavigationTypeOther == navigationAction.navigationType) {
#ifdef __CORDOVA_6_0_0
navType = -1;
#else
navType = 5;
#endif
}
shouldAllowRequest = (((BOOL (*)(id, SEL, id, int))objc_msgSend)(plugin, selector, navigationAction.request, navType));
if (!shouldAllowRequest) {
break;
}
}
}
if (!anyPluginsResponded) {
/*
* Handle all other types of urls (tel:, sms:), and requests to load a url in the main webview.
*/
shouldAllowRequest = [self defaultResourcePolicyForURL:url];
if (!shouldAllowRequest) {
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
}
}
if (shouldAllowRequest) {
NSString *scheme = url.scheme;
if ([scheme isEqualToString:@"tel"] ||
[scheme isEqualToString:@"mailto"] ||
[scheme isEqualToString:@"facetime"] ||
[scheme isEqualToString:@"sms"] ||
[scheme isEqualToString:@"maps"]) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
decisionHandler(WKNavigationActionPolicyCancel);
} else {
decisionHandler(WKNavigationActionPolicyAllow);
}
} else {
decisionHandler(WKNavigationActionPolicyCancel);
}
}
-(void)getServerBasePath:(CDVInvokedUrlCommand*)command
{
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:self.basePath] callbackId:command.callbackId];
}
-(void)setServerBasePath:(CDVInvokedUrlCommand*)command
{
NSString * path = [command argumentAtIndex:0];
self.basePath = path;
[self.handler setAssetPath:path];
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.CDV_LOCAL_SERVER]];
[(WKWebView*)_engineWebView loadRequest:request];
}
-(void)persistServerBasePath:(CDVInvokedUrlCommand*)command
{
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:[self.basePath lastPathComponent] forKey:CDV_SERVER_PATH];
[userDefaults synchronize];
}
@end
#pragma mark - CDVWKWeakScriptMessageHandler
@implementation CDVWKWeakScriptMessageHandler
- (instancetype)initWithScriptMessageHandler:(id<WKScriptMessageHandler>)scriptMessageHandler
{
self = [super init];
if (self) {
_scriptMessageHandler = scriptMessageHandler;
}
return self;
}
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
[self.scriptMessageHandler userContentController:userContentController didReceiveScriptMessage:message];
}
@end