1

我第一次购买 3 个提示时效果很好,如果我尝试第二次或第三次使用它,它会一直说“您已购买 3 个提示!” 几次,我找不到它有什么问题

#import "HSStore.h"
@implementation HSStore

-(IBAction)purchaseHints:(id)sender {

    askToPurchase = [[UIAlertView alloc] 
                     initWithTitle:@"Purchase Hints" 
                     message:@"Purchase 3 Hints?"
                     delegate:self 
                     cancelButtonTitle:nil
                     otherButtonTitles:@"Yes", @"No", nil]; 
    askToPurchase.delegate = self;
    [askToPurchase show];
}

#pragma mark StoreKit Delegate
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions       {
    for (SKPaymentTransaction *transaction in transactions) {
        switch (transaction.transactionState) {

            case SKPaymentTransactionStatePurchasing:{
            }
                break;
            case SKPaymentTransactionStatePurchased:{
                UIAlertView *tmp = [[UIAlertView alloc] 
                                    initWithTitle:@"Complete" 
                                    message:@"You have purchased 3 Hints!"
                                    delegate:self 
                                    cancelButtonTitle:nil 
                                    otherButtonTitles:@"Ok", nil]; 
                [tmp show];


                //Add Hints
                [self addHints];
                [self completeTransaction:transaction];

                }   

                break;
            case SKPaymentTransactionStateRestored:{
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            }

                break;



            case SKPaymentTransactionStateFailed:{



                if (transaction.error.code != SKErrorPaymentCancelled) {

                   NSLog(@"Error payment cancelled %d",transaction.error.code);

                }

                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];

            }

                break;



            default:

                break;

        }

    }

}





- (void) completeTransaction: (SKPaymentTransaction *)transaction

{



    // Remove the transaction from the payment queue.

    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];

}





- (void)finishTransaction:(SKPaymentTransaction *)transaction wasSuccessful:(BOOL)wasSuccessful

{
    // remove the transaction from the payment queue.

    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response  
{  

    SKProduct *validProduct = nil;

    int count = [response.products count];

   if (count>0) {

        validProduct = [response.products objectAtIndex:0];
        SKPayment *payment = [SKPayment paymentWithProduct:validProduct];
        [[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

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView==askToPurchase) {
        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:@"Buy3Hints"]];  
                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];
            }

        }
    }
}

-(void) addHints {
    //Load Hints

    NSUserDefaults *totalHints = [NSUserDefaults standardUserDefaults];

    NSNumber *loadHints = [totalHints objectForKey:@"hintsTotal"];

    hintsLeft = [loadHints intValue];

    //Add 3 Hints
    hintsLeft = hintsLeft + 3;
    //Store Hints Left
    NSNumber *storeHints = [NSNumber numberWithInt:hintsLeft];

    NSUserDefaults *storeTotalHints = [NSUserDefaults standardUserDefaults];

    [storeTotalHints setObject:storeHints forKey:@"hintsTotal"];

    [storeTotalHints synchronize];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

     // Do any additional setup after loading the view, typically from a nib.
}



- (void)viewDidUnload
{
    [super viewDidUnload];

    // Release any retained subviews of the main view.
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
4

0 回答 0