4

我正在使用以下代码在 Mac 应用中实现应用内购买。

        @implementation AppStoreObserver

    -(id) init {

        self = [super init];

        if (self) {

            _products = [[NSMutableArray alloc] init];

            [[SKPaymentQueue defaultQueue] addTransactionObserver:self];                
        }

        return self;
    }

    -(void)dealloc {

        [_products release];

        [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];

        [super dealloc];
    }

    #pragma mark - Query products

    -(void)loadStoreForProducts:(NSArray *)productIds {

        SKProductsRequest *productRequest = [[[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithArray:productIds]] autorelease];
        productRequest.delegate = self;
        [productRequest start];
    }

    #pragma mark - Product Delegate

    //Got product info
    - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
    {

        [_products removeAllObjects];

        for (SKProduct *_product in response.products) {
            [_products addObject:_product];
        }

        [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationStoreManagerProductsReceivedInvalid
                                                    object:nil
                                                  userInfo:[NSDictionary dictionaryWithObjectsAndKeys:response.invalidProductIdentifiers, @"invalidProductIdentifiers", _products, @"products", nil]];
    }

    #pragma mark - Purchase

    -(void) triggerPurchase:(SKProduct *) product {

        [[SKPaymentQueue defaultQueue] addPayment:[SKPayment paymentWithProduct:product]];
    }

    - (BOOL)canMakePurchases
    {
        return [SKPaymentQueue canMakePayments];
    }

    - (void)purchaseProduct:(NSString *)productId
    {
        if([self canMakePurchases]){

            for (SKProduct *_product in _products) {

                if ([_product.productIdentifier isEqualToString:productId]) {

                    [self triggerPurchase:_product];
                    break;
                }
            }        
        }
    }

    #pragma mark - Payment Queue observers

    - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
    {
         for (SKPaymentTransaction* transaction in transactions)
         {
            //NSLog(@"%d", transaction.transactionState);
              switch (transaction.transactionState)
              {
                case SKPaymentTransactionStatePurchasing:
                    NSLog(@"Purchasing");
                    break;

                   case SKPaymentTransactionStatePurchased:
                    NSLog(@"Purchased");
                        [self completeTransaction:transaction];
                        break;

                   case SKPaymentTransactionStateFailed:
                        NSLog(@"Error: %@", transaction.error);
                        [self failedTransaction:transaction];
                        break;

                   case SKPaymentTransactionStateRestored:
                    NSLog(@"Restored");
                    [self restoreTransaction:transaction];
                        break;

                   default:
                        break;
              }
         }
    }

    #pragma mark - Custom handlers

    - (void)restoreTransaction:(SKPaymentTransaction *)transaction
    {
        NSLog(@"restore transaction %@ %@",transaction.transactionIdentifier , transaction.originalTransaction);

        [self finishTransaction:transaction wasSuccessful:YES];
    }

    -(void) restoreCompletedTransactions {

        [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
    }

    - (void) completeTransaction: (SKPaymentTransaction *)transaction
    {
        [self finishTransaction:transaction wasSuccessful:YES];
    }

    - (void) failedTransaction: (SKPaymentTransaction *)transaction
    {
         if (transaction.error.code != SKErrorPaymentCancelled)
        {
            // error!
            [self finishTransaction:transaction wasSuccessful:NO];
        }
        else
        {
            // this is fine, the user just cancelled, so don’t notify
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            return;
        }

        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
        [alert addButtonWithTitle:@"OK"];
        [alert setMessageText:@"Alert"];
        [alert setInformativeText:@"In-App Purchase Failed"];
        [alert setAlertStyle:NSWarningAlertStyle];
        [alert runModal];
    }

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

        [[SKPaymentQueue defaultQueue] finishTransaction:transaction];

        //NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:transaction, @"transaction" , nil];
        if (wasSuccessful) {
            // send out a notification that we’ve finished the transaction
        } else {
            NSLog(@"user cancelled");
        }
    }

    @end

我能够成功检索 SKProduct,但是一旦我使用测试帐户购买产品,该过程就会停止而不会失败。已达到 SKPaymentTransactionStatePurchasing 状态,但之后没有任何反应。甚至没有显示购买确认对话框。

一旦我停止并重新运行应用程序,最后一个事务就会成功完成,但是一旦我尝试进行另一个事务,同样的事情就会发生。我花了几天时间试图找出原因,但没有成功。我希望这里有人能够找出问题所在。谢谢你的时间。

4

0 回答 0