From 8d43287609e90082e6c679f04b4444141f43e316 Mon Sep 17 00:00:00 2001 From: EddyVerbruggen Date: Wed, 14 Oct 2015 21:15:47 +0200 Subject: [PATCH] Allow port config when installing the plugin --- README.md | 9 +++- demo/index.html | 2 +- package.json | 4 +- plugin.xml | 8 +++- src/ios/AppDelegate+WKWebViewPolyfill.m | 12 ++++- src/ios/MyMainViewController.h | 1 + src/ios/MyMainViewController.m | 59 ++++++++++++++----------- 7 files changed, 60 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 7f0ff40..c4206cd 100644 --- a/README.md +++ b/README.md @@ -43,13 +43,18 @@ It's a screenshot of the [demo app](demo/index.html). From npm ``` -$ cordova plugin add @telerik/cordova-plugin-wkwebview -$ cordova prepare +$ cordova plugin add cordova-plugin-wkwebview +``` + +Specify a custom port (default 12344), you can omit this if you want to use the default in case you use a recent Cordova CLI version +``` +$ cordova plugin add cordova-plugin-wkwebview --variable WKWEBVIEW_SERVER_PORT=12344 ``` No need for anything else - you can now open the project in XCode 6 if you like. ## 4. Changelog +* __0.6.4__ On top of the port preference introduced in 0.6.3 you can now override the default variable when installing this plugin (see 'Installation'). * __0.6.3__ By default the embedded webserver uses port `12344`, but if you want to you can now override that port by setting f.i. `` in `config.xml`. * __0.6.2__ LocalStorage is copied from UIWebView to WKWebView again (iOS location was recently changed as it appears). * __0.6.1__ Allow reading files from /tmp, so the camera plugin file URI's work. Thx #155. diff --git a/demo/index.html b/demo/index.html index c1c5ae0..7484afb 100644 --- a/demo/index.html +++ b/demo/index.html @@ -81,7 +81,7 @@

First page

var proxied = window.XMLHttpRequest.prototype.open; window.XMLHttpRequest.prototype.open = function(method, url, async, user, pass) { if (method == "GET" && url.indexOf("://") == -1) { - arguments[1] = "http://localhost:12344/" + url; + arguments[1] = "http://localhost:12344/" + url; // assuming the (overridable) port is 12344 } return proxied.apply(this, arguments); }; diff --git a/package.json b/package.json index 12fbe6d..7e49302 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "version": "0.6.3", - "name": "@telerik/cordova-plugin-wkwebview", + "version": "0.6.4", + "name": "cordova-plugin-wkwebview", "cordova_name": "WKWebView Polyfill", "description": "A drop-in replacement of UIWebView for boosted performance and enhanced HTML5 support", "license": "MIT", diff --git a/plugin.xml b/plugin.xml index b6c49e0..a34bacc 100755 --- a/plugin.xml +++ b/plugin.xml @@ -1,7 +1,7 @@ + version="0.6.4"> WKWebView Polyfill @@ -19,6 +19,8 @@ https://github.com/Telerik-Verified-Plugins/WKWebView/issues + + @@ -47,6 +49,10 @@ + + $WKWEBVIEW_SERVER_PORT + + diff --git a/src/ios/AppDelegate+WKWebViewPolyfill.m b/src/ios/AppDelegate+WKWebViewPolyfill.m index cb29cbe..a57b5f3 100755 --- a/src/ios/AppDelegate+WKWebViewPolyfill.m +++ b/src/ios/AppDelegate+WKWebViewPolyfill.m @@ -63,6 +63,7 @@ - (void) createWindowAndStartWebServer:(BOOL) startWebServer { // Initialize Server startup if (startWebServer) { [self startServer]; + [myMainViewController copyLS:_webServer.port]; } // Update Swizzled ViewController with port currently used by local Server @@ -111,8 +112,15 @@ - (void)startServer // If a fixed port is passed in, use that one, otherwise use 12344. // If the port is taken though, look for a free port by adding 1 to the port until we find one. int httpPort = 12344; - - // note that the settings can be in any casing, but they are stored in lowercase + + // first we check any passed-in variable during plugin install (which is copied to plist, see plugin.xml) + NSNumber *plistPort = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"WKWebViewPluginEmbeddedServerPort"]; + if (plistPort != nil) { + httpPort = [plistPort intValue]; + } + + // now check if it was set in config.xml - this one wins if set. + // (note that the settings can be in any casing, but they are stored in lowercase) if ([self.viewController.settings objectForKey:@"wkwebviewpluginembeddedserverport"]) { httpPort = [[self.viewController.settings objectForKey:@"wkwebviewpluginembeddedserverport"] intValue]; } diff --git a/src/ios/MyMainViewController.h b/src/ios/MyMainViewController.h index ee6251e..06196a1 100755 --- a/src/ios/MyMainViewController.h +++ b/src/ios/MyMainViewController.h @@ -18,6 +18,7 @@ @property (nonatomic, readwrite, assign) BOOL alreadyLoaded; - (void)loadURL:(NSURL*)URL; +- (void)copyLS:(unsigned short)httpPort; - (void)setServerPort:(unsigned short) port; @end diff --git a/src/ios/MyMainViewController.m b/src/ios/MyMainViewController.m index c8a40dd..f7cdc2e 100755 --- a/src/ios/MyMainViewController.m +++ b/src/ios/MyMainViewController.m @@ -237,7 +237,7 @@ - (void)loadURL:(NSURL*)URL - (void)setServerPort:(unsigned short)port { - if(self.alreadyLoaded) { + if (self.alreadyLoaded) { // If we already loaded for some reason, we don't care about the local port. return; } else { @@ -326,32 +326,6 @@ - (void)viewDidLoad [self registerPlugin:[[CDVLocalStorage alloc] initWithWebView:self.webView] withClassName:NSStringFromClass([CDVLocalStorage class])]; }; - // Copy UIWebView to WKWebView so upgrading to the new webview is less of a pain in the .. - NSString* appLibraryFolder = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]; - NSString* cacheFolder; - - if ([[NSFileManager defaultManager] fileExistsAtPath:[appLibraryFolder stringByAppendingPathComponent:@"WebKit/LocalStorage/file__0.localstorage"]]) { - cacheFolder = [appLibraryFolder stringByAppendingPathComponent:@"WebKit/LocalStorage"]; - } else { - cacheFolder = [appLibraryFolder stringByAppendingPathComponent:@"Caches"]; - } - self.uiWebViewLS = [cacheFolder stringByAppendingPathComponent:@"file__0.localstorage"]; - - if (![self settingForKey:@"DisableLocalStorageSyncWithUIWebView"] || ![[self settingForKey:@"DisableLocalStorageSyncWithUIWebView"] boolValue]) { - // copy the localStorage DB of the old webview to the new one (it's copied back when the app is suspended/shut down) - self.wkWebViewLS = [[NSString alloc] initWithString: [appLibraryFolder stringByAppendingPathComponent:@"WebKit"]]; - -#if TARGET_IPHONE_SIMULATOR - // the simulutor squeezes the bundle id into the path - NSString* bundleIdentifier = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"]; - self.wkWebViewLS = [self.wkWebViewLS stringByAppendingPathComponent:bundleIdentifier]; -#endif - - // TODO if the app is ever launched on a different port.. LS can't be loaded -- not worse than the previous implementation, but still - self.wkWebViewLS = [self.wkWebViewLS stringByAppendingPathComponent:@"WebsiteData/LocalStorage/http_localhost_12344.localstorage"]; - [[CDVLocalStorage class] copyFrom:self.uiWebViewLS to:self.wkWebViewLS error:nil]; - } - /* * This is for iOS 4.x, where you can allow inline