如何在iPhone应用程序中添加多个应用内购买? [英] How do you add multiple in-app purchases to an iPhone application?
问题描述
好的,所以我基本上按照这个链接中的教程
http: //www.techotopia.com/index.php/An_iOS_7_In-App_Purchase_Tutorial
我也试过这个教程:如何在iOS应用程序中添加应用内购买?但它对我不起作用。如果有人帮助我,我会非常感激。我将使用第一个链接,因为这是我使用的教程。它适用于应用程序购买中的一个,但是,我如何在应用程序购买中进行更多操作?更多关卡?本教程只教一个,所以我尝试按照相同的教程,但通过更改名称,它仍然无法正常工作。我得到的最接近的是它购买它,但即使我连接奥特莱斯也没有启用按钮。需要帮助,编码如下:
okay, So i basically followed the tutorial in this link http://www.techotopia.com/index.php/An_iOS_7_In-App_Purchase_Tutorial I also tried the tutorial in this one: How do you add an in-app purchase to an iOS application? but it did not work for me. I would really appreciate it if someone helps me out. I'm going to use the first link because that's the tutorial I used. It works fine for one in app purchase, BUT, how do i do it for more in app purchases? for more levels? The tutorial only teaches one, and so I try and follow the same tutorial but by changing the names, and it STILL doesn't work. The closest I got was it does purchase it, but it does not enable the button even though I connected the Outlets. Need help, the coding is provided below:
ViewController.h
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所以它不是那个,我也把第二个。也好。
And the .m file is: (i did put the #import "PurchaseViewController.h" so it's not that and i also put the Second one as well.
- (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是
PurchaseViewController.h is
#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是:
PurchaseViewController.m is:
@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。如果你知道答案,请提前感谢你,请评论。我已经困难了2天。
How do i make it work for more than one IAP purchase? The ray wenderlick tutorial shows in a table view, but i don't want a table view, i want custom buttons with a custom background of my own, so this tutorial is great, but i don't know how to make it work with more than one iap. thank you in advance if u know the answer, please comment. i've been struggling over 2 days already.
推荐答案
在.h文件中添加此方法。
In .h file add this method.
+ (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。
by this we can get more productIdentifiers.
按照本教程点击这里
希望这对你有帮助。
这篇关于如何在iPhone应用程序中添加多个应用内购买?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!