2

我是应用内购买的新手,我遇到了问题。

现在购买流程如下:

  1. 单击“是”并准备使用UIAlertViewDelegate方法购买。
  2. paymentQueue:updatedTransaction用 case: 调用SKPaymentTransactionStatePurchasing
  3. Next 调用与 case 相同的方法SKPaymentTransactionStatePurchased。我的功能在此状态下已解锁(此状态应在购买过程结束时)
  4. 有关购买的密钥存储在钥匙串中。
  5. 再次称为步骤 2。
  6. 这是关于购买功能的问题(由 StoreKit 生成)。购买前已解锁功能 - 用户现在可以取消购买。
  7. 最后,我有上述方法与案例SKPaymentTransactionStateFailed,因为项目是在步骤 3 中购买的。

问题出在哪里?我该如何解决?

这是我的代码:

#pragma mark - UIAlertViewDelegate Methods
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

if (alertView == askToPurchase) {

    if ([SKPaymentQueue canMakePayments]) {
    //prepare to purchase            
        SKProductsRequest *request =[[SKProductsRequest alloc] initWithProductIdentifiers:
                                     [NSSet setWithObject:completeIdentifier]];
        request.delegate = self;
        [request start];
    }
    else {
    //...
    }
}
}

#pragma mark - SKProductsRequestDelegate Methods
- (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 paymentWithProductIdentifier:completeIdentifier];
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}
else {
// item not available.
}
}


#pragma mark - SKPaymentTransactionObserver Methods
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {

for (SKPaymentTransaction *transaction in transactions) {

    switch (transaction.transactionState) {

        case SKPaymentTransactionStatePurchasing:
        {
           //processing...            
            break;
        }

        case SKPaymentTransactionStatePurchased:
        {
    //purchased.

            NSError *error = nil;
            [STKeychain storeUsername:identifier andPassword:@"Purchased" forServiceName:[[NSBundle mainBundle] bundleIdentifier] updateExisting:YES error:&error];

            if (error) {

                // Error with store key in keychain.
            }

            [self.delegate TSIAPHelperResponse:[NSDictionary dictionaryWithObjectsAndKeys:
                                               identifier, @"identifier",
                                               [NSNumber numberWithBool:TRUE], @"success",
                                               nil]];
            break;
        }

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

        case SKPaymentTransactionStateFailed:
        {
            if (transaction.error.code != SKErrorPaymentCancelled) {
        //failure..                   
            }

            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
    //cancelled.            
            break;
        }

        default:
            break;
    }
}
}

- (void)requestDidFinish:(SKRequest *)request {

}

- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {
// failed to connect
}
4

1 回答 1

1

我认为本教程是正确描述应用内购买购买过程的最佳教程

应用内购买

于 2013-01-02T12:27:43.830 回答