1

我正在尝试在我的应用程序中使用 iap。这些是用游戏金币购买的,可以多次使用。我从菜鸟教程中编写了代码,它适用于那里的项目,有人可以告诉我我做错了什么吗?

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

然后它就停止了,没有从苹果那里得到任何东西。

4

2 回答 2

0

您确定com.emirbytes.IAPNoob.01在您的iTunesConnect.

com.emirbytes.IAPNoob.01如果您已创建,请检查您创建的产品 ID 的拼写iTunesConnect

更新1:Bundle ID不同,Product ID不同。

Bundle ID是用于 App 的唯一标识符。Product ID是用于购买产品列表的唯一标识符。

假设com.emirbytes.IAPNoob是 bundleID 和

  1. com.emirbytes.IAPNoob.swords - 是购买剑的产品ID
  2. com.emirbytes.IAPNoob.soldiers- 是购买士兵的产品ID

要创建Product ID,您必须前往iTunesConnect并创建一个应用程序,并且必须在 中设置产品 ID Manage In-App Purchases

于 2013-05-24T04:06:37.543 回答
0

确保您的委托已设置,您已设置request.delegate = self;但您正在使用self类方法。我不明白这是如何工作的。

更改+(void)myIAPWithItem:(NSString *)Item为实例方法-(void)myIAPWithItem:(NSString *)Item,然后实例化 Money 类并调用该方法。看看是否有帮助。

于 2013-05-23T02:11:59.783 回答