应用内购买未能加载产品列表

时间:2014-03-03 12:09:09

标签: ios in-app-purchase itunesconnect

我正在尝试实施一些inapp购买。我已经按照在线教程,我的代码发布在下面。我已将应用内购买添加到我的itunes连接帐户,但是当我运行代码时,它会调用'didfailwitherror'方法,该方法无法加载产品列表。有没有其他人有这个问题并且可能知道解决方案?

#import "IAPHelper.h"
#import <StoreKit/StoreKit.h>
NSString *const IAPHelperProductPurchasedNotification = @"IAPHelperProductPurchasedNotification";

@interface IAPHelper ()  <SKProductsRequestDelegate, SKPaymentTransactionObserver>

@end

@implementation IAPHelper{
  SKProductsRequest *_productsRequest;
  RequestProductsCompletionHandler _completionHandler;
  NSSet * _productIdentifiers;
  NSMutableSet * _purchasedProductIdentifiers;
}

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

    NSLog(@"get product ids");

if((self=[self init])){
    _productIdentifiers = productIdentifiers;
    _purchasedProductIdentifiers = [NSMutableSet set];
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    for (NSString* productIdentifier in _productIdentifiers) {
        NSLog(@"product id is %@", productIdentifier);

        BOOL productPurchased = [[NSUserDefaults standardUserDefaults]    boolForKey:productIdentifier];
        if (productPurchased) {
            [_purchasedProductIdentifiers addObject:productIdentifier];
        }
    }
}
return self;
}

-(void)requestProductsWithCompletionHandler:(RequestProductsCompletionHandler)completionHandler{

NSLog(@"Attempt to complete request for products");

  _completionHandler = [completionHandler copy];
  _productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:_productIdentifiers];
  _productsRequest.delegate=self;
  [_productsRequest start];
}

#pragma mark - SKProductsRequestDelegate

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

NSLog(@"Loaded list of products...");
_productsRequest = nil;

NSArray * skProducts = response.products;
for (SKProduct * skProduct in skProducts) {
    NSLog(@"Found product: %@ %@ %0.2f",
          skProduct.productIdentifier,
          skProduct.localizedTitle,
          skProduct.price.floatValue);
}

_completionHandler(YES, skProducts);
_completionHandler = nil;

}

- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {

NSLog(@"Failed to load list of products.");
_productsRequest = nil;

_completionHandler(NO, nil);
_completionHandler = nil;

}

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

- (void)buyProduct:(SKProduct *)product {

  NSLog(@"Buying %@...", product.productIdentifier);

   SKPayment * payment = [SKPayment paymentWithProduct:product];
  [[SKPaymentQueue defaultQueue] addPayment:payment];

}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction * transaction in transactions) {
    switch (transaction.transactionState)
    {
        case SKPaymentTransactionStatePurchased:
            [self completeTransaction:transaction];
            break;
        case SKPaymentTransactionStateFailed:
            [self failedTransaction:transaction];
            break;
        case SKPaymentTransactionStateRestored:
            [self restoreTransaction:transaction];
        default:
            break;
    }
};
}
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
  NSLog(@"completeTransaction...");

  [self provideContentForProductIdentifier:transaction.payment.productIdentifier];
  [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}

- (void)restoreTransaction:(SKPaymentTransaction *)transaction {
  NSLog(@"restoreTransaction...");

[self provideContentForProductIdentifier:transaction.originalTransaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}

- (void)failedTransaction:(SKPaymentTransaction *)transaction {

  NSLog(@"failedTransaction...");
  if (transaction.error.code != SKErrorPaymentCancelled)
  {
      NSLog(@"Transaction error: %@", transaction.error.localizedDescription);
  }

[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

- (void)provideContentForProductIdentifier:(NSString *)productIdentifier {
  NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  int currentHints = [userDefaults integerForKey:@"hintAmount"];
  [_purchasedProductIdentifiers addObject:productIdentifier];
  if ([productIdentifier  isEqual: @"1"]) {
    [userDefaults setInteger:(currentHints+50) forKey:@"hintAmount"];
  }else if ([productIdentifier  isEqual: @"2"]){
    [userDefaults setInteger:(currentHints+100) forKey:@"hintAmount"];
  }else if ([productIdentifier  isEqual: @"3"]){
    [userDefaults setInteger:(currentHints+200) forKey:@"hintAmount"];
  }else if ([productIdentifier  isEqual: @"4"]){
    [userDefaults setInteger:(currentHints+500) forKey:@"hintAmount"];
  }
  [[NSUserDefaults standardUserDefaults] synchronize];
  [[NSNotificationCenter defaultCenter] postNotificationName:IAPHelperProductPurchasedNotification object:productIdentifier userInfo:nil];

}


@end

2 个答案:

答案 0 :(得分:1)

仅供参考 - 您必须在设备上运行该应用才能正确获取IAP产品。我遇到的问题是它将所有产品标识符吐出“未购买”,但当我尝试在表格视图中显示时,却说“无法加载产品列表”。这是在模拟器中运行。一旦我在设备上运行,就完美地工作了!

我使用本教程来获取执行所有繁重操作的基本帮助程序文件: http://www.raywenderlich.com/21081/introduction-to-in-app-purchases-in-ios-6-tutorial

答案 1 :(得分:-1)

满足所有条件

1.您是否为应用程序ID启用了应用程序内购买?

2.您是否检查过您的产品已售出?

3.您是否已提交(并可选择拒绝)您的申请二进制文件?

4.您项目的.plist捆绑ID是否与您的App ID匹配?

5.您是否为新的App ID生成并安装了新的配置文件?

6.您是否已使用此新配置文件将项目配置为代码签名?

7.您是否正在为iPhone OS 3.0或更高版本构建?

8.您在制作SKProductRequest时是否使用完整的产品ID?

9.自从将产品添加到iTunes Connect后,您还等了几个小时?

10.您的银行详细信息在iTunes Connect上有效吗? (通过马克)

11.您是否尝试从设备中删除该应用并重新安装?

如果你对这些问题中的任何一个回答“否”,那就是你的问题。