android - React Native 应用内购买 Android
问题描述
我正在尝试将 In App Purchases(或 Google 喜欢称之为“In App Billing”)添加到我的 React Native App 中。
我做了什么:
- 在 Google Play 控制台中创建了一个产品(标识符为“unlock_premium”
yarn add react-native-iap
(这个图书馆)- 将以下代码添加到我的应用程序的组件中
- 在物理设备上进行了测试
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>
);
}
解决方案
已解决: 它现在对我有用!我尝试了几件事,但我不确定让它工作的关键是什么无论如何我所做的:
- 重新安装了包
- 将我的导入语句从
import InAppPurchase from "react-native-iap;" to "import * as InAppPurchase from 'react-native-iap';"
- 将代码中的 SKU 从“com.kimer.unlock_premium”更改为“unlock_premium”
推荐阅读
- php - 需要帮助定制 opencart
- c# - 如何通过自定义中间件从用户输入表单中提取值?
- powershell - Powershell ForEach 问题
- module - Odoo:如何知道谁安装了特定模块?
- python - 具有空变量和 0 维对象数据框的 xarray 和 netCDF 文件
- c++ - 尝试运行 NVIDIA FleX 时出现“检测到缓冲区溢出”
- php - 排队的工作卡住了,表可能锁定在 Laravel
- php - 访问。将排除的子文件夹重定向到域的 https 版本,其他所有内容都被重定向到另一个域
- c# - 将动态对象列表转换为字典
- sonarqube - api/issues/search 响应仅在登录时具有“作者”字段