简体   繁体   English

cordova inappbrowser 无法在 iOS 上打开外部 url

[英]cordova inappbrowser cannot open an external url on iOS

I'm developing an hybrid app in phonegap and basically I have a button in the index page which whenever is clicked opens up an external url.我正在 phonegap 中开发一个混合应用程序,基本上我在索引页面中有一个按钮,只要点击它就会打开一个外部 url。 I succeed on Android and fail on iOS.我在 Android 上成功,在 iOS 上失败。

I've looked up these questions iOS / Cordova: InAppBrowser not working - cordova/phonegap onclick doesn't work - PhoneGap Build: how to open external url in device browser on Android?了这些问题iOS/Cordova:InAppBrowser 不工作 -cordova /phonegap onclick 不起作用 -PhoneGap Build:如何在 Android 上的设备浏览器中打开外部 url? - phonegap open link in browser but unfortunately none of these questions helped me to fix the issue. - phonegap 在浏览器中打开链接,但不幸的是,这些问题都没有帮助我解决问题。


First hypothesis could be an error message alert : The error message on the iPhone where I'm testing my app is: [ERROR] Error initializing Cordova: Missing Command Error .第一个假设可能是错误消息警报:我正在测试我的应用程序的 iPhone 上的错误消息是: [ERROR] Error initializing Cordova: Missing Command Error

Second hypothesis I'm doing something wrong with the onclick event of the window.open method because when I click the open button on the iOS test device nothing happens.第二个假设我对window.open方法的onclick事件做错了,因为当我单击 iOS 测试设备上的打开按钮时,没有任何反应。

Here is the code:这是代码:

 document.getElementById("openBrowser").addEventListener("click", function(){ var ref = window.open('http://www.espn.com', '_self', 'location=yes'); ref.addEventListener('loadstart', function(event) { alert('start: ' + event.url); }); ref.addEventListener('loadstop', function(event) { alert('stop: ' + event.url); }); ref.addEventListener('loaderror', function(event) { alert('error: ' + event.message); }); ref.addEventListener('exit', function(event) { alert(event.type); }); });
 <button id="openBrowser">APRI</button>


As you can see the above piece of code works and works on the google ripple emulator too and as I said earlier at the beginning of my question it works also on Android (link to the android web app https://play.google.com/store/apps/details?id=com.phonegap.unoxtutti&hl=it ) As a matter of fact I'm not sure what problem may be behind this malfunctioning either the former or the latter hypothesis.正如您所看到的,上面的代码段也可以在谷歌涟漪模拟器上运行和运行,正如我之前在我的问题开头所说,它也可以在 Android 上运行(链接到 android 网络应用程序https://play.google.com /store/apps/details?id=com.phonegap.unoxtutti&hl=it ) 事实上,我不确定前一个或后一个假设出现故障的背后可能有什么问题。

Besides the cordova plugin inappbrowser is in config.xml除了cordova插件inappbrowser在config.xml中

    <plugin name="org.apache.cordova.inappbrowser" />

I'm really unable to find the bug but I hope to have provided you the necessary information to help me to fix this seemingly insolvable problem我真的无法找到错误,但我希望为您提供必要的信息来帮助我解决这个看似无法解决的问题

The test environmemts are Google Ripple Emulator ,Phonegap Desktop App and the Phonegap Mobile App .测试环境是Google Ripple EmulatorPhonegap Desktop AppPhonegap Mobile App The desktop app creates the directory of the project while the mobile app and the google ripple emulator are connected to the desktop app and are my test environments.桌面应用程序创建项目的目录,而移动应用程序和谷歌涟漪模拟器连接到桌面应用程序,是我的测试环境。 Last but not least on the ripple emulator the external url successfully opens up while it doesn't on the mobile app and the initialization error throws when the app is opened.最后但并非最不重要的是,在涟漪模拟器上,外部 url 成功打开,而在移动应用程序上却没有,并且在打开应用程序时会引发initialization error

After very long time spent in googling the web I made InAppBrowser open an external url.在谷歌搜索网络上花费了很长时间后,我让 InAppBrowser 打开了一个外部 url。 this is the SO answer who solved my bug 这是解决我的错误的 SO 答案

and this is the code of the answer in the link这是链接中答案的代码

 document.addEventListener('deviceready', onDeviceReady, false); function onDeviceReady() { // Mock device.platform property if not available if (!window.device) { window.device = { platform: 'Browser' }; } handleExternalURLs(); } function handleExternalURLs() { // Handle click events for all external URLs if (device.platform.toUpperCase() === 'ANDROID') { $(document).on('click', 'a[href^="http"]', function (e) { var url = $(this).attr('href'); navigator.app.loadUrl(url, { openExternal: true }); e.preventDefault(); }); } else if (device.platform.toUpperCase() === 'IOS') { $(document).on('click', 'a[href^="http"]', function (e) { var url = $(this).attr('href'); window.open(url, '_system'); e.preventDefault(); }); } else { // Leave standard behaviour } }
 <a href="http://stackoverflow.com">

Basically all I'm doing in the above snippet is to check whether the device is iOS or Android and depending on the device in use I use a different method.基本上我在上面的代码片段中所做的就是检查设备是 iOS 还是 Android,并根据使用的设备使用不同的方法。 This works on iOS Android and in a browser.这适用于 iOS Android 和浏览器。 I really hope this answer will help as many people as possible.我真的希望这个答案能帮助尽可能多的人。

Device Plugin and InAppBrowser Plugin are required else this code fails需要设备插件和 InAppBrowser 插件,否则此代码失败

After spending time in googling the web, I fixed the InAppBrowser open an external URL.花时间在谷歌上搜索网络后,我修复了 InAppBrowser 打开一个外部 URL。 Basically, There is bug in InAppBrowser plugin for ios platform.基本上,ios 平台的 InAppBrowser 插件存在错误。 when you click the link, The Alert appears on iPhone.当您单击该链接时,警报会出现在 iPhone 上。 I fixed the bug by using the older version of InAppbrowser plugin.我使用旧版本的 InAppbrowser 插件修复了该错误。 now it is working perfectly.现在它工作得很好。

@Riccardo Go through phonegap documentation for this add your code snippet on, onDeviceReady and try it. @Riccardo 通过 phonegap 文档添加您的代码片段 onDeviceReady 并尝试它。 InAppBrowser 应用程序浏览器

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

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