-
Notifications
You must be signed in to change notification settings - Fork 991
/
Copy pathCDVViewController.m
791 lines (656 loc) · 29 KB
/
CDVViewController.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
/*
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 <TargetConditionals.h>
#import <AVFoundation/AVFoundation.h>
#import <Foundation/Foundation.h>
#import <WebKit/WebKit.h>
#import <Cordova/CDVAppDelegate.h>
#import <Cordova/CDVPlugin.h>
#import "CDVPlugin+Private.h"
#import <Cordova/CDVConfigParser.h>
#import <Cordova/CDVSettingsDictionary.h>
#import <Cordova/CDVTimer.h>
#import "CDVCommandDelegateImpl.h"
static UIColor* defaultBackgroundColor(void) {
return UIColor.systemBackgroundColor;
}
@interface CDVViewController () <CDVWebViewEngineConfigurationDelegate> {
id <CDVWebViewEngineProtocol> _webViewEngine;
id <CDVCommandDelegate> _commandDelegate;
NSMutableDictionary<NSString *, CDVPlugin *> *_pluginObjects;
NSMutableDictionary<NSString *, NSString *> *_pluginsMap;
CDVCommandQueue* _commandQueue;
UIColor* _backgroundColor;
UIColor* _splashBackgroundColor;
CDVSettingsDictionary* _settings;
}
@property (nonatomic, readwrite, strong) NSMutableArray* startupPluginNames;
@property (nonatomic, readwrite, strong) UIView* launchView;
@property (readwrite, assign) BOOL initialized;
@end
@implementation CDVViewController
@synthesize pluginObjects = _pluginObjects;
@synthesize pluginsMap = _pluginsMap;
@synthesize commandDelegate = _commandDelegate;
@synthesize commandQueue = _commandQueue;
@synthesize webViewEngine = _webViewEngine;
@synthesize backgroundColor = _backgroundColor;
@synthesize splashBackgroundColor = _splashBackgroundColor;
@synthesize settings = _settings;
@dynamic webView;
@dynamic enumerablePlugins;
#pragma mark - Initializers
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self != nil) {
[self _cdv_init];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self != nil) {
[self _cdv_init];
}
return self;
}
- (instancetype)init
{
self = [super init];
if (self != nil) {
[self _cdv_init];
}
return self;
}
- (void)_cdv_init
{
if (!self.initialized) {
_commandQueue = [[CDVCommandQueue alloc] initWithViewController:self];
_commandDelegate = [[CDVCommandDelegateImpl alloc] initWithViewController:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppWillTerminate:)
name:UIApplicationWillTerminateNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppWillResignActive:)
name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppWillEnterForeground:)
name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onWebViewPageDidLoad:)
name:CDVPageDidLoadNotification object:nil];
// Default property values
self.configFile = @"config.xml";
self.webContentFolderName = @"www";
self.showInitialSplashScreen = YES;
// Initialize the plugin objects dict.
_pluginObjects = [[NSMutableDictionary alloc] initWithCapacity:20];
// Prevent reinitializing
self.initialized = YES;
}
}
#pragma mark -
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[_commandQueue dispose];
[self.webViewEngine loadHTMLString:@"about:blank" baseURL:nil];
@synchronized(_pluginObjects) {
[[_pluginObjects allValues] makeObjectsPerformSelector:@selector(dispose)];
[_pluginObjects removeAllObjects];
}
[self.webView removeFromSuperview];
[self.launchView removeFromSuperview];
_webViewEngine = nil;
}
#pragma mark - Getters & Setters
- (NSArray <CDVPlugin *> *)enumerablePlugins
{
@synchronized(_pluginObjects) {
return [_pluginObjects allValues];
}
}
- (NSString *)wwwFolderName
{
return self.webContentFolderName;
}
- (void)setWwwFolderName:(NSString *)name
{
self.webContentFolderName = name;
}
- (void)setBackgroundColor:(UIColor *)color
{
_backgroundColor = color ?: defaultBackgroundColor();
}
- (void)setSplashBackgroundColor:(UIColor *)color
{
_splashBackgroundColor = color ?: self.backgroundColor;
}
// Only for testing
- (void)setSettings:(CDVSettingsDictionary *)settings
{
_settings = settings;
}
- (nullable NSURL *)configFilePath
{
NSString* path = self.configFile;
// if path is relative, resolve it against the main bundle
if (![path isAbsolutePath]) {
NSString* absolutePath = [[NSBundle mainBundle] pathForResource:path ofType:nil];
if(!absolutePath){
NSAssert(NO, @"ERROR: %@ not found in the main bundle!", path);
}
path = absolutePath;
}
// Assert file exists
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
NSAssert(NO, @"ERROR: %@ does not exist.", path);
return nil;
}
return [NSURL fileURLWithPath:path];
}
- (NSURL *)appUrl
{
NSURL* appURL = nil;
if ([self.startPage rangeOfString:@"://"].location != NSNotFound) {
appURL = [NSURL URLWithString:self.startPage];
} else if ([self.webContentFolderName rangeOfString:@"://"].location != NSNotFound) {
appURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", self.webContentFolderName, self.startPage]];
} else if([self.webContentFolderName rangeOfString:@".bundle"].location != NSNotFound){
// www folder is actually a bundle
NSBundle* bundle = [NSBundle bundleWithPath:self.webContentFolderName];
appURL = [bundle URLForResource:self.startPage withExtension:nil];
} else if([self.webContentFolderName rangeOfString:@".framework"].location != NSNotFound){
// www folder is actually a framework
NSBundle* bundle = [NSBundle bundleWithPath:self.webContentFolderName];
appURL = [bundle URLForResource:self.startPage withExtension:nil];
} else {
// CB-3005 strip parameters from start page to check if page exists in resources
NSURL* startURL = [NSURL URLWithString:self.startPage];
NSString* startFilePath = [self.commandDelegate pathForResource:[startURL path]];
if (startFilePath == nil) {
appURL = nil;
} else {
appURL = [NSURL fileURLWithPath:startFilePath];
// CB-3005 Add on the query params or fragment.
NSString* startPageNoParentDirs = self.startPage;
NSRange r = [startPageNoParentDirs rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"?#"] options:0];
if (r.location != NSNotFound) {
NSString* queryAndOrFragment = [self.startPage substringFromIndex:r.location];
appURL = [NSURL URLWithString:queryAndOrFragment relativeToURL:appURL];
}
}
}
return appURL;
}
- (nullable NSURL *)errorURL
{
NSString *setting = [self.settings cordovaSettingForKey:@"ErrorUrl"];
if (setting == nil) {
return nil;
}
if ([setting rangeOfString:@"://"].location != NSNotFound) {
return [NSURL URLWithString:setting];
} else {
NSURL *url = [NSURL URLWithString:setting];
NSString *errorFilePath = [self.commandDelegate pathForResource:[url path]];
if (errorFilePath) {
return [NSURL fileURLWithPath:errorFilePath];
}
}
return nil;
}
- (nullable UIView *)webView
{
if (_webViewEngine != nil) {
return _webViewEngine.engineWebView;
}
return nil;
}
- (nullable NSString *)appURLScheme
{
NSString* URLScheme = nil;
NSArray* URLTypes = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleURLTypes"];
if (URLTypes != nil) {
NSDictionary* dict = [URLTypes objectAtIndex:0];
if (dict != nil) {
NSArray* URLSchemes = [dict objectForKey:@"CFBundleURLSchemes"];
if (URLSchemes != nil) {
URLScheme = [URLSchemes objectAtIndex:0];
}
}
}
return URLScheme;
}
#pragma mark - UIViewController & App Lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
// TODO: Remove in Cordova iOS 9
if ([UIApplication.sharedApplication.delegate isKindOfClass:[CDVAppDelegate class]]) {
CDVAppDelegate *appDelegate = (CDVAppDelegate *)UIApplication.sharedApplication.delegate;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
if (appDelegate.viewController == nil) {
appDelegate.viewController = self;
}
#pragma clang diagnostic pop
}
// Load settings
[self loadSettings];
// Instantiate the Launch screen
if (!self.launchView) {
[self createLaunchView];
}
// Instantiate the WebView
if (!self.webView) {
[self createGapView];
}
// /////////////////
if ([self.startupPluginNames count] > 0) {
[CDVTimer start:@"TotalPluginStartup"];
for (NSString* pluginName in self.startupPluginNames) {
[CDVTimer start:pluginName];
[self getCommandInstance:pluginName];
[CDVTimer stop:pluginName];
}
[CDVTimer stop:@"TotalPluginStartup"];
}
// /////////////////
NSURL* appURL = [self appUrl];
if (appURL) {
NSURLRequest* appReq = [NSURLRequest requestWithURL:appURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
[_webViewEngine loadRequest:appReq];
} else {
NSString* loadErr = [NSString stringWithFormat:@"ERROR: Start Page at '%@/%@' was not found.", self.webContentFolderName, self.startPage];
NSLog(@"%@", loadErr);
NSURL* errorUrl = [self errorURL];
if (errorUrl) {
errorUrl = [NSURL URLWithString:[NSString stringWithFormat:@"?error=%@", [loadErr stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLPathAllowedCharacterSet]] relativeToURL:errorUrl];
NSLog(@"%@", [errorUrl absoluteString]);
[_webViewEngine loadRequest:[NSURLRequest requestWithURL:errorUrl]];
} else {
NSString* html = [NSString stringWithFormat:@"<html><body> %@ </body></html>", loadErr];
[_webViewEngine loadHTMLString:html baseURL:nil];
}
}
// /////////////////
[self.webView setBackgroundColor:self.backgroundColor];
[self.launchView setBackgroundColor:self.splashBackgroundColor];
if (self.showInitialSplashScreen) {
[self.launchView setAlpha:1];
}
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVViewWillAppearNotification object:nil]];
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
#if TARGET_OS_MACCATALYST
BOOL hideTitlebar = [self.settings cordovaBoolSettingForKey:@"HideDesktopTitlebar" defaultValue:NO];
if (hideTitlebar) {
UIWindowScene *scene = self.view.window.windowScene;
if (scene) {
scene.titlebar.titleVisibility = UITitlebarTitleVisibilityHidden;
scene.titlebar.toolbar = nil;
}
} else {
// We need to fix the web content going behind the title bar
self.webView.translatesAutoresizingMaskIntoConstraints = NO;
[self.webView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor].active = YES;
[self.webView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].active = YES;
[self.webView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor].active = YES;
[self.webView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor].active = YES;
if ([self.webView respondsToSelector:@selector(scrollView)]) {
UIScrollView *scrollView = [self.webView performSelector:@selector(scrollView)];
scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
}
#endif
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVViewDidAppearNotification object:nil]];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVViewWillDisappearNotification object:nil]];
}
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVViewDidDisappearNotification object:nil]];
}
-(void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVViewWillLayoutSubviewsNotification object:nil]];
}
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVViewDidLayoutSubviewsNotification object:nil]];
}
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVViewWillTransitionToSizeNotification object:[NSValue valueWithCGSize:size]]];
}
/*
This method lets your application know that it is about to be terminated and purged from memory entirely
*/
- (void)onAppWillTerminate:(NSNotification *)notification
{
// empty the tmp directory
NSFileManager* fileMgr = [[NSFileManager alloc] init];
NSError* __autoreleasing err = nil;
// clear contents of NSTemporaryDirectory
NSString* tempDirectoryPath = NSTemporaryDirectory();
NSDirectoryEnumerator* directoryEnumerator = [fileMgr enumeratorAtPath:tempDirectoryPath];
NSString* fileName = nil;
BOOL result;
while ((fileName = [directoryEnumerator nextObject])) {
NSString* filePath = [tempDirectoryPath stringByAppendingPathComponent:fileName];
result = [fileMgr removeItemAtPath:filePath error:&err];
if (!result && err) {
NSLog(@"Failed to delete: %@ (error: %@)", filePath, err);
}
}
}
/*
This method is called to let your application know that it is about to move from the active to inactive state.
You should use this method to pause ongoing tasks, disable timer, ...
*/
- (void)onAppWillResignActive:(NSNotification *)notification
{
[self checkAndReinitViewUrl];
// NSLog(@"%@",@"applicationWillResignActive");
[self.commandDelegate evalJs:@"cordova.fireDocumentEvent('resign');" scheduledOnRunLoop:NO];
}
/*
In iOS 4.0 and later, this method is called instead of the applicationWillTerminate: method
when the user quits an application that supports background execution.
*/
- (void)onAppDidEnterBackground:(NSNotification *)notification
{
[self checkAndReinitViewUrl];
// NSLog(@"%@",@"applicationDidEnterBackground");
[self.commandDelegate evalJs:@"cordova.fireDocumentEvent('pause', null, true);" scheduledOnRunLoop:NO];
}
/*
In iOS 4.0 and later, this method is called as part of the transition from the background to the inactive state.
You can use this method to undo many of the changes you made to your application upon entering the background.
invariably followed by applicationDidBecomeActive
*/
- (void)onAppWillEnterForeground:(NSNotification *)notification
{
[self checkAndReinitViewUrl];
// NSLog(@"%@",@"applicationWillEnterForeground");
[self.commandDelegate evalJs:@"cordova.fireDocumentEvent('resume');"];
}
// This method is called to let your application know that it moved from the inactive to active state.
- (void)onAppDidBecomeActive:(NSNotification *)notification
{
[self checkAndReinitViewUrl];
// NSLog(@"%@",@"applicationDidBecomeActive");
[self.commandDelegate evalJs:@"cordova.fireDocumentEvent('active');"];
}
- (void)didReceiveMemoryWarning
{
BOOL doPurge = YES;
// iterate through all the plugin objects, and call hasPendingOperation
// if at least one has a pending operation, we don't call [super didReceiveMemoryWarning]
for (CDVPlugin *plugin in self.enumerablePlugins) {
if (plugin.hasPendingOperation) {
NSLog(@"Plugin '%@' has a pending operation, memory purge is delayed for didReceiveMemoryWarning.", NSStringFromClass([plugin class]));
doPurge = NO;
}
}
if (doPurge) {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
}
// Release any cached data, images, etc. that aren't in use.
}
/**
Show the webview and fade out the intermediary view
This is to prevent the flashing of the mainViewController
*/
- (void)onWebViewPageDidLoad:(NSNotification*)notification
{
self.webView.hidden = NO;
if ([self.settings cordovaBoolSettingForKey:@"AutoHideSplashScreen" defaultValue:YES]) {
CGFloat splashScreenDelaySetting = [self.settings cordovaFloatSettingForKey:@"SplashScreenDelay" defaultValue:0];
if (splashScreenDelaySetting == 0) {
[self showSplashScreen:NO];
} else {
// Divide by 1000 because config returns milliseconds and NSTimer takes seconds
CGFloat splashScreenDelay = splashScreenDelaySetting / 1000;
[NSTimer scheduledTimerWithTimeInterval:splashScreenDelay repeats:NO block:^(NSTimer * _Nonnull timer) {
[self showSplashScreen:NO];
}];
}
}
}
#pragma mark - View Setup
- (void)loadSettings
{
CDVConfigParser *parser = [CDVConfigParser parseConfigFile:self.configFilePath];
// Get the plugin dictionary, allowList and settings from the delegate.
_pluginsMap = parser.pluginsDict;
self.startupPluginNames = parser.startupPluginNames;
self.settings = [[CDVSettingsDictionary alloc] initWithDictionary:parser.settings];
// And the start page
if(parser.startPage && self.startPage == nil){
self.startPage = parser.startPage;
}
if (self.startPage == nil) {
self.startPage = @"index.html";
}
self.appScheme = [self.settings cordovaSettingForKey:@"Scheme"] ?: @"app";
}
/// Retrieves the view from a newwly initialized webViewEngine
/// @param bounds The bounds with which the webViewEngine will be initialized
- (nonnull UIView*)newCordovaViewWithFrame:(CGRect)bounds
{
NSString* defaultWebViewEngineClassName = [self.settings cordovaSettingForKey:@"CordovaDefaultWebViewEngine"];
NSString* webViewEngineClassName = [self.settings cordovaSettingForKey:@"CordovaWebViewEngine"];
if (!defaultWebViewEngineClassName) {
defaultWebViewEngineClassName = @"CDVWebViewEngine";
}
if (!webViewEngineClassName) {
webViewEngineClassName = defaultWebViewEngineClassName;
}
// Determine if a provided custom web view engine is sufficient
id <CDVWebViewEngineProtocol> engine;
Class customWebViewEngineClass = NSClassFromString(webViewEngineClassName);
if (customWebViewEngineClass) {
id customWebViewEngine = [self initWebViewEngine:customWebViewEngineClass bounds:bounds];
BOOL customConformsToProtocol = [customWebViewEngine conformsToProtocol:@protocol(CDVWebViewEngineProtocol)];
BOOL customCanLoad = [customWebViewEngine canLoadRequest:[NSURLRequest requestWithURL:self.appUrl]];
if (customConformsToProtocol && customCanLoad) {
engine = customWebViewEngine;
}
}
// Otherwise use the default web view engine
if (!engine) {
Class defaultWebViewEngineClass = NSClassFromString(defaultWebViewEngineClassName);
id defaultWebViewEngine = [self initWebViewEngine:defaultWebViewEngineClass bounds:bounds];
NSAssert([defaultWebViewEngine conformsToProtocol:@protocol(CDVWebViewEngineProtocol)],
@"we expected the default web view engine to conform to the CDVWebViewEngineProtocol");
engine = defaultWebViewEngine;
}
if ([engine isKindOfClass:[CDVPlugin class]]) {
[self registerPlugin:(CDVPlugin*)engine withClassName:webViewEngineClassName];
}
_webViewEngine = engine;
return _webViewEngine.engineWebView;
}
/// Initialiizes the webViewEngine, with config, if supported and provided
/// @param engineClass A class that must conform to the `CDVWebViewEngineProtocol`
/// @param bounds with which the webview will be initialized
- (id _Nullable) initWebViewEngine:(nonnull Class)engineClass bounds:(CGRect)bounds {
WKWebViewConfiguration *config = [self respondsToSelector:@selector(configuration)] ? [self configuration] : nil;
if (config && [engineClass instancesRespondToSelector:@selector(initWithFrame:configuration:)]) {
return [[engineClass alloc] initWithFrame:bounds configuration:config];
} else {
return [[engineClass alloc] initWithFrame:bounds];
}
}
- (void)createLaunchView
{
CGRect webViewBounds = self.view.bounds;
webViewBounds.origin = self.view.bounds.origin;
UIView* view = [[UIView alloc] initWithFrame:webViewBounds];
view.translatesAutoresizingMaskIntoConstraints = NO;
[view setAlpha:0];
NSString* launchStoryboardName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchStoryboardName"];
if (launchStoryboardName != nil) {
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:launchStoryboardName bundle:[NSBundle mainBundle]];
UIViewController* vc = [storyboard instantiateInitialViewController];
[self addChildViewController:vc];
UIView* imgView = vc.view;
imgView.translatesAutoresizingMaskIntoConstraints = NO;
[view addSubview:imgView];
[NSLayoutConstraint activateConstraints:@[
[NSLayoutConstraint constraintWithItem:imgView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeWidth multiplier:1 constant:0],
[NSLayoutConstraint constraintWithItem:imgView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeHeight multiplier:1 constant:0],
[NSLayoutConstraint constraintWithItem:imgView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0],
[NSLayoutConstraint constraintWithItem:imgView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]
]];
}
self.launchView = view;
[self.view addSubview:view];
[NSLayoutConstraint activateConstraints:@[
[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1 constant:0],
[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1 constant:0],
[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0],
[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]
]];
}
- (void)createGapView
{
CGRect webViewBounds = self.view.bounds;
webViewBounds.origin = self.view.bounds.origin;
UIView* view = [self newCordovaViewWithFrame:webViewBounds];
view.hidden = YES;
view.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[self.view addSubview:view];
[self.view sendSubviewToBack:view];
}
#pragma mark CordovaCommands
- (void)registerPlugin:(CDVPlugin*)plugin withClassName:(NSString*)className
{
plugin.viewController = self;
plugin.commandDelegate = _commandDelegate;
@synchronized(_pluginObjects) {
[_pluginObjects setObject:plugin forKey:className];
}
[plugin pluginInitialize];
}
- (void)registerPlugin:(CDVPlugin*)plugin withPluginName:(NSString*)pluginName
{
plugin.viewController = self;
plugin.commandDelegate = _commandDelegate;
NSString* className = NSStringFromClass([plugin class]);
@synchronized(_pluginObjects) {
[_pluginObjects setObject:plugin forKey:className];
}
[_pluginsMap setValue:className forKey:[pluginName lowercaseString]];
[plugin pluginInitialize];
}
/**
Returns an instance of a CordovaCommand object, based on its name. If one exists already, it is returned.
*/
- (nullable CDVPlugin *)getCommandInstance:(NSString *)pluginName
{
// first, we try to find the pluginName in the pluginsMap
// (acts as a allowList as well) if it does not exist, we return nil
// NOTE: plugin names are matched as lowercase to avoid problems - however, a
// possible issue is there can be duplicates possible if you had:
// "org.apache.cordova.Foo" and "org.apache.cordova.foo" - only the lower-cased entry will match
NSString* className = [_pluginsMap objectForKey:[pluginName lowercaseString]];
if (className == nil) {
return nil;
}
id obj = nil;
@synchronized(_pluginObjects) {
obj = [_pluginObjects objectForKey:className];
}
if (!obj) {
obj = [[NSClassFromString(className) alloc] initWithWebViewEngine:_webViewEngine];
if (!obj) {
NSString* fullClassName = [NSString stringWithFormat:@"%@.%@",
NSBundle.mainBundle.infoDictionary[@"CFBundleExecutable"],
className];
obj = [[NSClassFromString(fullClassName)alloc] initWithWebViewEngine:_webViewEngine];
}
if (obj != nil) {
[self registerPlugin:obj withClassName:className];
} else {
NSLog(@"CDVPlugin class %@ (pluginName: %@) does not exist.", className, pluginName);
}
}
return obj;
}
#pragma mark -
- (bool)isUrlEmpty:(NSURL *)url
{
if (!url || (url == (id) [NSNull null])) {
return true;
}
NSString *urlAsString = [url absoluteString];
return (urlAsString == (id) [NSNull null] || [urlAsString length]==0 || [urlAsString isEqualToString:@"about:blank"]);
}
- (bool)checkAndReinitViewUrl
{
NSURL* appURL = [self appUrl];
if ([self isUrlEmpty: [_webViewEngine URL]] && ![self isUrlEmpty: appURL]) {
NSURLRequest* appReq = [NSURLRequest requestWithURL:appURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
[_webViewEngine loadRequest:appReq];
return true;
}
return false;
}
#pragma mark - API Methods for Plugins
- (void)showLaunchScreen:(BOOL)visible
{
[self showSplashScreen:visible];
}
- (void)showSplashScreen:(BOOL)visible
{
CGFloat fadeSplashScreenDuration = [self.settings cordovaFloatSettingForKey:@"FadeSplashScreenDuration" defaultValue:250.f];
// Setting minimum value for fade to 0.25 seconds
fadeSplashScreenDuration = fadeSplashScreenDuration < 250 ? 250 : fadeSplashScreenDuration;
// AnimateWithDuration takes seconds but cordova documentation specifies milliseconds
CGFloat fadeDuration = fadeSplashScreenDuration/1000;
[UIView animateWithDuration:fadeDuration animations:^{
[self.launchView setAlpha:(visible ? 1 : 0)];
if (!visible) {
[self.webView becomeFirstResponder];
}
}];
}
- (void)parseSettingsWithParser:(id <NSXMLParserDelegate>)delegate
{
[CDVConfigParser parseConfigFile:self.configFilePath withDelegate:delegate];
}
@end