我有问题从app商店购买普通商品。我可以购买产品并验证收据,但是当我尝试恢复它们时,购买的交易清单是空的。 :( 任何的想法?我忘记了什么或者它在沙箱中不起作用?
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray*)transactions
{
//Is only fired when buying and not restoring.
for (SKPaymentTransaction * transaction in transactions) {
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
default:
break;
}
};
}
- (void)restoreCompletedTransactions {
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue]restoreCompletedTransactions];
}
-(void)completeTransaction:(SKPaymentTransaction *)transaction {
NSLog(@"completeTransaction...");
[self validateReceiptForTransaction:transaction];
_isPurchased = YES;
[self provideContentForProductIdentifier:transaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
答案 0 :(得分:0)
SKPaymentTransactionOBserver:
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
NSLog(@"Prossing.............");
break;
case SKPaymentTransactionStatePurchased:
{
[self completeTransaction:transaction];
NSError* error;
NSDictionary* jsonDict = [NSJSONSerialization
JSONObjectWithData:transaction.transactionReceipt
options:kNilOptions
error:&error];
NSLog(@"JSON Receipt: %@",jsonDict);
[[NSUserDefaults standardUserDefaults] setObject:jsonDict forKey:@"A"];
NSLog(@"Purchase was a Success.....");
}
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
NSLog(@"Purchase cancelled");
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
default:
break;
}
}
}
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
NSLog(@"completeTransaction...");
[self validateReceiptForTransaction:transaction];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
- (void)restoreTransaction:(SKPaymentTransaction *)transaction {
NSLog(@"restoreTransaction...");
[self validateReceiptForTransaction:transaction];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
- (void)failedTransaction:(SKPaymentTransaction *)transaction {
NSLog(@"failedTransaction...");
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
if (transaction.error.code != SKErrorPaymentCancelled)
{
NSLog(@"Transaction error: %@", transaction.error.localizedDescription);
}
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
- (void)provideContentForProductIdentifier:(NSString *)productIdentifier {
if ([productIdentifier isEqualToString:kMDPulseSubscriptionProductIdentifier]) {
[self purchaseSubscriptionWithMonths:1];
}
[[NSNotificationCenter defaultCenter] postNotificationName:IAPHelperProductPurchasedNotification object:productIdentifier userInfo:nil];
}
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error {
NSLog(@"%s","User Cancel.");
}
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
NSLog(@"Restore completed transactions finished.");
NSLog(@" Number of transactions in queue: %d", [[queue transactions] count]);
for (SKPaymentTransaction *trans in [queue transactions])
{
NSLog(@" transaction id %@ for product %@.", [trans transactionIdentifier], [[trans payment] productIdentifier]);
NSLog(@" original transaction id: %@ for product %@.", [[trans originalTransaction] transactionIdentifier],
[[[trans originalTransaction] payment]productIdentifier]);
if ([[[trans payment] productIdentifier] isEqual: kMDPulseSubscriptionProductIdentifier]) {
NSLog(@"Purchase Restored");
// Do your stuff to unlock
}
}
}
- (void)restoreCompletedTransactions
{
NSLog(@"Restore Tapped in transaction process");
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
答案 1 :(得分:0)
当您调用此方法进行恢复时
- (无效)restoreCompletedTransactions
{
NSLog(@"Restore Tapped in transaction process");
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
//然后将触发此委托函数
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)队列
{
alert = [[UIAlertView alloc] initWithTitle:@"IN APP PURCHASE" message:@"Processing" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[alert addSubview:progress];
[progress startAnimating];
[alert show];
[progress release];
for (SKPaymentTransaction *transaction in queue.transactions)
{
// Use this Commented code to Log and check details about your Products
/**
NSLog(@"%d",queue.transactions.count);
NSMutableArray * purchasedItemIDs = [[NSMutableArray alloc] init];
for (SKPaymentTransaction *transaction in queue.transactions)
{
NSString *productID = transaction.payment.productIdentifier;
[purchasedItemIDs addObject:productID];
NSLog(@"%@",purchasedItemIDs);
}*/
if( queue.transactions.count == 0 ) {
// User hav't purchased yet.
NSLog(@"User hav't purchased yet.");
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Restore" message:@"You have not purchased yet"
delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:Nil, nil];
[alertView show];
[alertView release];
}else {
// Upgrade to Full version.
NSLog(@"Upgrade to Full version.");
// Do stuffs here
//finally finish transaction
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
}
[alert dismissWithClickedButtonIndex:0 animated:YES];
}
希望这会有所帮助!!