【问题标题】:How to open other mobile apps on press in React Native?如何在 React Native 中打开其他移动应用程序?
【发布时间】:2019-07-09 22:17:04
【问题描述】:

我正在寻找一种简单的方法来打开应用程序,特别是 Facebook 和 Instagram,通过我的 React Native 应用程序按下按钮。它还应该首先检查应用程序是否安装在设备上,如果没有则打开应用商店。它需要同时在 iOS 和 Android 上运行。我是初学者,所以如果你能发布一个例子会有所帮助。

【问题讨论】:

    标签: react-native


    【解决方案1】:

    您可以使用 react-native 的 Linking 模块打开其他移动应用。

    import { Linking } from "react-native";
    
        const APP_ID = //ID of app need to open in play store
        const appDeepLinkURL = //Most of the mobile app provide it 
         Linking.openURL(appDeepLinkURL).catch(err => {
              Linking.openURL(
                `market://details?id=${APP_ID}`
              ).catch(err => Linking.openURL(
                `http://play.google.com/store/apps/details?id=${APP_ID}`
              ).catch(err => console.error("An error occurred", err)););
            });
    

    同样,你可以为 iOS 做, 可以参考官方文档here

    【讨论】:

    • 它不能与谷歌地图应用程序和其他应用程序一起使用它工作得很好,知道吗?
    • @DevA 您是否使用了正确的 ID com.google.android.apps.maps
    • @Harshkurra 如何获取其他应用程序的 App_Id 和 appDeepLinkURL?
    • 如果它是一个著名的应用程序,那么你将通过谷歌搜索得到正确的 ID,否则你必须进行一些逆向工程
    【解决方案2】:

    使用 react-native Linking 组件

    import { 
      TouchableOpacity,
      Text,
      Linking,
    
    } from 'react-native';
    
    <TouchableOpacity onPress={() => { Linking.openURL('sms:' + {contactNumber} 
     + '?body=Hi'); }}>
     <Text> Open Message App </Text>
    </TouchableOpacity>
    
    

    【讨论】:

    • 它不能与谷歌地图应用程序和其他应用程序一起使用它工作得很好,知道吗?
    最近更新 更多