Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

fix cannot loading remote url bug #237

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<source-file src="src/ios/CDVWebViewUIDelegate.m"/>

<dependency id="cordova-plugin-webserver" />

<preference name="AllowsBackForwardNavigationGestures" default="false" />
</platform>

</plugin>
3 changes: 3 additions & 0 deletions src/ios/AppDelegate+WKWebViewPolyfill.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ - (void) createWindowAndStartWebServer:(BOOL) startWebServer {
// don't restart the webserver if we don't have to (fi. after a crash, see #223)
if (_webServer != nil && [_webServer isRunning]) {
[myMainViewController setServerPort:_webServer.port];
[myMainViewController showPage];
return;
}

Expand All @@ -73,6 +74,8 @@ - (void) createWindowAndStartWebServer:(BOOL) startWebServer {

// Update Swizzled ViewController with port currently used by local Server
[myMainViewController setServerPort:_webServer.port];
[myMainViewController showPage];

}

- (void)addHandlerForPath:(NSString *) path {
Expand Down
2 changes: 1 addition & 1 deletion src/ios/MyMainViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
- (void)copyLS:(unsigned short)httpPort;
- (void)setServerPort:(unsigned short) port;
- (NSURL*)fixURL:(NSString*)URL;

-(void)showPage;
@end
74 changes: 37 additions & 37 deletions src/ios/MyMainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ - (void)createGapView:(WKWebViewConfiguration*) config

self.wkWebView = [self newCordovaWKWebViewWithFrame:webViewBounds wkWebViewConfig:config];
self.wkWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
NSString* allowsBackForwardNavigationGestures = [self settingForKey:@"AllowsBackForwardNavigationGestures"];
self.wkWebView.allowsBackForwardNavigationGestures = [allowsBackForwardNavigationGestures boolValue];

/*
#ifdef __IPHONE_9_0
Expand Down Expand Up @@ -263,19 +265,7 @@ - (void)setServerPort:(unsigned short)port

- (void)viewDidLoad
{
NSURL* appURL = nil;
NSString* loadErr = nil;

if ([self.startPage rangeOfString:@"://"].location != NSNotFound) {
appURL = [NSURL URLWithString:self.startPage];
} else if ([self.wwwFolderName rangeOfString:@"://"].location != NSNotFound) {
appURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", self.wwwFolderName, self.startPage]];
}

// iOS9 (runtime) compatibility
if (IsAtLeastiOSVersion(@"9.0")) {
appURL = _startURL;
}


//// Fix the iOS 5.1 SECURITY_ERR bug (CB-347), this must be before the webView is instantiated ////
NSString* backupWebStorageType = @"cloud"; // default value
Expand Down Expand Up @@ -456,7 +446,6 @@ - (void)viewDidLoad
paginationBreakingMode = 0;
}
}

// property check for compiling under iOS < 7
ios7sel = NSSelectorFromString(@"setPaginationBreakingMode:");
if ([self.webView respondsToSelector:ios7sel]) {
Expand All @@ -478,7 +467,6 @@ - (void)viewDidLoad
paginationMode = 0;
}
}

// property check for compiling under iOS < 7
ios7sel = NSSelectorFromString(@"setPaginationMode:");
if ([self.webView respondsToSelector:ios7sel]) {
Expand All @@ -501,33 +489,45 @@ - (void)viewDidLoad

[CDVTimer stop:@"TotalPluginStartup"];
}

if(appURL != nil) {
[self loadURL:appURL];
} else if(!_targetExistsLocally) {
self.alreadyLoaded = true;
loadErr = [NSString stringWithFormat:@"ERROR: Start Page at '%@' was not found.",
self.startPage];
NSLog(@"%@", loadErr);
///////////////////
[CDVUserAgentUtil acquireLock:^(NSInteger lockToken) {
_userAgentLockToken = lockToken;

[CDVUserAgentUtil setUserAgent:self.userAgent lockToken:lockToken];
NSString* html = [NSString stringWithFormat:@"<html><body> %@ </body></html>",
loadErr];
[self.wkWebView loadHTMLString:html baseURL:nil];
}];
} else {
// we'll load once the HTTP server starts.
}

// Start timer which periodically checks whether the app is alive
if (![self settingForKey:@"DisableCrashRecovery"] || ![[self settingForKey:@"DisableCrashRecovery"] boolValue]) {
_crashRecoveryTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(recoverFromCrash) userInfo:nil repeats:YES];
}
}

-(void)showPage{
NSURL* appURL = nil;
NSString* loadErr = nil;

if ([self.startPage rangeOfString:@"://"].location != NSNotFound) {
appURL = [NSURL URLWithString:self.startPage];
} else if ([self.wwwFolderName rangeOfString:@"://"].location != NSNotFound) {
appURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", self.wwwFolderName, self.startPage]];
}

// iOS9 (runtime) compatibility
if (IsAtLeastiOSVersion(@"9.0")) {
appURL = _startURL;
}
if(appURL != nil) {
[self loadURL:appURL];
} else if(!_targetExistsLocally) {
self.alreadyLoaded = true;
loadErr = [NSString stringWithFormat:@"ERROR: Start Page at '%@' was not found.",
self.startPage];
NSLog(@"%@", loadErr);
///////////////////
[CDVUserAgentUtil acquireLock:^(NSInteger lockToken) {
_userAgentLockToken = lockToken;

[CDVUserAgentUtil setUserAgent:self.userAgent lockToken:lockToken];
NSString* html = [NSString stringWithFormat:@"<html><body> %@ </body></html>",
loadErr];
[self.wkWebView loadHTMLString:html baseURL:nil];
}];
} else {
// we'll load once the HTTP server starts.
}
}
- (void) copyLS:(unsigned short)httpPort
{
// Copy LS of UIWebView to WKWebView so upgrading to the new webview is less of a pain in the ..
Expand Down