我安装了Ionic Cordova的inAppBrowser插件(至少我认为我安装正确。我按照这里的指示:https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-inappbrowser/#installation)。
我将inAppBrowser导入我的app.module.ts文件并将其添加到提供者列表中:
import { InAppBrowser } from '@ionic-native/in-app-browser';
和
providers: [
StatusBar,
SplashScreen,
InAppBrowser,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
我也将它注入相关页面:
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');
}
}
然后我使用以下命令在html页面中添加了一个按钮:
<button ion-button (click)="openWithSystemBrowser('https://www.preachingfriars.org')">Visit our site</button>
它似乎在Android中运行良好,但在iOS中没有运气。当我点击按钮时什么也没发生。我已经在论坛上搜索了好几天,仍然无法使其成功。我很感激您提供的任何帮助!
谢谢! 布伦特
答案 0 :(得分:1)
create方法将字符串作为选项参数而不是对象。 而且你必须调用show方法。
const browser = this.iab.create(targetUrl, '_self', 'location=no');
browser.show();
答案 1 :(得分:0)
尝试在平台就绪方法中加载它。
@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;
}
}
}
答案 2 :(得分:0)
InAppBrowser的选项。可选,默认为:location = yes。 选项字符串不得包含任何空格,并且每个 功能的名称/值对必须用逗号分隔。功能名称 不区分大小写。 -https://ionicframework.com/docs/native/in-app-browser/
这是将选项对象转换为在使用InAppBrowser.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);
// ...
答案 3 :(得分:0)
您可以尝试将其添加到config.xml文件中
插件名称=“ cordova-plugin-inappbrowser” spec =“ 3.1.0” value =“ CDVInAppBrowser”
如果CDVInAppBrowser不在此处,则将其添加为值
答案 4 :(得分:-1)
您可以尝试在config.xml文件中添加它
<allow-intent href="*" />
<feature name="InAppBrowser">
<param name="android-package" value="org.apache.cordova.inappbrowser.InAppBrowser" />
</feature>