Ionic InAppBrowser网站 - 如果已安装,请在应用程序中打开Twitter链接

时间:2018-05-09 12:40:25

标签: android ionic-framework inappbrowser

我有一个部署到Android手机的Ionic App,它使用InAppBrowser打开一个网站。在网站上是Twitter个人资料的按钮链接。

目前,Twitter按钮将在用户默认浏览器中打开。

如果安装在用户手机上,如何让按钮打开Twitter应用程序?

提前致谢。

修改

ViewModel

我收到错误:

export class HomePage {

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


     this.platform.ready().then( () => {

        const browser = this.inAppBrowser.create("https://awebsitewithfblink", '_blank', options);

        browser.addEventListener('loadstart', function(event) { alert(event.type + ' - ' + event.url); } );
     })


  }
}

1 个答案:

答案 0 :(得分:0)

我不知道您使用哪种Ionic版本,所以我会尝试为您提供最佳解决方案:

在离子1: 您可以执行以下操作:<a href="twitter://twitter.com/somePATH">

在Ionic 2/3中:我不知道第一种方法是否仍然有效,从未尝试过,但是这个方法应该这样做:

首先:安装插件

ionic plugin add cordova-plugin-inappbrowser        // For open the app
ionic plugin add cordova-plugin-appavailability     // For check if app is installed
ionic plugin add cordova-plugin-device              // For check which device the user use

第二:导入它们

import { InAppBrowser, AppAvailability, Device } from 'ionic-native';

第三:完整的解决方案,检查设备/如果应用程序在这里:

launchExternalApp(iosSchemaName: string, androidPackageName: string, appUrl: string, httpUrl: string, username: string) {
let app: string;
if (Device.device.platform === 'iOS') {
    app = iosSchemaName;
} else if (Device.device.platform === 'Android') {
    app = androidPackageName;
} else {
    let browser = new InAppBrowser(httpUrl + username, '_system');
    return;
}

AppAvailability.check(app).then(
    () => { // success callback
        let browser = new InAppBrowser(appUrl + username, '_system');
    },
    () => { // error callback
        let browser = new InAppBrowser(httpUrl + username, '_system');
    }
);
}

openTwitter(username: string) {
    this.launchExternalApp('twitter://', 'com.twitter.android', 'twitter://user?screen_name=', 'https://twitter.com/', username);
}
  

来源1:https://forum.ionicframework.com/t/open-facebook-twitter-app-from-my-android-app/9757/3

     

来源2:https://forum.ionicframework.com/t/ionic-opening-external-app/77932/3

编辑:

由于您想在InAppBrower网站上执行操作,因此我发现此代码对您有用:

var twitterWindow = window.open("https://twitter.com/", '_blank', 'location=no');
twitterWindow.addEventListener('loadstart',  function(event) {
                        alert("Current Event : "+ event.url);
                    });
  

来源3:Getting URL of InAppBrowser

您只需控制此网址并检查它是否是Twitter网址。如果它是一个,你只需要像我以前的答案那样做。