Skip to content

Commit

Permalink
#136 WP8 support
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Jan 17, 2016
1 parent 199c941 commit 5e66774
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-customurlscheme",
"version": "4.1.1",
"version": "4.1.2",
"description": "Launch your app by using this URL: mycoolapp://, you can add a path and even pass params like this: mycoolerapp://somepath?foo=bar",
"cordova": {
"id": "cordova-plugin-customurlscheme",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-customurlscheme"
version="4.1.1">
version="4.1.2">

<name>Custom URL scheme</name>

Expand Down
11 changes: 10 additions & 1 deletion src/wp8/CompositeUriMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@

internal class CompositeUriMapper : UriMapperBase
{
public static string launchUrl;

public override Uri MapUri(Uri uri)
{
string launchUri = uri.ToString();
if (launchUri.StartsWith("/Protocol?encodedLaunchUri="))
{
int launchUrlIndex = launchUri.IndexOf("encodedLaunchUri=");
launchUrl = System.Net.HttpUtility.UrlDecode(launchUri.Substring(launchUrlIndex+17));
return new Uri("/MainPage.xaml", UriKind.Relative);
}
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var types = assemblies.SelectMany(a =>
{
Expand All @@ -28,7 +37,7 @@ public override Uri MapUri(Uri uri)
if (mappedUri != null)
{
return mappedUri;
}
}
}
}

Expand Down
52 changes: 45 additions & 7 deletions src/wp8/hooks/add-uri-mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ module.exports = function (context) {
path = context.requireCordovaModule('path'),
projectRoot = context.opts.projectRoot;

// While on AppBuilder the above may work, the Cordova CLI doesn't like it
// While on AppBuilder this may work, the Cordova CLI doesn't like it
// (or at least not all versions of it).
var xamlFile = path.join(projectRoot, "App.xaml.cs");
var appXaml = path.join(projectRoot, "App.xaml.cs");
var mainPageXaml = path.join(projectRoot, "MainPage.xaml.cs");
try {
fs.statSync(xamlFile);
fs.statSync(appXaml);
fs.statSync(mainPageXaml);
} catch (err) {
xamlFile = path.join(projectRoot, "platforms", "wp8", "App.xaml.cs");
appXaml = path.join(projectRoot, "platforms", "wp8", "App.xaml.cs");
mainPageXaml = path.join(projectRoot, "platforms", "wp8", "MainPage.xaml.cs");
try {
fs.statSync(xamlFile);
fs.statSync(appXaml);
fs.statSync(mainPageXaml);
} catch (err2) {
console.error("Custom URL Scheme plugin couldn't find your App's xaml file! Try to adjust the file manually according to the 'add-uri-mapper.js' hook.");
return;
}
}

fs.readFile(xamlFile, 'utf8', function (err,data) {
fs.readFile(appXaml, 'utf8', function (err,data) {
if (err) {
console.error("Error while configuring the Custom URL Scheme: " + err);
deferred.reject(err);
Expand All @@ -29,7 +33,41 @@ module.exports = function (context) {
var result = data.replace(/^(\s*?)(RootFrame.NavigationFailed\s*?\+=\s*?RootFrame_NavigationFailed;)/gm,
"$1$2\n\n$1// Assign the URI-mapper class to the application frame\n$1RootFrame.UriMapper = new CompositeUriMapper();");

fs.writeFile(xamlFile, result, 'utf8', function (err) {
fs.writeFile(appXaml, result, 'utf8', function (err) {
if (err){
deferred.reject(err);
} else{
deferred.resolve();
}
});
});

fs.readFile(mainPageXaml, 'utf8', function (err,data) {
if (err) {
console.error("Error while configuring the Custom URL Scheme: " + err);
deferred.reject(err);
return;
}

// first add a line to refer to a new method
var result = data.replace(/^(\s*?)(this.CordovaView.Loaded\s*?\+=\s*?CordovaView_Loaded;)/gm,
"$1$2\n\n$1// Wire a handler so we can check for our custom scheme\n$1this.CordovaView.Browser.LoadCompleted += CordovaBrowser_LoadCompleted;");

// now add that new method
result = result.replace(/^(\s*?)(\/\/ Constructor)/gm,
"$1void CordovaBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e) {\n"+
"$1\tif (CompositeUriMapper.launchUrl != null) {\n" +
"$1\t\tstring handleOpenURL = string.Format(\"(function() {{ document.addEventListener('deviceready', function() {{ if (typeof handleOpenURL === 'function') {{ handleOpenURL(\\\"{0}\\\"); }} }}); }})()\", CompositeUriMapper.launchUrl);\n" +
"$1\t\ttry {\n" +
"$1\t\t\tthis.CordovaView.CordovaBrowser.InvokeScript(\"eval\", new string[] { handleOpenURL });\n" +
"$1\t\t} catch (Exception) {}\n" +
"$1\t\tCompositeUriMapper.launchUrl = null;\n" +
"$1\t}\n" +
"$1\tthis.CordovaView.Browser.LoadCompleted -= CordovaBrowser_LoadCompleted;\n" +
"$1}\n\n" +
"$1$2");

fs.writeFile(mainPageXaml, result, 'utf8', function (err) {
if (err){
deferred.reject(err);
} else{
Expand Down
18 changes: 10 additions & 8 deletions www/wp8/LaunchMyApp.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
(function () {
function activatedHandler(e) {
if (typeof handleOpenURL == 'function' && e.uri) {
handleOpenURL(e.uri.rawUri);
}
};

var wui = Windows.UI.WebUI.WebUIApplication;
wui.addEventListener("activated", activatedHandler, false);
function activatedHandlerWinUI(e) {
if (typeof handleOpenURL == 'function' && e.uri) {
handleOpenURL(e.uri.rawUri);
}
};

if (typeof Windows != 'undefined') {
var wui = Windows.UI.WebUI.WebUIApplication;
wui.addEventListener("activated", activatedHandlerWinUI, false);
}
}());

0 comments on commit 5e66774

Please sign in to comment.