我是应用内购买的新手,我遇到了问题。
现在购买流程如下:
- 单击“是”并准备使用
UIAlertViewDelegate
方法购买。 paymentQueue:updatedTransaction
用 case: 调用SKPaymentTransactionStatePurchasing
。- Next 调用与 case 相同的方法
SKPaymentTransactionStatePurchased
。我的功能在此状态下已解锁(此状态应在购买过程结束时) - 有关购买的密钥存储在钥匙串中。
- 再次称为步骤 2。
- 这是关于购买功能的问题(由 StoreKit 生成)。购买前已解锁功能 - 用户现在可以取消购买。
- 最后,我有上述方法与案例
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
}