简体   繁体   English

如何在 Ionic/Angular 混合应用程序中使用cordova inappbrowser 打开链接

[英]How do I use cordova inappbrowser to open links in Ionic/Angular hybrid app

I am working on a hybrid mobile app using Ionic Framework and AngularJS.我正在使用 Ionic Framework 和 AngularJS 开发混合移动应用程序。 I am using Ionic Creator & Ionic Lab.我正在使用 Ionic Creator 和 Ionic Lab。 In my app, I have several buttons that link to external resources (webpages, mailto: links, pdf files etc).在我的应用程序中,我有几个按钮可以链接到外部资源(网页、mailto:链接、pdf 文件等)。 The links work fine when I test them within the browser (Ionic Lab).当我在浏览器(离子实验室)中测试它们时,这些链接工作正常。 However, when I test the app in an Android virtual device or an actual device, none of the links are working.但是,当我在 Android 虚拟设备或实际设备中测试该应用程序时,所有链接都不起作用。

I found a question where someone had a similar issue:我发现了一个有人遇到类似问题的问题:

/questions/33627061/ionic-simple-href-not-working-on-android-device

I tried Implementing all the proposed solutions from adding to config.xml:我尝试从添加到 config.xml 中实现所有建议的解决方案:

<allow-navigation href="*" />

Installing cordova inappbrowser plugin via:通过以下方式安装cordova inappbrowser插件:

$sudo cordova plugin add cordova-plugin-inappbrowser

I tried using ng-click="some_function()" instead of href, then defining the function in my controller.我尝试使用 ng-click="some_function()" 而不是 href,然后在我的控制器中定义函数。

The OP on that question finally resolved his issue by commenting out/uncommenting from index.html:关于该问题的 OP 最终通过从 index.html 注释掉/取消注释解决了他的问题:

<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>

Looking at my browser console logs there was a 404 not found for cordova.js, so I manually added it to www/ from the platform_www/ directory for android, as well as cordova_plugins.js查看我的浏览器控制台日志,找不到cordova.js 的404,因此我手动将其从android 的platform_www/ 目录以及cordova_plugins.js 添加到www/

However after trying all this, the links are still not working.然而,在尝试了所有这些之后,链接仍然无法正常工作。 In my consol logs I now get an error saying:在我的控制台日志中,我现在收到一条错误消息:

GET http://localhost:8100/plugins/cordova-plugin-inappbrowser/www/inappbrowser.js 404 (Not Found)
Uncaught Error: Module cordova-plugin-inappbrowser.inappbrowser does not exist. http://localhost:8100/cordova.js Line: 1421

But the thing is inappbrowser.js is in that folder!但问题是 inappbrowser.js 在那个文件夹中!

Please help!请帮忙! I'm at wits end and have no idea what to do next.我不知所措,不知道下一步该做什么。 I have uploaded the sample code I'm working with on github:我已经上传了我在 github 上使用的示例代码:

https://github.com/chmod-777/link-test https://github.com/chmod-777/link-test

Software versions:软件版本:

Linux mint 17.1 node v0.12.2 npm 2.7.4 cordova 6.2.0 ionic 1.7.14 Linux mint 17.1 node v0.12.2 npm 2.7.4 cordova 6.2.0 ionic 1.7.14

For starters, try changing the way you're calling the open method in your controller code to this:首先,尝试将您在控制器代码中调用open方法的方式更改为:

.controller('pageCtrl', function($scope, $window, $cordovaInAppBrowser) {

    $scope.open_google = function() {
          $cordovaInAppBrowser.open('https://www.yahoo.com/', '_system', 'clearcache=yes');
          // I also tried this
          //$window.open('https://www.msn.com/', '_system', 'location=yes');
          // and this
          //window.open('https://www.netflix.com/', '_system', 'location=yes');
        }

})

I have used the inappBrowser plugin with Jquery mobile.It worked fine.我在 Jquery mobile 中使用了 inappBrowser 插件。它工作正常。

                 ref=null;


                var url= accRestService.someURL;

                if( device.platform === "Android"){
                ref =cordova.InAppBrowser.open(url, "_blank",'location=no,clearcache=yes,hardwareback=no,zoom=no');
                }else{
                    ref =cordova.InAppBrowser.open(url, "_blank",'location=no,clearcache=yes,closebuttoncaption=Go back to App,toolbar=yes,presentationstyle=formsheet');
                }


                ref.addEventListener('loadstart',onBrowserLoadStart);
                ref.addEventListener('loadstop',onBrowserLoadStop);
                ref.addEventListener('loaderror', onBrowserError);
                ref.addEventListener('exit', onBrowserClose);

First, make sure that you have installed the cordova plugin "inappbrowser"首先,确保你已经安装了cordova插件“inappbrowser”

cordova plugin add cordova-plugin-inappbrowser

(see https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-inappbrowser/index.html ) (见https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-inappbrowser/index.html

Then, you can use for example :然后,您可以使用例如:

try {
    var uri = "http://some-example.com";
    cordova.InAppBrowser.open(uri, '_system');
} catch(e) {
    window.open(uri, '_blank');
}

the catch block will be used as a fallback when testing the app in a browser.在浏览器中测试应用程序时,catch 块将用作后备。

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

相关问题 PhoneGap / Cordova 2.3 .:如何在InAppBrowser中打开所有外部链接? - PhoneGap/Cordova 2.3.: how to open all external links in InAppBrowser? 如何在Cordova的InAppBrowser中优雅地中断加载某个URL并在系统浏览器中打开它? - How do I gracefully interrupt loading of a certain URL in Cordova's InAppBrowser and open it in the system browser? 如何使用InAppBrowser打开外部页面上的所有链接? - How do I open all links from an external page using InAppBrowser? 如何将Cordova InAppBrowser用于HostedWebApps? - How to use Cordova InAppBrowser into a HostedWebApps? 如何在Cordova / Ionic中打开外部应用程序? - How to open an external app in Cordova/Ionic? 如何在cordova项目中异步打开inappbrowser - how to open inappbrowser asynchronously in cordova project 角度:如何在外部浏览器中打开角度变量-Appgyver Hybrid App - Angular: How to open angular variable in external browser - appgyver hybrid app Cordova 插件 InAppBrowser 未打开链接 - Cordova plugin InAppBrowser is not opening links 如何从离子 3 中的 inAppBrowser 返回应用程序 - How to return back to app from inAppBrowser in ionic 3 使用Ionic + TypeScript + Angular的混合应用程序 - hybrid app using Ionic + TypeScript + Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM