恢复应用购买问题

时间:2014-08-03 02:35:22

标签: ios in-app-purchase

我已成功实施广告移除非消费类应用内购买。我读到我的应用程序中还必须有一个“恢复”按钮。我已经尝试过去两天实现恢复功能,阅读stackoverflow上的所有其他帖子,没有任何对我有用。按下恢复按钮后,它会按预期请求电子邮件和密码,但即使我之前没有在该Apple ID上购买该产品,它仍将恢复它。以下是我正在使用的代码。任何帮助将不胜感激!

- (IBAction)RestoreProduct:(id)sender {


[[SKPaymentQueue defaultQueue] addTransactionObserver:self];

[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];

NSLog(@"Calling Restore");

_restoreButton.enabled = NO;

_productTitle.text = [NSString stringWithFormat:@"Checking for product"];


}




- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
NSLog(@"Calling First Function");
NSMutableArray *purchasedItemIDs = [[NSMutableArray alloc] init];

NSLog(@"received restored transactions: %i", queue.transactions.count);
for (SKPaymentTransaction *transaction in queue.transactions)
{
    NSString *productID = transaction.payment.productIdentifier;
    [purchasedItemIDs addObject:productID];
    NSLog(@"This Part");
    [self unlockFeature];
    _productTitle.text = [NSString stringWithFormat:@"Purchase Restored!"];
}

}




- (void)paymentQueue:(SKPaymentQueue*)queue restoreCompletedTransactionsFailedWithError:    (NSError*)error
{
NSLog(@"error");
}

EDIT。

好的,这是我从恢复按钮到@end的所有代码。

- (IBAction)RestoreProduct:(id)sender {


[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];

NSLog(@"Calling Restore");

_restoreButton.enabled = NO;

_productTitle.text = [NSString stringWithFormat:@"Checking for product"];


}

- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
NSLog(@"Calling First Function");
NSMutableArray *purchasedItemIDs = [[NSMutableArray alloc] init];

NSLog(@"received restored transactions: %i", queue.transactions.count);
for (SKPaymentTransaction *transaction in queue.transactions)
{
    NSString *productID = transaction.payment.productIdentifier;
    [purchasedItemIDs addObject:productID];
    NSLog(@"This Part");

}

}

- (void)paymentQueue:(SKPaymentQueue*)queue restoreCompletedTransactionsFailedWithError:    (NSError*)error
{
NSLog(@"error");
}

#pragma mark -
#pragma mark SKPaymentTransactionObserver

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{

for (SKPaymentTransaction *transaction in transactions)
{
    switch (transaction.transactionState) {
        case SKPaymentTransactionStatePurchased:
            [self unlockFeature];
            _productTitle.text = [NSString stringWithFormat:@"Purchase Complete!"];
            [[SKPaymentQueue defaultQueue]
             finishTransaction:transaction];
            break;

        case SKPaymentTransactionStateFailed:
            NSLog(@"Transaction Failed");
            [[SKPaymentQueue defaultQueue]
             finishTransaction:transaction];
            break;

        default:
            break;
    }
}
}

-(void)unlockFeature
{
_buyButton.enabled = NO;
[_buyButton setTitle:@"Purchased"
            forState:UIControlStateDisabled];
[_homeViewController enableLevel2];
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
_buyButton.enabled = NO;
_restoreButton.enabled = NO;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

1 个答案:

答案 0 :(得分:0)

我有一个调用

的按钮
+ (void)restoreABC
{
    NSLog(@"Restore Purchases");
   [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}

我猜你的问题是你也在打电话

[[SKPaymentQueue defaultQueue] addTransactionObserver:self];

紧接在上面:

[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];

删除第一行,你很可能会去。

编辑: 根据下面的评论,[self unlockfeature]行的声音是罪魁祸首: - )

编辑#2:

添加

    case SKPaymentTransactionStateRestored:
        NSLog(@"Feature Previously Purchased");
        [self unlockFeature];
        break;

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions