gpt4 book ai didi

ios - 无法识别 UpdatedTransactions 委托(delegate)中购买的产品

转载 作者:行者123 更新时间:2023-11-29 00:39:06 25 4
gpt4 key购买 nike

我在 IAP 中有几个产品(在这个例子中有 2 个),我正在使用 UITableView,用户将单击它并选择他们想要购买的产品。但是,我无法在 updatedTransactions 委托(delegate)中识别用户购买了哪个交易。以下是我的代码:

NSArray *arrayProducts;

-(void) getProductInfo:(BLevelViewController *) viewController
{
if ([SKPaymentQueue canMakePayments])
{
_linkStatus.text = @"Able to purchase";
SKProductsRequest *requestProducts = [[SKProductsRequest alloc]
initWithProductIdentifiers:[NSSet setWithObjects:@"example.1coin", @"example.2coin", nil]];
requestProducts.delegate = self;
[requestProducts start];

}else{
_linkStatus.text = @"Please enable In App Purchase in Setting";
}
}

-(void) productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSArray *products = response.products;

if (products.count != 0)
{
_linkStatus.text = @"Product loaded";
arrayProducts = products;

} else {
_linkStatus.text = @"Product no found";
arrayProducts = @[@"0",@"0"];
}

products = response.invalidProductIdentifiers;

for (SKProduct *product in products)
{
NSLog(@"Product not found: %@", product);
}

}


-(void) tableView:(UITableView *)tableView_ didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

if (indexPath.row == 0)
{
SKPayment *payment = [SKPayment paymentWithProduct:[arrayProducts objectAtIndex:0]];
[[SKPaymentQueue defaultQueue] addPayment:payment];

} else if (indexPath.row == 1)
{
SKPayment *payment = [SKPayment paymentWithProduct:[arrayProducts objectAtIndex:1]];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}

}

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased:
if ([transaction.description isEqualToString:[arrayProducts objectAtIndex:0]]){
[self unlockFeature1coin];
}
if ([transaction.description isEqualToString:[arrayProducts objectAtIndex:1]]){
[self unlockFeature10coins];
}
[[SKPaymentQueue defaultQueue]
finishTransaction:transaction];
break;

case SKPaymentTransactionStateFailed:
[[SKPaymentQueue defaultQueue]
finishTransaction:transaction];
break;

case SKPaymentTransactionStatePurchasing:

break;


default:
break;
}
}
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

BCoinsViewCell * cell = [tableView dequeueReusableCellWithIdentifier:coinsCellIdentifier];

if (indexPath.row == 0)
{
cell.cellLabel.text = @"Buy 10 coins";
[cell.cellButton setTitle:@"Buy" forState:UIControlStateNormal];
}
else if (indexPath.row == 1)
{
cell.cellLabel.text = @"Buy 1 coin";
[cell.cellButton setTitle:@"Buy" forState:UIControlStateNormal];
}

return cell;
}

问题出在下面几行,我应该使用什么作为标识符,以便我可以根据用户购买调用不同的功能?

        if ([transaction.description isEqualToString:[arrayProducts objectAtIndex:0]]){
[self unlockFeature1coins];
}
if ([transaction.description isEqualToString:[arrayProducts objectAtIndex:1]]){
[self unlockFeature10coins];
}

最佳答案

您可以像这样从 SKPaymentTransaction 对象中获取产品标识符 transaction.payment.productIdentifier。只需用这段代码替换您的 if 条件:

    if ([transaction.payment.productIdentifier isEqualToString:@"example.1coin"]){

[self unlockFeature1coins];
}
if ([transaction.payment.productIdentifier isEqualToString:@"example.10coins"]){

[self unlockFeature10coins];
}

关于ios - 无法识别 UpdatedTransactions 委托(delegate)中购买的产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39944257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com