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

Commit

Permalink
Allow port config when installing the plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Oct 14, 2015
1 parent ed94fb3 commit 8d43287
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 35 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. `<preference name="WKWebViewPluginEmbeddedServerPort" value="20000" />` 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.
Expand Down
2 changes: 1 addition & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ <h1>First page</h1>
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);
};
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
8 changes: 7 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
id="com.telerik.plugins.wkwebview"
version="0.6.3">
version="0.6.4">

<name>WKWebView Polyfill</name>

Expand All @@ -19,6 +19,8 @@

<issue>https://github.com/Telerik-Verified-Plugins/WKWebView/issues</issue>

<preference name="WKWEBVIEW_SERVER_PORT" default="12344" />

<engines>
<engine name="cordova" version=">=3.0.0"/>
</engines>
Expand Down Expand Up @@ -47,6 +49,10 @@
</dict>
</config-file>

<config-file target="*-Info.plist" parent="WKWebViewPluginEmbeddedServerPort">
<string>$WKWEBVIEW_SERVER_PORT</string>
</config-file>

<!-- For WKWebView -->
<framework src="WebKit.framework" />

Expand Down
12 changes: 10 additions & 2 deletions src/ios/AppDelegate+WKWebViewPolyfill.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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];
}
Expand Down
1 change: 1 addition & 0 deletions src/ios/MyMainViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
59 changes: 32 additions & 27 deletions src/ios/MyMainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 <video> and <audio>, and also autoplay them
*/
Expand Down Expand Up @@ -538,6 +512,37 @@ - (void)viewDidLoad
}
}

- (void) copyLS:(unsigned short)httpPort
{
// Copy LS of 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
// TODO use the port, luke!
NSString *portAsString = [NSString stringWithFormat:@"%d", httpPort];
self.wkWebViewLS = [self.wkWebViewLS stringByAppendingPathComponent:[[@"WebsiteData/LocalStorage/http_localhost_" stringByAppendingString:portAsString] stringByAppendingString:@".localstorage"]];
[[CDVLocalStorage class] copyFrom:self.uiWebViewLS to:self.wkWebViewLS error:nil];
}
}

- (WKWebView*)newCordovaWKWebViewWithFrame:(CGRect)bounds wkWebViewConfig:(WKWebViewConfiguration*) config
{
WKWebView* cordovaView = [[WKWebView alloc] initWithFrame:bounds configuration:config];
Expand Down

0 comments on commit 8d43287

Please sign in to comment.