简体   繁体   English

Ionic 4 - InAppBrowser 不使用“_blank” - 页面无法加载

[英]Ionic 4 - InAppBrowser not working with '_blank' - Page doesn't load

I've completed an Ionic 4 app which utilizes the InAppBrowser and it works fine on iOS device when the target is _self but when the target is _blank the InAppBrowser attempts to load the web page but remains on a white screen.我已经完成了一个使用 InAppBrowser 的 Ionic 4 应用程序,当目标是 _self 时,它在 iOS 设备上运行良好,但当目标是 _blank 时,InAppBrowser 尝试加载网页,但仍停留在白屏上。

I've installed我已经安装了

 ionic cordova plugin add cordova-plugin-inappbrowser
 npm install @ionic-native/in-app-browser

I've imported on the corresponding ts page and in app.module.ts我已经在相应的 ts 页面和 app.module.ts 中导入

import { InAppBrowser } from '@ionic-native/in-app-browser/ngx';

I've declared it as a provider in app.module.ts我已在 app.module.ts 中将其声明为提供者

providers: [
InAppBrowser,
StatusBar,
SplashScreen,
CallNumber,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],

Here is the code这是代码

const browser = this.iab.create('https://www.google.com', '_blank');
browser.show();

I remember this error specifically for ios devices.我记得这个错误专门针对 ios 设备。 The cause for me was the use of localhost:XXXX instead of a valid url where XXXX is the port number.我的原因是使用localhost:XXXX而不是有效的 URL,其中 XXXX 是端口号。 This would happen if there is no application running on your XXXX port.如果您的 XXXX 端口上没有运行应用程序,就会发生这种情况。

What I realised was on iOS, when you use an invalid URL like localhost:XXXX the inappbrowser is unable to render the page with 404 error.我意识到在 iOS 上,当您使用像 localhost:XXXX 这样的无效 URL 时, inappbrowser无法呈现带有 404 错误的页面。 It just fails to show anything :(. On an android device you would get a 404 page saying, URL not found. But with iOS this doesnot happen. Took me a while to figure out.它只是没有显示任何内容:(。在 Android 设备上,您会看到一个 404 页面,说找不到 URL。但在 iOS 中,这不会发生。我花了一段时间才弄清楚。

Coming to your problem, if you are getting this issue with a valid working URL then check if you are connected to the internet.谈到您的问题,如果您使用有效的工作 URL 遇到此问题,请检查您是否已连接到互联网。

Although the problem persists in the Ionic Dev App, it does not occur in the final app build which I've tested via TestFlight.尽管问题在 Ionic Dev App 中仍然存在,但在我通过 TestFlight 测试的最终应用程序版本中并没有出现。 So it appears that this is simply an Ionic Dev App issue.所以看起来这只是一个 Ionic Dev App 问题。

As at today 26th Feb, 2021. using ionic 6. I encountered this problem but I found out this problem is occurring because I have built my project before adding cordova InAppBrowser plugin.截至今天 2021 年 2 月 26 日。使用 ionic 6。我遇到了这个问题,但我发现这个问题正在发生,因为我在添加cordova InAppBrowser 插件之前已经构建了我的项目。

my solution我的解决方案

  1. open your project directory.打开你的项目目录。 locate folder android (or iOS depending on the platform your building) and folder platform.找到文件夹 android(或 iOS,具体取决于您构建的平台)和文件夹平台。

  2. make a copy of the two folders listed above somewhere outside the project directory or you just simply rename them.在项目目录之外的某处复制上面列出的两个文件夹,或者您只需重命名它们。

  3. Open your CLI and navigate to project directory then run the following code in order.打开您的 CLI 并导航到项目目录,然后按顺序运行以下代码。

    1. npm run build

    2. ionic capacitor add android

    3. ionic capacitor copy android && cd android && gradlew assembleDebug && cd ..

您不需要添加以下代码browser.show();

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

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