react - native:如何从react native应用打开google play商店

React-Native: How to open google play store from react native app?

本文关键字:native react google play 商店 应用      更新时间:2023-09-26

我正试图找到一种方法,如何打开谷歌播放商店与我的应用程序,当用户按下一个按钮内的反应原生应用程序。有什么办法可以做到吗?

如果人们懒得阅读android的烦人的SPA开发文档:

market://details?id=<package_name>

在反应原生土地:

Linking.openURL("market://details?id=<package_name>");

的例子:

Linking.openURL("market://details?id=googoo.android.btgps");

编辑:确保导入Linking以使用它

import { View, Text, StyleSheet, Linking } from 'react-native';

你可以使用深层链接从你的应用程序中重定向用户:https://developer.android.com/distribute/tools/promote/linking.html

和react-native的Linking API:http://facebook.github.io/react-native/docs/linking.html

对于将来的用户,您可以从接受的答案中继续阅读文档以获取更多信息。但如果你想要一个更快的结果,这里是。

react-native中的Linking作为

import { Linking } from 'react-native'
Linking.openURL("http://play.google.com/store/apps/details?id=<package_name>")

如果你正在使用expo,那么下面的代码就是为你准备的。

通过expo install expo-linking安装expo-linking

import * as Linking from "expo-linking";
Linking.openURL("http://play.google.com/store/apps/details?id=<package_name>")

注意: <package_name>是你的应用程序在manifest文件中定义的包名,如果你使用expo

app.json

这对我来说很有效:去google play。在那里搜索你的应用,复制url并把它放在

下面
import { Linking } from "react-native";
Linking.openURL("your app url her")

看起来这个包是一个很酷的选择。

可用于React Native和Expo

https://github.com/kimxogus/react-native-version-check

Expo链接文档提及。

有可能用户没有安装Lyft应用程序,在这种情况下,你可能想要打开app/Play Store,或者让他们知道他们需要先安装它。对于这些情况,我们建议使用react-native-app-link库。

它有麻省理工学院的许可,所以我们可以很好地使用它。

让我们看看这个库是怎么做的!

https://github.com/FiberJW/react-native-app-link/blob/master/index.js

export const openInStore = async ({ appName, appStoreId, appStoreLocale = 'us', playStoreId }) => {
  if (Platform.OS === 'ios') {
    Linking.openURL(`https://apps.apple.com/${appStoreLocale}/app/${appName}/id${appStoreId}`);
  } else {
    Linking.openURL(
      `https://play.google.com/store/apps/details?id=${playStoreId}`
    );
  }
};

所以我把它改成:

export const openInStore = async (config: {
    appStoreConfig: {
        appName: string;
        appId: string;
        storeLocale: string;
    };
    playStoreConfig: {
        appId: string;
    };
}) => {
    if (Platform.OS === "ios") {
        await Linking.openURL(
            `https://apps.apple.com/${config.appStoreConfig.storeLocale}/app/${config.appStoreConfig.appName}/id${config.appStoreConfig.appId}`
        );
    } else if (Platform.OS === "android") {
        await Linking.openURL(
            `https://play.google.com/store/apps/details?id=${config.playStoreConfig.appId}`
        );
    }
};

用法:

try {
    openInStore({
        playStoreConfig: {appId: "com.stevemoretz.test"},
        appStoreConfig: {
            appId: "com.stevemoretz.test",
            appName: "Test",
            storeLocale: "us",
        },
    });
} catch (e) {}

应该可以工作,但还没有测试!