[英]android: React native open an app from another app?
我正在嘗試從我的應用程序中打開另一個應用程序( https://play.google.com/store/apps/details?id=com.inova.velocity )。 但是所有的教程都只是將 url 重定向到 playstore。(我找到了一個 github 鏈接( https://github.com/FiberJW/react-native-app-link )它只打開了 iOS 的應用程序,但適用於 Android它正在重定向到 Playstore)。 有沒有辦法解決這個問題?
Linking.canOpenURL('market://details?id=com.inova.velocity')
.then((canOpen) => {
if (canOpen) {
console.log('open app');
return Linking.openURL('market://details?id=com.inova.velocity')
};
}).catch(err => console.log('An error occurred', err));
是的,您的代碼是正確的。 但是您使用了 playstore url,而不是 schema url。 您必須設置可以從相關應用程序開發人員處獲取的 schemaUrl。 如果沒有為該應用程序設置架構 URL,您將無法打開它。 獲得 SchemaUrl 后,您可以使用您的代碼。 像下面。
Linking.canOpenURL(SchemaUrl).then(supported => {
if (supported) {
console.log('accepted');
return Linking.openURL(SchemaUrl);
} else {
console.log('an error occured');
}
}).catch(
err => console.log('an error occured');
);
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
。
使用react-native-send-intent 模塊,你可以做到
SendIntentAndroid.openApp('packagename').then((wasOpened) => {}); 您的包名稱是您要打開的任何應用程序包名稱。
例如 SendIntentAndroid.openApp('com.inova.velocity').then((wasOpened) => {});
wasOpened 是一個布爾承諾,告訴您應用程序是否已打開
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.