如果声明用于显示基于In App Purchase的内容

时间:2014-01-06 22:49:18

标签: ios objective-c in-app-purchase

我已将In App Purchasing添加到我的iOS应用程序中,现在正尝试添加if语句,告诉应用根据产品是否已购买显示不同的内容

我在viewDidLoad中的代码是这样的:

    SKProduct * product = (SKProduct *) [_products self];
[_priceFormatter setLocale:product.priceLocale];


if ([[RageIAPHelper sharedInstance] productPurchased:product.productIdentifier]) {


    labelPrice.text = @"Purchased";


} else {


    labelPrice.text = @"Not Purchased";


}

问题是应用程序何时加载,无论是否购买了商品,都显示“未购买”

还有另一种简单的方法来检查用户是否已完成购买吗?

1 个答案:

答案 0 :(得分:0)

productPurchased:(如果您正在使用教程中的IAPHelper),只需检查产品是否包含在数组中。

你如何填充该数组? (即:[RageIAPHelper sharedInstance]如何实施?

当我使用它时,我用我的产品填充了IAPHelper(如下所示),然后初始化了通过NSUserDefaults购买的产品:

- (id)initWithProductIdentifiers:(NSSet *)productIdentifiers {

    if ((self = [super init])) {

        // Store product identifiers
        _productIdentifiers = productIdentifiers;

        // Check for previously purchased products
        _purchasedProductIdentifiers = [NSMutableSet set];
        for (NSString * productIdentifier in _productIdentifiers) {
            BOOL productPurchased = [[NSUserDefaults standardUserDefaults] boolForKey:productIdentifier];
            if (productPurchased) {
                [_purchasedProductIdentifiers addObject:productIdentifier];
                NSLog(@"Previously purchased: %@", productIdentifier);
            } else {
                NSLog(@"Not purchased: %@", productIdentifier);
            }
        }

        // Add self as transaction observer
        [[SKPaymentQueue defaultQueue] addTransactionObserver:self];

    }
    return self;

}

然后productPurchased只检查那个数组:

- (BOOL)productPurchased:(NSString *)productIdentifier {
    return [_purchasedProductIdentifiers containsObject:productIdentifier];
}