React-native-iap: RNIap.getProducts没有在iOS上返回Promise

创建于 2018-12-19  ·  24评论  ·  资料来源: dooboolab/react-native-iap

版本的react-native-iap

“ react-native-iap”:“ ^ 2.3.25”

本机版本

“ react-native”:“ 0.57.4”,

您遇到错误的平台(IOS或Android还是两者兼有?)

iOS

预期行为

buyProduct应该返回promise(.then()不是call),

实际行为

buyProduct不返回任何承诺,在android中工作正常, .then()调用成功,但在ios中则不行。

经过测试的环境(仿真器?真实设备?)

真实设备

重现行为的步骤

onBuyNowPress() {
    this.setState({ loading: true });
    RNIap.getProducts(['product_id']).then(success => { 
       RNIap.buyProduct('product_id').then(purchase => { 
        console.log("Purchase >>", purchase)
        this.setState({
          loading: false,
          receipt: purchase.transactionReceipt, 
        });
        Alert.alert('Purchase Successful!');
        this.props.getPurchase();
       }).catch(err => { 
         console.log(err.code, err.message);
          this.setState({ loading: false });
          Alert.alert(err.message);
          if(err.message === "You already own this item.") {
            this.props.getPurchase();
          }
        }) 
      }).catch(error => { 
        alert(error); 
        this.setState({ loading: false });
      })
  }

在ios中,我已经购买了产品,因此当我再次按立即购买时,会弹出弹出窗口,就像您已经购买了该产品一样。 但是.then ().catch()没有得到调用。

我也尝试过getAvailablePurchases()方法,它总是返回[]

📱 iOS 🙏 help wanted

最有用的评论

您可以尝试2.4.0-beta4吗?

所有24条评论

@LinusU我认为@JJMoon是正确的。 我已删除所有async ,问题再次出现。 你有什么感想? @ ZeroCool00您现在会降级到2.3.24吗?

@ ZeroCool00实际上,您能为我们尝试2.3.26吗?

@hyochan我会尽快测试..让您知道,谢谢您的迅速答复。

@hyochan我尝试使用2.3.26,它不起作用? 有什么办法吗?

@ ZeroCool00然后它一定是链接问题。 您应该尝试unlink然后再次重新link

@hyochan我尝试过,即使我取消链接,然后卸载并重新安装,然后再次链接。 但它仍然无法正常工作

@ ZeroCool00如果您在窗口中,本机链接脚本将更加不稳定。 您手动尝试过吗? 也请尝试2.3.24 。 我认为您面临一些配置问题。

@hyochan im在Mac上,我断开链接,然后卸载,然后安装2.3.24,然后进行手动设置,按照每个步骤操作,它与android一起使用,在ios中仍未显示。 程序运行正常。 即时消息越来越弹出,产品也得到购买,但没有承诺回报。

ios还有其他配置吗? 即时通讯卡在这里,有没有其他方法可以让我已经购买。

@JJMoon您能帮忙@ ZeroCool00吗?

@ ZeroCool00也尝试删除Alert并使用console进行测试。 因为ios在处理购买时会自动生成本机Alert ,这可能会中断您的代码。

@hyochan Plz重新打开了这个..我尝试了所有事情..我也删除了警报。 甚至我停止调试并检查..它只是无法正常工作。 我想发布我的应用,但我被困在这里。 我什至尝试了这个库https://github.com/chirag04/react-native-in-app-utils

这个库也有一个同样的问题..采购过程运行良好,但没有得到任何承诺。

是否有任何生命周期方法可以获取事件回调。 我只希望可以在其中调用服务器api的事件回调。

@ ZeroCool00您可以尝试同时使用asyncawait吗? 我也在我们的应用程序中使用了它,我们没有遇到这个问题。

@hyochan能否请您提供演示..您如何使用它..这将是非常

async forIOS() {
    let purchase =  await RNIap.buyProduct('product_id');
    console.log("BannerPurchase >>",purchase);  **<--- THIS IS NOT GETTING CALL**
    if(purchase) {
      console.log("Purchase >>", purchase)
      this.props.getPurchase();
      this.setState({
        loading: false,
        receipt: purchase.transactionReceipt, 
      });
      console.log('Purchase succesfully')
    }
  }

@hyochan我按照您说的去做,看来这个过程是无限的,什么也不返回。 我什至没有日志。

由于最近有许多其他人使用我们的模块,因此您的情况看起来很奇怪。 除非我们有您的完整工作代码,否则我们很难弄清楚。

@hyochan具有与@ ZeroCool00相同的问题
刚刚从2.3.26升级到2.4.0-beta3(取消了链接),情况相同。
从设备中获取日志。 使用真正的iPhone,通过xCode(有线连接)进行开发构建。

[IAPInfoManager]: Update operation failed, error: Error Domain=SSErrorDomain Code=109 "Cannot connect to iTunes Store" UserInfo={NSLocalizedDescription=Cannot connect to iTunes Store, SSErrorHTTPStatusCodeKey=401}

但是出现带有购买的弹出窗口以及成功案例。 但是在Javascript线程上没有任何反应:(
iOS模块未解决或拒绝承诺。

try {
      let purchase = false;

      console.log('BEFORE BUY');
      if (Platform.OS === 'ios') {
        purchase = await RNIap.buyProduct(selected);
      } else {
        purchase = await RNIap.buySubscription(selected);
      }
      console.log('AFTER BUY');

      await RNIap.finishTransaction();
      console.info('purchase >', purchase);
      setSubscription({type: selected, purchaseData: purchase});
    } catch (err) {
      console.log(err); // TODO add something went wrong
    }

AFTER BUY
甚至不露面。 没有异步/等待的同一故事,解决了.then许诺。

任何想法可能导致此问题?

您可以尝试2.4.0-beta4吗?

@hyochan非常感谢👍
问题已经解决了。

@IsaevTimur您如何解决的? 它仍然无法正常工作。

@hyochan这是我的inapp购买模块的完整代码

import React, { Component } from 'react';
import { View, ImageBackground, TouchableOpacity, Image, Alert, 
  ActivityIndicator, Platform } from 'react-native';
import {inapp, btnBuy, purple} from '../helper/constants';
import ImageResizeMode from 'react-native/Libraries/Image/ImageResizeMode'
import { connect } from 'react-redux';
import { Icon } from 'native-base';
import { Actions } from 'react-native-router-flux';
import * as RNIap from 'react-native-iap';
import { getItems, getPurchase, fetchCategory } from '../action';

const itemSkus = Platform.select({
  ios: [
    'max_asl_unlimited_access'
  ],
  android: [
    'max_asl_unlimited_access'
  ]
});

class Banner extends Component {

  constructor(props){
    super(props);
    this.onBuyNowPress = this.onBuyNowPress.bind(this);
    this.forAndroid = this.forAndroid.bind(this);
    this.forIOS = this.forIOS.bind(this);
  }

  state = {
    loading: false,
  }

  async componentDidMount() {
    try {
      const result = await RNIap.initConnection();
      console.log('result', result);
      this.props.getItems();
    } catch (err) {
      console.warn(err.code, err.message);
    }
}

  componentDidUpdate(prevProps) {
    if(prevProps.purchase !== this.props.purchase) {
      this.props.fetchCategory();
    } 
  }

  onBuyNowPress() {
    this.setState({ loading: true });
    RNIap.getProducts(itemSkus).then(success => { 
      if(Platform.OS == "ios") {
        this.forIOS()
      } else {
        this.forAndroid()
      }
    }).catch(error => { 
      alert(error); 
      this.setState({ loading: false });
    })
  }

  async forIOS() {
    console.log('BannerCall');
    try {
      const purchase = await RNIap.buyProduct('max_asl_unlimited_access');
      console.log("BannerPurchase >>",purchase);
      if(purchase) {
        this.props.getPurchase();
        this.setState({
          loading: false,
        });
        console.log('Purchase succesfully')
      }
    } catch (err) {
      console.log("BannerError >> ", err);
    }
  }

  forAndroid() {
    RNIap.buyProduct('max_asl_unlimited_access').then(purchase => { 
      this.props.getPurchase();
      this.setState({
        loading: false,
        receipt: purchase.transactionReceipt, 
      });
      console.log('Purchase succesfully')
      Alert.alert("Purchase succesfully")
     }).catch(err => { 
       console.log(err.code, err.message);
        this.setState({ loading: false });
        if(err.message === "You already own this item.") {
          this.props.getPurchase();
          Alert.alert("You already own this product, we are restoring your purchase.")
        }
      }) 
  }

  componentWillUnmount() {
    RNIap.endConnection();
  }

  renderSpinner() {
    if(this.state.loading) {
      return (
        <ActivityIndicator size="large" color="#F7CD52" />
      )
    }
    return null
}

  render() {
    const { container, bannerStyle, btnContainer, iconClose } = styles;
    return (
      <View style={container}>
          <ImageBackground source={inapp} style={bannerStyle} resizeMode={ImageResizeMode.contain}>
              <Icon name="close" style={iconClose} type="FontAwesome" onPress={() => {Actions.pop()}}/>
              <View style={btnContainer}>
                {this.renderSpinner()}
                <TouchableOpacity onPress={() => this.onBuyNowPress()}>
                    <Image source={btnBuy}  />
                </TouchableOpacity>
              </View>
          </ImageBackground>
      </View>
    );
  }
}


const styles = {
  container: {
    flex: 1,  
  },
  iconClose: {
    color: '#FFF',
    fontSize: 30,
    alignSelf: 'flex-end',
    margin: 20,
    fontWeight: 'bold',
  },
  bannerStyle: {
    flex: 1,
    width: '100%',
    height: '100%',
    backgroundColor: purple
  }, 
  btnBuy: {
    marginRight: 20,
    marginTop: 160,
    height: 85,
    width: 215,
  },
  btnContainer: {
    flex: 1,
    marginTop: 50,
    justifyContent: 'center',
    alignItems: 'center'
  }
};

function mapStateToProps({ purchase }) {
    return { purchase };
}

export default connect(mapStateToProps, { getItems, getPurchase, fetchCategory })(Banner);

@ ZeroCool00

确保您正在使用-2.4.0-beta4。

另外,请确保您正在使用SANDBOX用户(正在开发中)。 可以在iTunes connect中进行配置。 这些用户在“沙箱”部分下的此处https://appstoreconnect.apple.com/access/users中创建。

希望能帮助到你 :)

@IsaevTimur
但是我的应用商店购买过程运行正常,这意味着一切都已配置完毕,是的,我使用2.4.0-beta4。

这是我在package.json文件上的条目

"react-native-iap": "^2.4.0-beta4",

@hyochan我终于解决了问题..问题是当我第一次测试时,我没有在.then()方法中写这行await RNIap.finishTransaction(); 。 我后来添加了它。 那就是问题,解决方案是我需要清除交易记录( await RNIap.clearTransaction(); ),并且它可以正常工作。

再次感谢您对这个出色的图书馆的支持。

@ ZeroCool00恭喜成功!! 是的,应该在您这方面更好地解决这类问题,因为我们需要对您做错了什么有所了解。 感谢您再来一次可能因为同一件事而受苦的人们。

此页面是否有帮助?
0 / 5 - 0 等级