0
votes

I installed the inAppBrowser plugin for Ionic Cordova (at least I think I installed it correctly. I followed the directions here: https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-inappbrowser/#installation).

I imported inAppBrowser into my app.module.ts file and added it to the list of providers:

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

and

providers: [
StatusBar,
SplashScreen,
InAppBrowser,
{provide: ErrorHandler, useClass: IonicErrorHandler}  
]

I also injected it into the relevant pages like this:

export class AboutPage {

options : InAppBrowserOptions = {
    location : 'yes',
    hidden : 'no',
    clearcache : 'yes',
    clearsessioncache : 'yes',
    zoom : 'yes',//Android only
    hardwareback : 'yes',
    mediaPlaybackRequiresUserAction : 'no',
    shouldPauseOnSuspend : 'no', //Android only
    closebuttoncaption : 'Close', //iOS only
    disallowoverscroll : 'no', //iOS only
    toolbar : 'yes', //iOS only
    enableViewportScale : 'no', //iOS only
    allowInlineMediaPlayback : 'no',//iOS only
    presentationstyle : 'pagesheet',//iOS only
    fullscreen : 'yes',//Windows only
};

  constructor(public navCtrl: NavController, public navParams: NavParams, private theInAppBrowser: InAppBrowser) {
  }

  public openWithSystemBrowser(url : string){
    let target = "_system";
    this.theInAppBrowser.create(url,target,this.options);
}
public openWithInAppBrowser(url : string){
    let target = "_blank";
    this.theInAppBrowser.create(url,target,this.options);
}
public openWithCordovaBrowser(url : string){
    let target = "_self";
    this.theInAppBrowser.create(url,target,this.options);
}

  ionViewDidLoad() {
    console.log('ionViewDidLoad AboutPage');
  }




}

Then I added a button into the html page using:

<button ion-button (click)="openWithSystemBrowser('https://www.preachingfriars.org')">Visit our site</button>

It seems to work great in Android, but no luck in iOS. When I click the button nothing at all happens. I have been scouring the forums for days and still can't manage to make it work. I'd appreciate any help you could provide!

Thanks! Brent

5
Did this ever get solved? - Josh
Nope. I abandoned Ionic completely and just switched to phonegap. - Brent

5 Answers

1
votes

The create method takes a string as options parameters and not an object. And also you have to call the show method.

const browser = this.iab.create(targetUrl, '_self', 'location=no');
browser.show();
0
votes

Try loading it within platform ready method.

@Component({
  templateUrl: 'app.html'
})
export class MyApp {

  browser: InAppBrowserObject;
options: InAppBrowserOptions = {
  location: 'no', //Or 'no' 
  hidden: 'yes', //Or  'yes'
  zoom: 'no', //Android only ,shows browser zoom controls 
  hardwareback: 'yes',
  mediaPlaybackRequiresUserAction: 'yes',
  shouldPauseOnSuspend: 'no', //Android only 
  closebuttoncaption: 'Share', //iOS only
  disallowoverscroll: 'no', //iOS only 
  toolbar: 'yes', //iOS only 
  toolbarposition: 'bottom',
  enableViewportScale: 'no', //iOS only 
  allowInlineMediaPlayback: 'no', //iOS only 
  presentationstyle: 'formsheet', //iOS only 
  fullscreen: 'yes', //Windows only  
  suppressesIncrementalRendering: 'no',
  transitionstyle: 'crossdissolve',

};

constructor(public toastCtrl: ToastController, public actionSheetCtrl: ActionSheetController, public network: Network, public iab: InAppBrowser, public platform: Platform, statusBar: StatusBar, public splashScreen: SplashScreen) {

  platform.ready().then(() => {
    statusBar.styleDefault();
    this.openinInappbrowser();
  });

}

openinInappbrowser() {
  this.browser = this.iab.create('https://ionicframework.com', '_blank', this.options);
  this.browser.show();
  this.splashScreen.hide();
  this.browser.on('loaderror').subscribe(event => {

    this.browser.hide();
    this.presentToast('Something Wnt Wrong');
  });

}

let toast = this.toastCtrl.create({
  message: arg,
  duration: 1500,
  position: 'bottom'
});

toast.onDidDismiss(() => {
  console.log('Dismissed toast');
});

toast.present();
}
isNetavailable() {
  if (this.network.type == 'none' || this.network.type == 'unknown') {
    return false;
  } else {
    return true;
  }

}

}
0
votes

Options for the InAppBrowser. Optional, defaulting to: location=yes. The options string must not contain any blank space, and each feature's name/value pairs must be separated by a comma. Feature names are case insensitive. - https://ionicframework.com/docs/native/in-app-browser/

Here's how you could convert your options object to a string that can be used when using theInAppBrowser.create:

// ...

var optionsStr = '';

for (var key of Object.keys(this.options || {})) {
    if (optionsStr) {// Add a comma if there are any previous options to keep each key value pair seperated
        optionsStr += ",";
    }
    // Add <key>=<value> to optionsStr
    optionsStr += key + '=' + options[key];
}

// Be sure to pass a string instead of an object for options https://ionicframework.com/docs/native/in-app-browser/
// create(url: string, target: string, options: string)
this.browser = this.theInAppBrowser.create('https://ionicframework.com', '_blank', optionsStr);

// ...
0
votes

you can try adding this in your config.xml file

plugin name="cordova-plugin-inappbrowser" spec="3.1.0" value="CDVInAppBrowser"

Add CDVInAppBrowser as the value if it is not here

-2
votes

you can try adding this in your config.xml file

<allow-intent href="*" />
<feature name="InAppBrowser">
        <param name="android-package" value="org.apache.cordova.inappbrowser.InAppBrowser" />
</feature>