我正在尝试在我的应用中使用iap。这些都是以游戏黄金购买,可以使用一次以上。我从noob教程编写代码并且它可以在那里工作项目有人可以告诉我我做错了吗?
#import "IAP.h"
#import "Money.h"
@interface IAP ()
@end
@implementation IAP;
#define kStoredData @"com.AlexApps.TinyTrucks"
+(void)myIAPWithItem:(NSString *)Item{
if ([SKPaymentQueue canMakePayments]) {
NSString *PurchaseAddress = [[NSString alloc] initWithString:[NSString stringWithFormat:@"TTE_%@kGold" , Item]];
//PurchaseAddress is the appId for this in app purchase
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:PurchaseAddress]];
request.delegate = self;
[request start];
} else {
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:@"Prohibited"
message:@"Parental Control is enabled, cannot make a purchase!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
[tmp show];
}
}
#pragma mark StoreKit Delegate
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:{
// show wait view here
break;
}
case SKPaymentTransactionStatePurchased:{
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view and unlock feature 2
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:@"Complete"
message:@"You have unlocked Feature 2!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
[tmp show];
// apply purchase action - hide lock overlay and
NSLog(@"befvsda");
// do other thing to enable the features
break;
}
case SKPaymentTransactionStateRestored:{
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view here
break;
}
case SKPaymentTransactionStateFailed:{
if (transaction.error.code != SKErrorPaymentCancelled) {
NSLog(@"Error payment cancelled");
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view here
break;
}
default:{
break;
}
}
}
}
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
// remove wait view here
SKProduct *validProduct = nil;
int count = [response.products count];
if (count>0) {
validProduct = [response.products objectAtIndex:0];
SKPayment *payment = [SKPayment paymentWithProductIdentifier:@"com.emirbytes.IAPNoob.01"];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
} else {
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:@"Not Available"
message:@"No products to purchase"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
[tmp show];
}
}
-(void)requestDidFinish:(SKRequest *)request
{
}
-(void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
NSLog(@"Failed to connect with error: %@", [error localizedDescription]);
}
#pragma mark AlertView Delegate
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
程序到达这一行:
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:PurchaseAddress]];
request.delegate = self;
[request start];
然后它就停止了,并且没有从苹果那里得到任何回报。
答案 0 :(得分:0)
确保您的委托已设置,您有request.delegate = self;
,但您在类方法中使用self
。我不知道那是怎么回事。
将+(void)myIAPWithItem:(NSString *)Item
更改为实例方法-(void)myIAPWithItem:(NSString *)Item
,然后实例化Money类并调用该方法。看看是否有帮助。
答案 1 :(得分:0)
您确定在com.emirbytes.IAPNoob.01
中为产品ID iTunesConnect
创建了设置。
如果您已创建,请检查您在com.emirbytes.IAPNoob.01
iTunesConnect
的拼写
更新1:否Bundle ID
不同,Product ID
不同。
Bundle ID
是App使用的唯一标识符。 Product ID
是用于购买产品列表的唯一标识符。
假设com.emirbytes.IAPNoob
是bundleID和
com.emirbytes.IAPNoob.swords
- 是购买剑的产品ID com.emirbytes.IAPNoob.soldiers
- 是购买士兵的产品ID 要创建Product ID
,您必须转到iTunesConnect并创建一个应用,并且必须在Manage In-App Purchases
中设置产品ID。