首页 > 解决方案 > React Native 应用内购买 Android

问题描述

我正在尝试将 In App Purchases(或 Google 喜欢称之为“In App Billing”)添加到我的 React Native App 中。

我做了什么:

  1. 在 Google Play 控制台中创建了一个产品(标识符为“unlock_premium”
  2. yarn add react-native-iap这个图书馆)
  3. 将以下代码添加到我的应用程序的组件中
  4. 在物理设备上进行了测试react-native run-android,并通过将其发布到 Google Play 控制台的 Alpha 测试

注:应用已签名并在manifest文件中启用计费权限,版本react-native-iap为0.3.23。

问题: 在运行调试版本时,console.log()仅打印产品是undefined,并且productInfo在运行 Alpha 部署版本时不显示在屏幕上(因此产品在那里也未定义)。该products变量只是一个空数组。

try-statement 似乎成功了,因为我没有看到它打印出任何错误。)

import InAppPurchase from "react-native-iap";

const itemSKUs = Platform.select({
    android: [
        "com.kimer.unlock_premium" // I've also tried just "unlock_premium"
    ]
})
...

constructor(props) {
        super(props);
        this.state = {
            modalVisible: false,
            premiumProduct: undefined
        };
}

...

async componentDidMount() {
        try {
            await InAppPurchase.prepare();
            const products = await InAppPurchase.getProducts(itemSKUs);
            console.log("finished call to get products");
            console.log(products[0]);
            this.setState({premiumProduct: products[0]});
        } catch (err) {
            console.warn(err);
        }
}

...

render() {
        let productInfo;
        if (this.state.premiumProduct !== undefined) {
            productInfo = (
                <Text>{this.state.premiumProduct}
                {this.state.premiumProduct.localizedPrice}
                {this.state.premiumProduct.currency} {this.state.premiumProduct.productID}
                {this.state.premiumProduct.title}
                {this.state.premiumProduct.description}</Text>
            );
        }

        return (
            <View>
                ...
                {productInfo}
                ...
            </View>
        );
}

标签: androidreact-nativegoogle-playin-app-purchasein-app-billing

解决方案


已解决: 它现在对我有用!我尝试了几件事,但我不确定让它工作的关键是什么无论如何我所做的:

  1. 重新安装了包
  2. 将我的导入语句从import InAppPurchase from "react-native-iap;" to "import * as InAppPurchase from 'react-native-iap';"
  3. 将代码中的 SKU 从“com.kimer.unlock_premium”更改为“unlock_premium”

推荐阅读