javascript 在 Cordova 中使用带有自定义 URL 方案的 handleOpenURL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34257097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using handleOpenURL with Custom URL scheme in Cordova
提问by user3839044
I'm developing an app and have almost everything figured out, except for the custom URL scheme plugin(https://github.com/EddyVerbruggen/Custom-URL-scheme). I've successfully installed the plugin and set up a custom URL scheme of signsrestaurantandbar. So when I use signsrestaurantandbar://, my application opens. The problem I'm facing is handling the URL. In the readme, it says I can use function handleOpenURL(URL) for this, but I'm still having issues trying to load a particular page within the app.
我正在开发一个应用程序,除了自定义 URL 方案插件(https://github.com/EddyVerbruggen/Custom-URL-scheme)之外,几乎所有的东西都弄清楚了。我已经成功安装了插件并设置了 signrestaurantandbar 的自定义 URL 方案。因此,当我使用 signrestaurantandbar:// 时,我的应用程序会打开。我面临的问题是处理 URL。在自述文件中,它说我可以为此使用函数 handleOpenURL(URL),但我仍然在尝试在应用程序中加载特定页面时遇到问题。
Here's what I tried:
这是我尝试过的:
function handleOpenURL(url) {
var strValue = url;
strValue = strValue.replace('signsrestaurantandbar://','');
window.location.href = strValue + ".html";
}
I put this in my index.html page... though it should open page.html on loading signsrestaurantandbar://page, it doesn't do it properly. In my chrome console, it says it loaded the page, but it appears blank without any error and this happens only once. When I try to load signsrestaurantandbar://page the second time, it just loads the app.
我把它放在我的 index.html 页面中......虽然它应该在加载 signrestaurantandbar://page 时打开 page.html,但它没有正确执行。在我的 chrome 控制台中,它说它加载了页面,但它显示为空白,没有任何错误,而且这种情况只发生一次。当我第二次尝试加载 signrestaurantandbar://page 时,它只会加载应用程序。
I would appreciate any hints on how to approach loading particular pages using the custom URL scheme.
我很感激有关如何使用自定义 URL 方案加载特定页面的任何提示。
采纳答案by JesseMonroy650
You need to make sure you list your "custom" URL in your CSP
.
您需要确保在您的CSP
.
Added 2016-02-11: NOTE: YOUR APP IS NOW INSECURE. IT IS UP TO YOU TO SECURE YOUR APP.
添加 2016-02-11:注意:您的应用程序现在不安全。保护您的应用程序取决于您。
It would look something like this:
它看起来像这样:
<meta http-equiv="Content-Security-Policy"
content="default-src * signsrestaurantandbar:;
style-src * 'self' 'unsafe-inline' 'unsafe-eval';
script-src * 'self' 'unsafe-inline' 'unsafe-eval';">
Usually the wildcard setting (*
) can handle most applications, but not your "custom" protocol.
NOTE:Wildcard setting have the potential of keep your app out of the "app stores".
通常通配符设置 ( *
) 可以处理大多数应用程序,但不能处理您的“自定义”协议。
注意:通配符设置有可能使您的应用程序远离“应用程序商店”。
You may also need to add to your config.xml
您可能还需要添加到您的 config.xml
<allow-intent href="signsrestaurantandbar:" />
This whitelistworksheet should help. HOW TO apply the Cordova/Phonegap the whitelist system
此白名单工作表应该会有所帮助。 如何应用 Cordova/Phonegap 白名单系统
You should also read the whitelistmatrix, especially the sectionon <allow-intent (...) />
- Best of Luck
您还应该阅读白名单矩阵,尤其是关于<allow-intent (...) />
- 祝你好运的部分
回答by Pavel Chuchuva
Add global handleOpenURL function using this code:
使用以下代码添加全局 handleOpenURL 函数:
window.handleOpenURL = function(url) {
console.log(">>>>>>>>>>>>>>>>>>>");
// do stuff, for example
// document.getElementById("url").value = url;
console.log(url);
};
See Cordova Custom URL Scheme Handling.
Note that if you use alert
in this function your app would hang:
请注意,如果您alert
在此功能中使用您的应用程序将挂起:
You cannot launch any interactive features like alerts in the handleOpenURL code, if you do, your app will hang. Similarly, you should not call any Cordova APIs in there, unless you wrap it first in a setTimeout call, with a timeout value of zero.
您不能在 handleOpenURL 代码中启动任何交互式功能,例如警报,如果您这样做,您的应用程序将挂起。同样,您不应在其中调用任何 Cordova API,除非您首先将其包装在 setTimeout 调用中,并且超时值为 0。
The benefit of this method is you don't need to change Content Security Policy using Content-Security-Policy
meta tag.
这种方法的好处是您不需要使用Content-Security-Policy
元标记更改内容安全策略。