Will Ullrich
Will Ullrich

Reputation: 2228

In-App Purchase Product ID Updates One Behind

I have a quick question to a strange glitch in my In-App Purchase set up:

My ViewController has 2 buttons: A and B. When the user clicks one of these buttons, i am having my productID set to the chosen item. I then have a Buy button that the user should click to purchase the item. The issue I am having is that I can not seem to get the SKProductRequest to start the productsRequest method with the correct item.

In other words, the productRequest to Apple is always lagging 1 behind the correct product. Here is my code i have implemented:

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";
}

Not sure what to do... I have tried calling the CanPurchase method in the viewDidLoad method, I have even tried switch statements inside the methods for the productID, but nothing worked.

If someone could suggest a solution or try and help I would be very appreciative. I have been trying to fix this for days. Thanks!

FYI: here is a log of what is happening ( I manually added comments on here for each line to let you know what the user has done to get that log in place of the full date)

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

That loop continues, always with the productRequest lagging behind by 1 with the chosen IAP. Please help!

Upvotes: 0

Views: 102

Answers (1)

matt
matt

Reputation: 535306

It looks like you're just confusing yourself here:

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

You are getting the localized title from the product you stored earlier. This is why you are one behind. Instead, get the product and hence its title from the products that the response returned to you:

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

Upvotes: 1

Related Questions