应用内购买产品 ID 更新落后一位

In-App Purchase Product ID Updates One Behind

我有一个关于我的应用内购买设置中的奇怪故障的快速问题:

我的 ViewController 有 2 个按钮:AB。当用户单击这些按钮之一时,我将我的 productID 设置为所选项目。然后我有一个 Buy 按钮,用户应该点击它来购买商品。我遇到的 问题 是我似乎无法获得 SKProductRequest 以使用正确的项目启动 productsRequest 方法。

换句话说,向Apple发送的productRequest总是比正确的product滞后1。这是我实现的代码:

ViewController.h

#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>

@interface ViewController : UIViewController <SKPaymentTransactionObserver, SKProductsRequestDelegate>

/// In app purchase crap
@property (strong, nonatomic) SKProduct *product;
@property (strong, nonatomic) SKProductsRequest *request;
@property (retain, nonatomic) NSString *productID;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

int start;

- (void)viewDidLoad {
    [super viewDidLoad];

    // Attempt to get request aligned with item selected.. :\
    start = YES; 
    _productID = @"com.example.IAP.itemA";

    [self Buy:self];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




- (IBAction)Buy:(id)sender {

// Check for user to be able to make payment
    if ([SKPaymentQueue canMakePayments]) {
        _request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:_productID]];
        _request.delegate = self;

        [_request start];
    } else {
        NSLog(@"Please enable In App Purchasing in your settings");
    }
}



- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {

    NSArray *products = response.products;

    if (products.count != 0) {
        NSLog(@"Products available: %@", _product.localizedTitle);
        _product = products[0];
        if (start != YES) {
            [self beginPaymentQueue];
        }
        else {
            start = NO;
        }
    } else {
        NSLog(@"Products not found.");
    }

    products = response.invalidProductIdentifiers;

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

- (void)beginPaymentQueue {            
    SKPayment *payment = [SKPayment paymentWithProduct:_product];
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue  defaultQueue] addPayment:payment];
}


- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {

    for (SKPaymentTransaction *transaction in transactions) {
        switch (transaction.transactionState) {
            case SKPaymentTransactionStatePurchased: { [self UnlockPurchase];
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break; }
            case SKPaymentTransactionStateFailed: { NSLog(@"Transaction Failed");
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break; }
            default:
                break;
        }
    }
}

- (void)UnlockPurchase {
    NSLog(@"Purchase Successful!");
}



// Item Buttons
- (IBAction)buttonA:(id)sender {
    _productID = @"com.example.IAP.itemA";
}

- (IBAction)buttonB:(id)sender {
    _productID = @"com.example.IAP.itemB";
}

不知道该怎么做...我尝试在 viewDidLoad 方法中调用 CanPurchase 方法,我什至尝试在 productID 的方法中使用 switch 语句,但没有任何效果。

如果有人可以提出解决方案或尝试提供帮助,我将不胜感激。几天来我一直在努力解决这个问题。谢谢!

仅供参考:这是正在发生的事情的日志(我在这里为每一行手动添加了评论,让您知道用户为获得该日志所做的工作完整日期)

app loads        12:14:49.488 IAP[4928:370136] Products available: (null)
click B then buy 12:14:52.053 IAP[4928:370136] Products available: IAP A
cancel iap       12:15:01.086 IAP[4928:370136] Transaction Failed
click B then buy 12:15:02.622 IAP[4928:370136] Products available: IAP B
cancel iap       12:15:04.256 IAP[4928:370136] Transaction Failed
click A then buy 12:15:02.622 IAP[4928:370136] Products available: IAP B
cancel iap       12:15:04.256 IAP[4928:370136] Transaction Failed
click A then buy 12:15:02.622 IAP[4928:370136] Products available: IAP A
cancel iap       12:15:04.256 IAP[4928:370136] Transaction Failed

该循环继续,productRequest 始终比所选 IAP 滞后 1。请帮忙!

看起来你只是在混淆自己:

NSArray *products = response.products;
if (products.count != 0) {
    NSLog(@"Products available: %@", _product.localizedTitle);
    _product = products[0];

您正在从之前存储的产品中获取本地化标题。这就是为什么你落后了。相反,从响应返回给您的产品中获取产品及其标题:

NSArray *products = response.products;
if (products.count != 0) {
    SKProduct* product = products[0];
    NSLog(@"Products available: %@", product.localizedTitle);