代码之家  ›  专栏  ›  技术社区  ›  Madhur

从React native App打开外部应用程序(单击按钮)

  •  1
  • Madhur  · 技术社区  · 6 年前

    我想通过点击我使用过的按钮来打开新的React本机应用程序

    在React native中链接概念

    React native Code:Test是另一个应用程序的名称

    Linking.openURL('Test://app');
    

    下面是在android.manifest.xml文件中添加Intent的URL Andriod Intent

    代码:Android.manifestfile.xml

      <activity
            android:name=".MainActivity"
            android:launchMode="singleTask"
            android:label="@string/app_name"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
              <intent-filter>
                  <action android:name="android.intent.action.VIEW" />
                  <category android:name="com.Test" />
                  <category android:name="android.intent.category.BROWSABLE" />
                  <data android:scheme="Test" android:host="app" />
              </intent-filter>
          </activity>
    

    请帮忙解决问题,提前谢谢!

    3 回复  |  直到 6 年前
        1
  •  3
  •   anil sidhu    6 年前

    将此代码添加到AndroidManifest.xml文件中,与当前意图过滤器并行

     <intent-filter android:label="@string/app_name">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <!-- Accepts URIs that begin with "example://gizmos” -->
            <data android:scheme="app"
                  android:host="testApp" />
        </intent-filter> 
    

    然后运行这个命令 React-native run-android

    将下面的代码添加到react本机文件中。

    <Button title="Click me" onPress={ ()=>{ Linking.openURL('app://testApp')}} />
    

    为了节省时间。您可以将两个代码放在同一个应用程序中,同一个应用程序将按按钮打开

    我只是试试这个代码,它对我有用 如果仍然面临问题,请通知我(Y)

        2
  •  0
  •   hitendra Savkare    6 年前

    您可以使用以下代码打开外部应用程序

    Linking.canOpenURL("fb://app").then(supported => {
      if (supported) {
        Linking.openURL("fb://app");
      } else {
        alert('sorry invalid url')
      }
    });
    
        3
  •  -1
  •   Alien    6 年前
    <Button title="Click me" onPress={ ()=>{ Linking.openURL('https://google.com')}} />
    

    以下是您可以查看的链接: https://snack.expo.io/rJm_YkqyW