@@ -28,6 +28,7 @@ Licensed to the Apache Software Foundation (ASF) under one
28
28
#import " CDVWKProcessPoolFactory.h"
29
29
#import " GCDWebServer.h"
30
30
#import " GCDWebServerPrivate.h"
31
+ #import " IONAssetHandler.h"
31
32
32
33
#define CDV_BRIDGE_NAME @" cordova"
33
34
#define CDV_IONIC_STOP_SCROLL @" stopScroll"
@@ -107,6 +108,8 @@ @interface CDVWKWebViewEngine ()
107
108
@property (nonatomic , readwrite ) CGRect frame;
108
109
@property (nonatomic , strong ) NSString *userAgentCreds;
109
110
@property (nonatomic , assign ) BOOL internalConnectionsOnly;
111
+ @property (nonatomic , assign ) BOOL useScheme;
112
+ @property (nonatomic , strong ) IONAssetHandler * handler;
110
113
111
114
@property (nonatomic , readwrite ) NSString *CDV_LOCAL_SERVER;
112
115
@end
@@ -152,6 +155,13 @@ - (void)initWebServer
152
155
[GCDWebServer setLogLevel: kGCDWebServerLoggingLevel_Warning ];
153
156
self.webServer = [[GCDWebServer alloc ] init ];
154
157
158
+ [self updateBindPath ];
159
+ [self setServerPath: [self getStartPath ]];
160
+
161
+ [self startServer ];
162
+ }
163
+
164
+ -(NSString *) getStartPath {
155
165
NSString * wwwPath = [[NSBundle mainBundle ] pathForResource: @" www" ofType: nil ];
156
166
157
167
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults ];
@@ -162,11 +172,8 @@ - (void)initWebServer
162
172
NSString * snapshots = [cordovaDataDirectory stringByAppendingPathComponent: @" ionic_built_snapshots" ];
163
173
wwwPath = [snapshots stringByAppendingPathComponent: [persistedPath lastPathComponent ]];
164
174
}
165
-
166
- [self updateBindPath ];
167
- [self setServerPath: wwwPath];
168
-
169
- [self startServer ];
175
+ self.basePath = wwwPath;
176
+ return wwwPath;
170
177
}
171
178
172
179
-(BOOL ) isNewBinary
@@ -264,9 +271,22 @@ - (void)pluginInitialize
264
271
{
265
272
// viewController would be available now. we attempt to set all possible delegates to it, by default
266
273
NSDictionary * settings = self.commandDelegate .settings ;
267
- self.internalConnectionsOnly = [settings cordovaBoolSettingForKey: @" WKInternalConnectionsOnly" defaultValue: YES ];
274
+ if (@available (iOS 11.0 , *)) {
275
+ self.useScheme = [settings cordovaBoolSettingForKey: @" UseScheme" defaultValue: NO ];
276
+ } else {
277
+ self.useScheme = NO ;
278
+ }
268
279
269
- [self initWebServer ];
280
+ self.internalConnectionsOnly = [settings cordovaBoolSettingForKey: @" WKInternalConnectionsOnly" defaultValue: YES ];
281
+ if (self.useScheme ) {
282
+ NSString *bind = [settings cordovaSettingForKey: @" HostName" ];
283
+ if (bind == nil ){
284
+ bind = @" app" ;
285
+ }
286
+ self.CDV_LOCAL_SERVER = [NSString stringWithFormat: @" ionic://%@ " , bind];
287
+ } else {
288
+ [self initWebServer ];
289
+ }
270
290
271
291
self.uiDelegate = [[CDVWKWebViewUIDelegate alloc ] initWithTitle: [[NSBundle mainBundle ] objectForInfoDictionaryKey: @" CFBundleDisplayName" ]];
272
292
@@ -307,6 +327,15 @@ - (void)pluginInitialize
307
327
WKWebViewConfiguration * configuration = [self createConfigurationFromSettings: settings];
308
328
configuration.userContentController = userContentController;
309
329
330
+ if (@available (iOS 11.0 , *)) {
331
+ if (self.useScheme ) {
332
+ self.handler = [[IONAssetHandler alloc ] init ];
333
+ [self .handler setAssetPath: [self getStartPath ]];
334
+ [configuration setURLSchemeHandler: self .handler forURLScheme: @" ionic" ];
335
+ [configuration setURLSchemeHandler: self .handler forURLScheme: @" ionic-asset" ];
336
+ }
337
+ }
338
+
310
339
// re-create WKWebView, since we need to update configuration
311
340
// remove from keyWindow before recreating
312
341
[self .engineWebView removeFromSuperview ];
@@ -459,7 +488,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
459
488
if (context == KVOContext) {
460
489
if (object == [self webView ] && [keyPath isEqualToString: @" URL" ] && [object valueForKeyPath: keyPath] == nil ){
461
490
NSLog (@" URL is nil. Reloading WKWebView" );
462
- if ([self .webServer isRunning ]) {
491
+ if ([self isSafeToReload ]) {
463
492
[(WKWebView *)_engineWebView reload ];
464
493
} else {
465
494
[self loadErrorPage: nil ];
@@ -472,7 +501,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
472
501
473
502
- (void )onAppWillEnterForeground : (NSNotification *)notification {
474
503
if ([self shouldReloadWebView ]) {
475
- if ([self .webServer isRunning ]) {
504
+ if ([self isSafeToReload ]) {
476
505
NSLog (@" %@ " , @" CDVWKWebViewEngine reloading!" );
477
506
[(WKWebView *)_engineWebView reload ];
478
507
} else {
@@ -516,6 +545,11 @@ - (BOOL)shouldReloadWebView
516
545
return [self shouldReloadWebView: wkWebView.URL title: wkWebView.title];
517
546
}
518
547
548
+ - (BOOL )isSafeToReload
549
+ {
550
+ return [self .webServer isRunning ] || self.useScheme ;
551
+ }
552
+
519
553
- (BOOL )shouldReloadWebView : (NSURL *)location title : (NSString *)title
520
554
{
521
555
BOOL title_is_nil = (title == nil );
@@ -551,7 +585,7 @@ - (id)loadRequest:(NSURLRequest *)request
551
585
}
552
586
request = [NSURLRequest requestWithURL: url];
553
587
}
554
- if ([self .webServer isRunning ]) {
588
+ if ([self isSafeToReload ]) {
555
589
return [(WKWebView *)_engineWebView loadRequest: request];
556
590
} else {
557
591
return [self loadErrorPage: request];
@@ -831,7 +865,7 @@ - (void)webView:(WKWebView*)theWebView didFailNavigation:(WKNavigation*)navigati
831
865
832
866
- (void )webViewWebContentProcessDidTerminate : (WKWebView *)webView
833
867
{
834
- if ([self .webServer isRunning ]) {
868
+ if ([self isSafeToReload ]) {
835
869
[webView reload ];
836
870
} else {
837
871
[self loadErrorPage: nil ];
@@ -912,9 +946,15 @@ -(void)getServerBasePath:(CDVInvokedUrlCommand*)command
912
946
-(void )setServerBasePath : (CDVInvokedUrlCommand*)command
913
947
{
914
948
NSString * path = [command argumentAtIndex: 0 ];
915
- [self setServerPath: path];
949
+ if (self.useScheme ) {
950
+ self.basePath = path;
951
+ [self .handler setAssetPath: path];
952
+ } else {
953
+ [self setServerPath: path];
954
+ }
955
+
916
956
NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: self .CDV_LOCAL_SERVER]];
917
- if ([self .webServer isRunning ]) {
957
+ if ([self isSafeToReload ]) {
918
958
[(WKWebView *)_engineWebView loadRequest: request];
919
959
} else {
920
960
[self loadErrorPage: request];
0 commit comments