如何在iPhone应用程序中添加多个应用内购买?

时间:2014-07-30 02:16:59

标签: objective-c in-app-purchase

好的,所以我基本上都遵循了这个链接中的教程 http://www.techotopia.com/index.php/An_iOS_7_In-App_Purchase_Tutorial 我也试过这个教程:How do you add an in-app purchase to an iOS application?但它对我不起作用。如果有人帮助我,我会非常感激。我将使用第一个链接,因为这是我使用的教程。它适用于应用程序购买中的一个,但是,我如何在应用程序购买中进行更多操作?更多关卡?本教程只教一个,所以我尝试按照相同的教程,但通过更改名称,它仍然无法正常工作。我得到的最接近的是它确实购买它,但即使我连接奥特莱斯也没有启用按钮。需要帮助,编码如下:

  

ViewController.h

enter code here#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>
#import "PurchaseViewController.h"
#import "SecondPurchaseViewController.h"

@interface MAGViewController : UIViewController

- (IBAction)purchaseItem:(id)sender;
@property (strong, nonatomic) IBOutlet UIButton *level2Button;
@property (strong, nonatomic) PurchaseViewController *purchaseController;

- (IBAction)SecondpurchaseItem:(id)sender;
@property (strong, nonatomic) IBOutlet UIButton *level3Button;
@property (strong, nonatomic) SecondPurchaseViewController *SecondpurchaseController;


-(void)enableLevel2;
-(void)enableLevel3;
@end

.m文件是:(我确实把#import“PurchaseViewController.h”放了,所以不是那样,我也放第二个。

- (IBAction)purchaseItem:(id)sender {

_purchaseController.productID =
@"com.example.IAP.courseone";

[self.navigationController
 pushViewController:_purchaseController animated:YES];

[_purchaseController getProductInfo: self];
}

- (IBAction)SecondpurchaseItem:(id)sender {

    _SecondpurchaseController.SecondproductID =
    @"com.example.IAP.coursetwo";

    [self.navigationController
     pushViewController:_SecondpurchaseController animated:YES];

    [_SecondpurchaseController getSecondProductInfo: self];
}

-(void)enableLevel2
{
    _level2Button.enabled = YES;
}

-(void)enableLevel3
{
    _level3Button.enabled = YES;
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    _purchaseController = [[PurchaseViewController alloc]init];

    [[SKPaymentQueue defaultQueue]
     addTransactionObserver:_purchaseController];

    //

    _SecondpurchaseController = [[SecondPurchaseViewController alloc]init];

    [[SKPaymentQueue defaultQueue]
     addTransactionObserver:_SecondpurchaseController];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

PurchaseViewController.h是

#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>

@interface PurchaseViewController : UIViewController <SKPaymentTransactionObserver, SKProductsRequestDelegate>

@property (strong, nonatomic) IBOutlet UILabel *productTitle;
@property (strong, nonatomic) IBOutlet UITextView *productDescription;
@property (strong, nonatomic) IBOutlet UIButton *buyButton;
- (IBAction)buyProduct:(id)sender;


@property (strong, nonatomic) SKProduct *product;
@property (strong, nonatomic) NSString *productID;

- (void)getProductInfo:(UIViewController *)viewController;

@end

PurchaseViewController.m是:

@interface PurchaseViewController ()
@property (strong, nonatomic) MAGViewController *homeViewController;
@end


@implementation PurchaseViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

//// -->

-(void)getProductInfo: (MAGViewController *) viewController
{
    _homeViewController = viewController;

    if ([SKPaymentQueue canMakePayments])
    {
        SKProductsRequest *request = [[SKProductsRequest alloc]
                                      initWithProductIdentifiers:
                                      [NSSet setWithObject:self.productID]];
        request.delegate = self;

        [request start];
    }
    else
        _productDescription.text =
        @"Please enable In App Purchase in Settings";
}

//
//

#pragma mark -
#pragma mark SKProductsRequestDelegate

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

    NSArray *products = response.products;

    if (products.count != 0)
    {
        _product = products[0];
        _buyButton.enabled = YES;
        _productTitle.text = _product.localizedTitle;
        _productDescription.text = _product.localizedDescription;
    } else {
        _productTitle.text = @"Product not found";
    }

    products = response.invalidProductIdentifiers;

    for (SKProduct *product in products)
    {
        NSLog(@"Product not found: %@", product);
    }
}

////

- (IBAction)buyProduct:(id)sender {
    SKPayment *payment = [SKPayment paymentWithProduct:_product];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}

////

#pragma mark -
#pragma mark SKPaymentTransactionObserver

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState) {
            case SKPaymentTransactionStatePurchased:
                [self unlockFeature];
                [[SKPaymentQueue defaultQueue]
                 finishTransaction:transaction];
                break;

            case SKPaymentTransactionStateRestored:
                NSLog(@"Transaction state -> Restored");
                //add the same code as you did from SKPaymentTransactionStatePurchased here
                [self unlockFeature];
                [[SKPaymentQueue defaultQueue]
                 finishTransaction:transaction];
                break;

            case SKPaymentTransactionStateFailed:
                NSLog(@"Transaction Failed");
                [[SKPaymentQueue defaultQueue]
                 finishTransaction:transaction];
                break;

            default:
                break;
        }
    }
}

////

-(void)unlockFeature
{
    _buyButton.enabled = NO;
    [_buyButton setTitle:@"Purchased"
                forState:UIControlStateDisabled];
    [_homeViewController enableLevel2];
}

- (IBAction) restore{
    //this is called when the user restores purchases, you should hook this up to a button
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}

- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
    NSLog(@"received restored transactions: %lul", queue.transactions.count);
    for (SKPaymentTransaction *transaction in queue.transactions)
    {
        if(SKPaymentTransactionStateRestored){
            NSLog(@"Transaction state -> Restored");
            //called when the user successfully restores a purchase
            [self unlockFeature];
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            break;
        }

    }

}

//// <--



- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _buyButton.enabled = NO;

}

如何让它适用于多个IAP购买? ray wenderlick教程在表视图中显示,但我不想要表视图,我想要自定义背景的自定义按钮,所以这个教程很棒,但我不知道如何使它工作不止一个iap。如果你知道答案,请提前感谢你,请评论。我已经挣扎了两天多了。

1 个答案:

答案 0 :(得分:0)

在.h文件中添加此方法。

 + (RageIAPHelper *)sharedInstance;

在实施文件.m

+ (RageIAPHelper *)sharedInstance {
  static dispatch_once_t once;
  static RageIAPHelper * sharedInstance;
  dispatch_once(&once, ^{
      NSSet * productIdentifiers = [NSSet setWithObjects:
                                  @"com.razeware.inapprage.drummerrage",
                                  @"com.razeware.inapprage.itunesconnectrage",
                                  @"com.razeware.inapprage.nightlyrage",
                                  @"com.razeware.inapprage.studylikeaboss",
                                  @"com.razeware.inapprage.updogsadness",
                                  nil];
      sharedInstance = [[self alloc] initWithProductIdentifiers:productIdentifiers];
   });
  return sharedInstance;
}

通过这个我们可以获得更多的productIdentifiers。

按照本教程Click Here

进行操作

希望这会对你有所帮助。