Search code examples
androidangularionic-frameworkionic2inappbrowser

Ionic InAppBrowser. Not always working


I currently have an Ionic app that creates an InAppBrowser pointing to a Website. I have deployed it to my Android phone, however there is a big issue.

Sometimes the app doesn’t work and opens the website with my default browser. Sometimes it does work and opens the website in the app using InAppBrowser.

I have tried using the different targets self, blank, system and different Android phones, but nothing fully works.

I am really confused and tried a number of things, any ideas? Thanks in advance.

Below are my code files:

home.html

<ion-header>
  <ion-navbar color="dark">
    <ion-title>
      InAppBrowser
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
  InAppBrowser Not Displayed.
</ion-content>

home.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { InAppBrowser, InAppBrowserOptions } from "@ionic-native/in-app-browser";

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public navCtrl: NavController, private inAppBrowser: InAppBrowser) {
      const options: InAppBrowserOptions = {
          toolbar: 'no',
          location: 'no',
          zoom: 'no'
      }

      const browser = this.inAppBrowser.create("https://www.awebsite.co.uk", '_self', options);
  }
}

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';

import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';

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

@NgModule({
  declarations: [
    MyApp,
    HomePage
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    InAppBrowser
  ]
})
export class AppModule {}

Solution

  • Did you try to wait the "platform" to be "ready" to use? https://ionicframework.com/docs/api/platform/Platform/

    export class HomePage {
    
      constructor(public navCtrl: NavController, private inAppBrowser: InAppBrowser, public platform: Platform) {
          const options: InAppBrowserOptions = {
              toolbar: 'no',
              location: 'no',
              zoom: 'no'
          }
    
         this.platform.ready().then( () => {
            const browser = this.inAppBrowser.create("https://www.awebsite.co.uk", '_self', options);
         })
      }
    }
    

    Doing this, the code will only run when the app is ready. In the device environment, when Cordova is ready to be used.