我尝试将自定义网址发送到使用PhoneGap-Build创建的应用程序。为此,我们使用Eddy-Verbruggen的customurlscheme插件
https://github.com/EddyVerbruggen/Custom-URL-scheme
(感谢插件)。为了对此进行测试,我为zip集成了以下appfile:
config.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<widget
xmlns = "http://www.w3.org/ns/widgets"
xmlns:gap = "http://phonegap.com/ns/1.0"
id = "mysimpleapp.de"
versionCode = "002"
version = "1.0.1" >
<!-- versionCode is optional and Android only -->
<name>My Test-App</name>
<description>
The Simple Testapp
</description>
<author href="https://www.example.org" email="doe@example.org">
John Doe
</author>
<!-- PlugIns -->
<plugin name="cordova-plugin-backbutton" spec="~0.3.0" />
<plugin name="cordova-plugin-console" spec="~1.1.0" />
<plugin name="cordova-plugin-device" spec="~2.0.2" />
<plugin name="cordova-plugin-statusbar" spec="~2.4.2" />
<plugin name="cordova-plugin-whitelist" spec="~1.3.3" />
<plugin name="cordova-plugin-x-socialsharing" spec="~5.3.2" />
<plugin name="cordova-plugin-inappbrowser" spec="~3.0.0" />
<preference name="orientation" value="portrait" />
<preference name="StatusBarBackgroundColor" value="#000" />
<gap:plugin name="cordova-plugin-customurlscheme" source="npm">
<param name="URL_SCHEME" value="mysimpleapp" />
</gap:plugin>
<allow-intent href="mysimpleapp:" />
<allow-navigation href="*" />
<access origin="*" />
</widget>
和 index.html
<!DOCTYPE HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0 minimal-ui"/>
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="msapplication-TileImage" content="/mstile-144x144.png">
<meta name="theme-color" content="#ffffff">
<meta http-equiv="Content-Security-Policy"
content="default-src * mysimpleapp:;
style-src * 'self' 'unsafe-inline' 'unsafe-eval';
script-src * 'self' 'unsafe-inline' 'unsafe-eval';">
<title>My Test App</title>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript">
var handleOpenURL = function(url) {
setTimeout(function() {
alert ("handleOpenURL event raised with url: " + url);
// Number 4 in apk
}, 0);
}
var app = {
initialize: function() {
this.bindEvents();
alert("initialize event raised");
//Number 1 in apk
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
app.receivedEvent('deviceready');
// Number 2 in apk
try {
alert("onDeviceReady event raised");
}
catch(err) {
alert("Device Ready error : " + err);
}
},
receivedEvent: function() {
alert("Received Event...");
// Number 3 in apk
}
};
app.initialize();
</script>
</head>
<body>
<h1>App Test</h1>
<p>Silence is golden...</p>
</body>
现在,我使用来自外部网页的外部链接通过JavaScript来启动我的(Android)apk:
window.location = "mysimpleapp://";
我从注释中的警报中写下了订单。 我的问题是,在设备就绪事件触发之前,我不知道必须将handleOpenURL放置在何处,以便函数能够得到处理。
在某些尝试下,我尝试将应用程序的所有JavaScript代码放入如下的handleOpenUrl函数中:
function handleOpenURL(url) {
setTimeout(function() {
// my Javacript-Code
}, 500);
}
当应用程序从外部URL启动时,该方法有效。但是,如果该应用直接从我的主屏幕启动,则该事件不会触发。当我在下面放置一个自调用功能时,该自调用功能将在handleOpenURL之前触发,所以我回到了起点。