[英]Open external App from React native App ( Button Click)
我想通過單擊我使用過的按鈕來打開 New React Native App
在 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>
我該如何解決這個問題?
將此代碼添加到與當前意圖過濾器平行的 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-native 文件中。
<Button title="Click me" onPress={ ()=>{ Linking.openURL('app://testApp')}} />
為了節省時間。 您可以在同一個應用程序中同時編寫代碼,並且在按下按鈕時將打開同一個應用程序
我只是試試這個代碼,它對我有用讓我知道是否仍然面臨問題(Y)
您可以使用以下代碼打開外部應用程序
Linking.canOpenURL("fb://app").then(supported => {
if (supported) {
Linking.openURL("fb://app");
} else {
alert('sorry invalid url')
}
});
100%使用
react-native-send-intent
從另一個應用程序打開所有應用程序。React Native Android模塊使用Android的Intent操作打開第三方應用程序。
安裝npm install react-native-send-intent --save
注冊模塊> = 0.29(在MainApplication.java中)僅添加2行
import com.burnweb.rnsendintent.RNSendIntentPackage; // <--- import in MainApllication.java
public class MainApplication extends Application implements ReactApplication {
......
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new RNSendIntentPackage()); // <------ add this line to your MainApplication
class
}
......
}
示例/在您的本機代碼中打開應用
SendIntentAndroid.isAppInstalled('com.medlife.customer').then((isInstalled) => {
if (isInstalled) {
SendIntentAndroid.openApp('com.medlife.customer').then((wasOpened) => {
});
console.log("is installed true");
}
else {
Linking.openURL('https://play.google.com/store/apps/details?id=com.medlife.customer&hl=en').catch(err => {
console.log(err)
})
}
});
如果您需要打開另一個應用程序,那么我將從我的應用程序中打開3rd Party
Medlife
應用程序,然后僅在SendIntentAndroid.openApp('com.medlife.customer')
更改pacakage name
。
<Button title="Click me" onPress={ ()=>{ Linking.openURL('https://google.com')}} />
這是您可以查看的鏈接: https : //snack.expo.io/rJm_YkqyW
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.