0

很长的问题,但很容易理解,所以请花点时间:)

几周前,我为我的应用创建了一个商店页面。我是应用内购买的新手,并获得了一些网络代码。这是为 1 个对象制作的,所以我决定根据自己的需要编辑代码。一次购买应用程序的专业版,购买 3 次购买 3 种不同数量的硬币。

当我购买硬币时,它可以正常工作,唯一的问题是我第二次购买它会增加我购买的硬币数量的 2 倍,第三次它将增加我购买的硬币数量的 3 倍等等。我希望有人可以看看在我的代码中并告诉我出了什么问题。对于这个主题,我将代码稍微小了一点,并且不会显示所有购买。

//the 100 coins button
-(IBAction)savedata100:(id)sender {
        askToPurchase100Munten = [[UIAlertView alloc] 
                         initWithTitle:@"100 Munten" 
                         message:@"Ga verder om 100 Munten te kopen."
                         delegate:self 
                         cancelButtonTitle:nil
                         otherButtonTitles:@"Verder", @"Cancel", nil]; 
        askToPurchase100Munten.delegate = self;
        [askToPurchase100Munten show];
        [askToPurchase100Munten release];

//下一步

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

else if (alertView==askToPurchase100Munten) {
    if (buttonIndex==0) {
        // user tapped YES, but we need to check if IAP is enabled or not.
        if ([SKPaymentQueue canMakePayments]) { 

            SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:@"100Munten"]];  

            Temp = 2;
            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];
            [tmp release];
        }
    }
}

//下一步

-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:    (SKProductsResponse *)response  
{ 
statusLabel.text = @"";
int count = [response.products count];
}
if (Temp == 2){
    if (count>0) {

        SKProduct *selectedProduct = [response.products objectAtIndex:0];
        SKPayment *payment = [SKPayment paymentWithProduct:selectedProduct];
        [[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];
        [tmp release];
    }
}

//最后一步这是每次我做的时候被调用的次数越来越多(uiView也弹出更多次)

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
    switch (transaction.transactionState) {
        case SKPaymentTransactionStatePurchasing:

            // show wait view here
            statusLabel.text = @"Verwerken...";
            break;

        case SKPaymentTransactionStatePurchased:

            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            // remove wait view and unlock feature 2

            statusLabel.text = @"Klaar!";

            if (Temp == 2){
                //ADD COINS WHEN PURCHASES IS COMPLETE
                int coinsToAdd = 100;
                 int currentCoins = [[NSUserDefaults standardUserDefaults] integerForKey:@"savedstring"];
                 int savestring =
                 currentCoins + coinsToAdd;
                 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setInteger:savestring forKey:@"savedstring"]; [defaults synchronize];
                 [label111 setText:[NSString stringWithFormat:@"Munten: %i",[self getCoins]]];

                //ALERTVIEW TO SHOW YOU PURCHASED COINS
                UIAlertView *tmp2 = [[UIAlertView alloc]
                                    initWithTitle:@"Voltooid" 
                                    message:@"Je hebt 100 Munten Gekocht"
                                    delegate:self 
                                    cancelButtonTitle:nil 
                                    otherButtonTitles:@"Ok", nil]; 
                [tmp2 show];
                [tmp2 release];

感谢阅读希望有人可以帮助我:)

4

1 回答 1

0

完成交易后,您应该将 transaction.transactionState 设置为 SKPaymentTransactionStatePurchased,否则您将在下一次重复交易。

于 2012-11-07T18:45:25.197 回答