Ionic 3-如何在设备默认浏览器中打开外部URL

3

我希望在设备的默认浏览器中打开外部URL。比如对于安卓系统应该在Chrome中打开,在iOS中应该在Safari中打开。

最初,我尝试使用window.open(url,"_system")在安卓系统中可以正常工作。但当我在iOS中尝试时,它没有起作用。

经过一些研究,我发现由于Safari的弹出窗口阻止功能,它无法打开。然后我禁用了弹出窗口阻止设置。但问题仍存在。后来我得知我需要安装InAppBrowser插件才能使其可用。

因此,我按照以下步骤进行操作:

ionic cordova plugin add cordova-plugin-inappbrowser

npm install --save @ionic-native/in-app-browser

在 app.module.ts 文件中添加了提供程序

安装插件后,在 Android 设备上应用程序崩溃了。(尚未在 iOS 上检查)。

我不知道原因。如果我删除插件,应用程序可以工作,但现在我也无法在 Android 上打开 URL。

请问有人能告诉我如何在 Android 和 iOS 中打开外部 URL 吗?

配置

1. ionic - v3

2. Angular - v6

3. cordova - v8

2个回答

2
这是一个在V3版本中对我有效的解决方案,使用了App Browser Plugin
<button ion-button block clear (click)="openWebpage(url)">Open Webpage</button>

"openWebpage" 方法的代码如下:
 openWebpage(url: string) {
        const options: InAppBrowserOptions = {
          zoom: 'no'
        }

        // Opening a URL and returning an InAppBrowserObject
        const browser = this.inAppBrowser.create(url, '_self', options);

       // Inject scripts, css and more with browser.X
      }

我对代码没有问题。每当我将代码添加到app.module.ts作为提供者时,应用程序就会崩溃。 - Flutterian

1
将此更改在 config.xml 中。
<preference name="FadeSplashScreen" value="false" />
<preference name="AutoHideSplashScreen" value="false" />

app.component.ts 中。
initializeApp() {
this.platform.ready().then(() => {
  // Okay, so the platform is ready and our plugins are available.
  // Here you can do any higher level native things you might need.
  this.statusBar.styleDefault();
  this.splashScreen.hide();
});
}

java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法“void android.widget.ImageView.setAnimation(android.view.animation.Animation)” 在org.apache.cordova.splashscreen.SplashScreen$4.run(SplashScreen.java:230)如果我移除插件,一切都正常工作,除了在浏览器中打开URL。 - Flutterian
你在哪里调用inappbrowser?是在打开应用程序时吗? - AbdulAzeem
每当我将它添加到app.module.ts作为提供者时,应用程序就会崩溃。 - Flutterian
你是在哪里调用inappbrowser的?是在启动画面出现后吗?我认为问题可能出现在启动画面和第一个页面之间。 - AbdulAzeem

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接