@@ -28,6 +28,7 @@ Licensed to the Apache Software Foundation (ASF) under one
2828#import " CDVWKProcessPoolFactory.h"
2929#import " GCDWebServer.h"
3030#import " GCDWebServerPrivate.h"
31+ #import " IONAssetHandler.h"
3132
3233#define CDV_BRIDGE_NAME @" cordova"
3334#define CDV_IONIC_STOP_SCROLL @" stopScroll"
@@ -107,6 +108,8 @@ @interface CDVWKWebViewEngine ()
107108@property (nonatomic , readwrite ) CGRect frame;
108109@property (nonatomic , strong ) NSString *userAgentCreds;
109110@property (nonatomic , assign ) BOOL internalConnectionsOnly;
111+ @property (nonatomic , assign ) BOOL useScheme;
112+ @property (nonatomic , strong ) IONAssetHandler * handler;
110113
111114@property (nonatomic , readwrite ) NSString *CDV_LOCAL_SERVER;
112115@end
@@ -152,6 +155,13 @@ - (void)initWebServer
152155 [GCDWebServer setLogLevel: kGCDWebServerLoggingLevel_Warning ];
153156 self.webServer = [[GCDWebServer alloc ] init ];
154157
158+ [self updateBindPath ];
159+ [self setServerPath: [self getStartPath ]];
160+
161+ [self startServer ];
162+ }
163+
164+ -(NSString *) getStartPath {
155165 NSString * wwwPath = [[NSBundle mainBundle ] pathForResource: @" www" ofType: nil ];
156166
157167 NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults ];
@@ -162,11 +172,8 @@ - (void)initWebServer
162172 NSString * snapshots = [cordovaDataDirectory stringByAppendingPathComponent: @" ionic_built_snapshots" ];
163173 wwwPath = [snapshots stringByAppendingPathComponent: [persistedPath lastPathComponent ]];
164174 }
165-
166- [self updateBindPath ];
167- [self setServerPath: wwwPath];
168-
169- [self startServer ];
175+ self.basePath = wwwPath;
176+ return wwwPath;
170177}
171178
172179-(BOOL ) isNewBinary
@@ -264,9 +271,22 @@ - (void)pluginInitialize
264271{
265272 // viewController would be available now. we attempt to set all possible delegates to it, by default
266273 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+ }
268279
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+ }
270290
271291 self.uiDelegate = [[CDVWKWebViewUIDelegate alloc ] initWithTitle: [[NSBundle mainBundle ] objectForInfoDictionaryKey: @" CFBundleDisplayName" ]];
272292
@@ -307,6 +327,15 @@ - (void)pluginInitialize
307327 WKWebViewConfiguration * configuration = [self createConfigurationFromSettings: settings];
308328 configuration.userContentController = userContentController;
309329
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+
310339 // re-create WKWebView, since we need to update configuration
311340 // remove from keyWindow before recreating
312341 [self .engineWebView removeFromSuperview ];
@@ -459,7 +488,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
459488 if (context == KVOContext) {
460489 if (object == [self webView ] && [keyPath isEqualToString: @" URL" ] && [object valueForKeyPath: keyPath] == nil ){
461490 NSLog (@" URL is nil. Reloading WKWebView" );
462- if ([self .webServer isRunning ]) {
491+ if ([self isSafeToReload ]) {
463492 [(WKWebView *)_engineWebView reload ];
464493 } else {
465494 [self loadErrorPage: nil ];
@@ -472,7 +501,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
472501
473502- (void )onAppWillEnterForeground : (NSNotification *)notification {
474503 if ([self shouldReloadWebView ]) {
475- if ([self .webServer isRunning ]) {
504+ if ([self isSafeToReload ]) {
476505 NSLog (@" %@ " , @" CDVWKWebViewEngine reloading!" );
477506 [(WKWebView *)_engineWebView reload ];
478507 } else {
@@ -516,6 +545,11 @@ - (BOOL)shouldReloadWebView
516545 return [self shouldReloadWebView: wkWebView.URL title: wkWebView.title];
517546}
518547
548+ - (BOOL )isSafeToReload
549+ {
550+ return [self .webServer isRunning ] || self.useScheme ;
551+ }
552+
519553- (BOOL )shouldReloadWebView : (NSURL *)location title : (NSString *)title
520554{
521555 BOOL title_is_nil = (title == nil );
@@ -551,7 +585,7 @@ - (id)loadRequest:(NSURLRequest *)request
551585 }
552586 request = [NSURLRequest requestWithURL: url];
553587 }
554- if ([self .webServer isRunning ]) {
588+ if ([self isSafeToReload ]) {
555589 return [(WKWebView *)_engineWebView loadRequest: request];
556590 } else {
557591 return [self loadErrorPage: request];
@@ -831,7 +865,7 @@ - (void)webView:(WKWebView*)theWebView didFailNavigation:(WKNavigation*)navigati
831865
832866- (void )webViewWebContentProcessDidTerminate : (WKWebView *)webView
833867{
834- if ([self .webServer isRunning ]) {
868+ if ([self isSafeToReload ]) {
835869 [webView reload ];
836870 } else {
837871 [self loadErrorPage: nil ];
@@ -912,9 +946,15 @@ -(void)getServerBasePath:(CDVInvokedUrlCommand*)command
912946-(void )setServerBasePath : (CDVInvokedUrlCommand*)command
913947{
914948 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+
916956 NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: self .CDV_LOCAL_SERVER]];
917- if ([self .webServer isRunning ]) {
957+ if ([self isSafeToReload ]) {
918958 [(WKWebView *)_engineWebView loadRequest: request];
919959 } else {
920960 [self loadErrorPage: request];
0 commit comments