如何在应用内购买实现多个消费品

时间:2015-06-27 01:11:22

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

我已经成功实施了一种耗材产品,但我不知道如何实施多种耗材产品。我想添加更多ProductIdentifiers,如com.lalala.20batteries,com.lalala.30batteries

任何人都可以给我一些指南

这是我的单一消费品代码

@interface ViewController () <SKProductsRequestDelegate, SKPaymentTransactionObserver>

@end

@implementation ViewController


#define ProductIdentifier @"com.lalala.10batteries"

- (IBAction)taps10batteries{
   NSLog(@"User requests to get 10 batteries");

   if([SKPaymentQueue canMakePayments]){
      NSLog(@"User can make payments");

      SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:ProductIdentifier]];
      productsRequest.delegate = self;
      [productsRequest start];

   }
   else{
      NSLog(@"User cannot make payments due to parental controls");

   }
}



- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
   SKProduct *validProduct = nil;
   int count = [response.products count];
   if(count > 0){
      validProduct = [response.products objectAtIndex:0];
      NSLog(@"Products Available!");
      [self purchase:validProduct];
   }
   else if(!validProduct){
      NSLog(@"No products available");

   }
}


- (IBAction)purchase:(SKProduct *)product{
   SKPayment *payment = [SKPayment paymentWithProduct:product];

   [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
   [[SKPaymentQueue defaultQueue] addPayment:payment];


}



- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
   NSLog(@"received restored transactions: %i", queue.transactions.count);
   for(SKPaymentTransaction *transaction in queue.transactions){
      if(transaction.transactionState == SKPaymentTransactionStateRestored){
         //called when the user successfully restores a purchase
         NSLog(@"Transaction state -> Restored");

         [self get10Batteries];
         [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
         break;
      }
   }
}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
   for(SKPaymentTransaction *transaction in transactions){
      switch(transaction.transactionState){
         case SKPaymentTransactionStatePurchasing: NSLog(@"Transaction state -> Purchasing");

            break;
         case SKPaymentTransactionStatePurchased:

            [self get10Batteries]; 
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            NSLog(@"Transaction state -> Purchased");
            break;
         case SKPaymentTransactionStateRestored:
            NSLog(@"Transaction state -> Restored");

            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            break;
         case SKPaymentTransactionStateFailed:

            if(transaction.error.code == SKErrorPaymentCancelled){
               NSLog(@"Transaction state -> Cancelled");
               //the user cancelled the payment ;(
            }
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            break;
      }
   }
}


- (void)get10Batteries{

    NSUbiquitousKeyValueStore *cloudstore1 = [NSUbiquitousKeyValueStore defaultStore];

   //load cloud integer
   coins = [cloudstore1 doubleForKey:@"yo" ];



   coins = [[NSUserDefaults standardUserDefaults]
                       integerForKey:@"com.lalala.10batteries"];
   coins += 10;
   [[NSUserDefaults standardUserDefaults] setInteger:coins
                                              forKey:@"com.lalala.10batteries"];
   [[NSUserDefaults standardUserDefaults] synchronize];



  _coinLabel.text =  [NSString stringWithFormat:@"%li", (long)coins];



   //save icloud

   [cloudstore1 setDouble:coins forKey:@"yo"];

   [cloudstore1 synchronize];


}

- (void)viewDidLoad {
    [super viewDidLoad];

   NSUbiquitousKeyValueStore *cloudstore1 = [NSUbiquitousKeyValueStore defaultStore];
   _coinLabel.text = [cloudstore1 stringForKey:@"yo" ];


   NSUserDefaults *coinsdefaults = [NSUserDefaults standardUserDefaults];




   if([coinsdefaults objectForKey:@"com.lalala.10batteries"] != nil) {

      coins = [[NSUserDefaults standardUserDefaults] integerForKey:@"com.lalala.10batteries"];

      coins = [coinsdefaults integerForKey:@"com.lalala.10batteries"];

       _coinLabel.text =  [NSString stringWithFormat:@"%li", (long)coins];



   }



}

2 个答案:

答案 0 :(得分:2)

您的代码可以轻松支持多种产品,就像它支持一种产品一样...... 首先,声明您的其他产品ID,就像使用其中一样:

#define ProductIdentifier1 @"com.lalala.10batteries"
#define ProductIdentifier2 @"com.lalala.20batteries"

然后,创建一个loadAllProducts方法,它与taps10batteries方法完全相同,只不过您使用所有产品ID声明SKProductsRequest,就像使用一个产品ID一样:< / p>

SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObjects:ProductIdentifier1, ProductIdentifier2, nil]];

除非您更喜欢单独加载每个产品,否则您需要为每个产品创建一个方法,即taps20batteries,您可以忽略我的答案的下一部分......

回调方法可以返回多个产品,就像它可以返回一个产品一样。按照您的意愿处理响应,就像您使用其中一样:

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
   int count = [response.products count];
   if(count > 0){
       _products = [response.products sortedArrayUsingComparator:^(id a, id b) { //sort all products by price
            NSDecimalNumber *first = [(SKProduct*)a price];
            NSDecimalNumber *second = [(SKProduct*)b price];
            return [second compare:first];
        }];
       for (SKProduct *product in _products) {
         NSLog(@"Product Available: %@", product.productIdentifier);
         //do something with the product..
         //perhaps gather the products into an array and present it somehow to the user so s/he can select what to buy?
       }
   }
   else {
   NSLog(@"No products available");

   }
}

如果您想要一个可以使用的完整实现,请尝试this one

答案 1 :(得分:1)

查看您创建SKProductsRequest对象的行:

SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:ProductIdentifier]];

NSSet是一个袋子,里面有未分类的物品,袋子内只允许有一种(没有骰子)。

你所要做的就是改变它:

SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObjects:
                                  @"com.blahblah.unlockall",
                                  @"com.blahblah.sounds",
                                  @"com.blahblah.alarms",
                                  @"com.blahblah.wallpapers",
                                  nil]];  

然后你应该有一个自定义的包装类(CustomIAPHelper),如教程。

在视图控制器中,让我们说你有一个tableView,然后代码如下:

- (void)reload {
    _products = nil;
    [self.tableView reloadData];
    [CustomIAPHelper sharedInstance].delegate = self;
    [[CustomIAPHelper sharedInstance] requestProductsWithCompletionHandler:^(BOOL success, NSArray *products) {
        if (success) {

            // Sort the products array (by price)
           _products = [products sortedArrayUsingComparator:^(id a, id b) {
                NSDecimalNumber *first = [(SKProduct*)a price];
                NSDecimalNumber *second = [(SKProduct*)b price];
                return [second compare:first];
            }];

            [self.tableView reloadData];
        }
        [refreshControl endRefreshing];
        [activity stopAnimating];
        [activity setHidden:YES];
    }];
}

请记住设置和实施iAPHelper的协议方法。