方法叫多次

时间:2012-11-07 18:06:33

标签: iphone objective-c ios

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

几周前,我为我的应用创建了一个商店页面。我是应用内购买的新手,并获得了一些网络代码。这是针对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];

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

1 个答案:

答案 0 :(得分:0)

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