我是In-App Purchase的新手,我遇到了问题。
现在购买流程如下:
UIAlertViewDelegate
方法购买。paymentQueue:updatedTransaction
的大小写为:SKPaymentTransactionStatePurchasing
。SKPaymentTransactionStatePurchased
调用相同的方法。我的功能在此状态下解锁(此状态应在购买流程结束时)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
}