简体   繁体   English

Cordova-通过远程HTML访问设备功能

[英]Cordova - Access device functionality from remote HTML

I have a Cordova application that waits for the deviceready event and, when that event fires, sets the window.location.href to a remote site, say, http://foo.bar.com/lander.html . 我有一个Cordova应用程序,它等待deviceready事件,并在该事件触发时,将window.location.href设置为远程站点,例如http://foo.bar.com/lander.html From that page, I have several links to other pages - say page1.html and page2.html . 在该页面上,我有多个指向其他页面的链接-例如page1.htmlpage2.html I want to put a button in page1.html that calls Cordova's navigator.notification.vibrate function. 我想在page1.html中放置一个按钮,该按钮调用Cordova的navigator.notification.vibrate函数。

I have the above set up and everything appears to work, except that navigation.notification is undefined when I click the button in page1.html to try to get the device to vibrate. 我已经完成上述设置,并且一切正常,但单击page1.html的按钮以尝试使设备振动时, navigation.notification未定义。 I have copied cordova.js onto foo.bar.com and am referencing it from page1.html . 我已经将cordova.js复制到foo.bar.com并从page1.html引用它。 Do I have to include other javascript source files on foo.bar.com ? 我是否必须在foo.bar.com上包含其他JavaScript源文件? Will this approach ever work? 这种方法行得通吗?

Would it work if instead of changing the window location I were to get the content by AJAX and put the content in a div container in the index.html page that underlies the Cordova app? 如果我不是通过更改窗口位置而是通过AJAX获取内容并将内容放入Cordova应用程序所在的index.html页的div容器中,是否可以工作?

This is all on iOS if that matters. 如果重要的话,这些都在iOS上。 I included the dialogs and vibration plugins only. 我仅包括dialogsvibration插件。 I am receiving the deviceready event in the Cordova app and successfully redirecting to the remote site. 我在Cordova应用程序中收到deviceready事件,并成功重定向到了远程站点。 Please let me know if there are other factors which I need to take into consideration. 请让我知道是否还有其他需要考虑的因素。 Thanks. 谢谢。

UPDATE 1: I got vibration working from the Cordova app's local index.html by using navigator.vibrate instead of navigator.notification.vibrate. 更新1:通过使用navigator.vibrate而不是navigator.notification.vibrate,从Cordova应用程序的本地index.html获得了振动。 Changing this on the remote site did not help. 在远程站点上更改此设置无济于事。 I also tried including copies of vibration.js, Cordova.js and cordova_plugins.js from the remote page - still no luck. 我还尝试了从远程页面包含振动.js,Cordova.js和cordova_plugins.js的副本-仍然没有运气。

UPDATE 2: I found in the Cordova docs where what I'm asking about is discussed: Invoking Cordova JavaScript functions from a remotely-loaded HTML page (an HTML page not stored locally on the device) is an unsupported configuration. 更新2:我发现在Cordova文档中讨论了我要问的问题:从远程加载的HTML页面(不在设备本地存储的HTML页面)调用Cordova JavaScript函数是不受支持的配置。 This is because Cordova was not designed for this, and the Apache Cordova community does no testing of this configuration. 这是因为Cordova不是为此设计的,并且Apache Cordova社区未对此配置进行测试。 While it can work in some circumstances, it is not recommended nor supported. 尽管它在某些情况下可以工作,但不建议也不支持。 There are challenges with the same origin policy, keeping the JavaScript and native portions of Cordova synchronized at the same version (since they are coupled via private APIs which may change), the trustworthiness of remote content calling native local functions, and potential app store rejection. 相同的原始策略存在挑战,要使JavaScript和Cordova的本机部分保持相同版本的同步(因为它们通过可能更改的专用API耦合),调用本机本地功能的远程内容的可信度以及潜在的应用商店拒绝。

The display of remotely-loaded HTML content in a webview should be done using Cordova's InAppBrowser. 应使用Cordova的InAppBrowser在Web视图中显示远程加载的HTML内容。 The InAppBrowser is designed so that JavaScript running there does not have access to the Cordova JavaScript APIs for the reasons listed above. InAppBrowser的设计使得在上面运行的JavaScript不能访问Cordova JavaScript API。 Please refer to the Security Guide. 请参阅安全指南。

Based on this, it sounds possible but like we'd be out on our own. 基于此,听起来可能可行,但就像我们会独自一人。 Still, if anybody is successfully doing this, we'd like to give it a try. 不过,如果有人成功完成此操作,我们还是想尝试一下。

I thinhk what you are looking for is a push notification. 我thinhk您正在寻找的是推送通知。 There is a plugin for this. 有一个插件。 http://ngcordova.com/docs/plugins/pushNotifications/ You need a task running on your app though. http://ngcordova.com/docs/plugins/pushNotifications/尽管您需要在应用程序上运行任务。

Hope this is helpfull. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM