2013-04-29 109 views
1

我想在一个应用中实现IAP,但我仍然遇到困难。我跟着各种教程,但他们都过时了,充满了错误。唯一一个可以工作,我发现是this one应用内购买,展示产品,但没有任何反应

,但我有一个问题,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. 
     } 
+0

请张贴您的代码。 – David 2013-04-29 15:52:19

+0

老兄,发布你的tableView:(UITableView *)tableView didSelectRowAtIndexPath方法。 – chandu 2013-04-29 16:00:51

回答

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

苹果在应用程序内购买处理提供了一个体面的step by step guide

+0

Thx,我认为教程缺少这样的东西...... – Blue 2013-04-29 18:05:00

1

主要方式来运行单边行动计划涉及到几种不同的方法,但也有需要推行单边行动计划时要遵循几个不同的步骤。

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

  • 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-loop中使用一个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]; 
    } 
} 

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

我已经在几个应用程序中成功地使用了这段代码。

+0

Thx对此,它是一种不同的方法,可能它对我的目的更好......但对我的情况正确的answare来自于beeBee – Blue 2013-04-29 18:06:22

相关问题