1

我正在尝试在一个应用程序中实现 IAP,但我仍然遇到困难。我遵循了各种教程,但它们都已经过时并且充满了错误。我发现唯一可以工作的是这个

但是我遇到了一个问题,这 3 个产品出现在我的 tableview 上,但是当我点击其中一个时,什么也没有发生……单元格变成蓝色,仅此而已……我错过了什么吗?

还是那个教程不完整?

如何运行购买尝试?

这是我的代码:

   -(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
     {
      [productDetailsList addObjectsFromArray: response.products];
      [productDisplayTableView reloadData];
     }


     -(void)request:(SKRequest *)request didFailWithError:(NSError *)error
      {
         NSLog(@"Failed to connect with error: %@", [error localizedDescription]);
       }    


       - (NSInteger)tableView:(UITableView *)tableView
   numberOfRowsInSection:(NSInteger)section
       {
        return [self.productDetailsList count];
       }
       - (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
       {
           static NSString *GenericTableIdentifier = @"GenericTableIdentifier";
           UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: GenericTableIdentifier];
           if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: GenericTableIdentifier];
           }
           NSUInteger row = [indexPath row];
           SKProduct *thisProduct = [productDetailsList objectAtIndex:row];
           [cell.textLabel setText:[NSString stringWithFormat:@"%@ - %@",            thisProduct.localizedTitle, thisProduct.price]];
           return cell;
       }



        - (void)viewDidLoad
       {
           productDetailsList    = [[NSMutableArray alloc] init];
           productIdentifierList = [[NSMutableArray alloc] init];
           for (short item_count=1; item_count <= 5; item_count++) {
               [productIdentifierList addObject:[NSString              stringWithFormat:@"com.mycompany.myapp.%d", item_count]];
           }
           SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithArray:productIdentifierList]];
           request.delegate = self;
           [request start];

           [super viewDidLoad];
           // Do any additional setup after loading the view from its nib.
       }
4

2 回答 2

2

你需要有一些东西:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
if ([SKPaymentQueue canMakePayments])
{
    SKProduct *selectedProduct = [self.productDetailsList objectAtIndex:indexPath.row];
    SKPayment *payment = [SKPayment paymentWithProduct:selectedProduct];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}
}

Apple为处理应用程序购买提供了一个不错的分步指南。

于 2013-04-29T17:22:51.247 回答
1

运行 IAP 的主要方法涉及几种不同的方法,但在实施 IAP 时需要遵循几个不同的步骤。

这些要求中的第一个是协议。请在您的头文件中包含以下每个协议。

  • SKProductsRequestDelegate
  • SKPaymentTransactionObserver
  • SKRequestDelegate

您需要请求方法:

-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    if(response.products.count > 0)
    {
        SKProduct* product;

        for(int i = 0; i<response.products.count; i++)
        {
            product = [response.products objectAtIndex:i];

            if([product.productIdentifier isEqualToString:@"product identifier"])
            {
                self.currentProduct = product;
                [self beginPaymentWithProduct:product];
            }
        }
    }
}

我使用 if 语句来跟踪正在购买的产品。如果您有多个产品,您将需要在该 for 循环中为每个产品标识符添加一个 if 语句。稍后使用它来解锁购买完成后的任何内容。

您还需要 beginPayment 方法:

- (void)beginPaymentWithProduct:(SKProduct*)product
{
    SKPayment *payment = [SKPayment paymentWithProduct:product];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}

您还需要付款处理方法。我不会把它们都贴在这里,因为这会占用太多空间,但我会给你原型。

-(void)requestDidFinish:(SKRequest *)request;
-(void)request:(SKRequest *)request didFailWithError:(NSError *)error;
- (void)recordTransaction:(SKPaymentTransaction *)transaction;
- (void)finishTransaction:(SKPaymentTransaction *)transaction wasSuccessful:(BOOL)wasSuccessful;
- (void)completeTransaction:(SKPaymentTransaction *)transaction;
- (void)restoreTransaction:(SKPaymentTransaction *)transaction;
- (void)failedTransaction:(SKPaymentTransaction *)transaction;

对于表中应该购买的每个按钮,他们将需要在方法上执行类似于此的didSelectRowAtIndex...方法:

- (void)buyCoins:(id)sender
{
    if([self canMakePurchases])
    {
        ualRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithArray:[NSArray arrayWithObjects: @"product identifier", nil]]];
        [ualRequest setDelegate:self];
        [ualRequest start];
    }
}

此方法将成功运行产品请求。如果你有所有这些组件,你应该没有问题。

我已在多个应用程序中成功使用此代码。

于 2013-04-29T16:06:24.143 回答