在应用购买中恢复的方法是解锁尚未购买的功能

时间:2013-08-07 04:40:37

标签: ios objective-c in-app-purchase

我有一个非消耗性IAP用于“保存功能”。它最近被拒绝,因为我没有在应用程序中包含还原功能。我添加了用于恢复的代码,但是当我创建了一个未购买应用程序并按下恢复按钮的新测试用户时,保存功能已解锁。

以下是我检查用户是否购买了保存功能的方法

 if (![userDefaults boolForKey:@"isPurchase"]){
        [self purchaseMyProduct:[validProducts objectAtIndex:0]];
    }
    else { ... }

这些是我为恢复IAP而添加的方法。

- (void)restorePurchases { //called when button pressed

    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue]restoreCompletedTransactions];

}

- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {

    for (SKPaymentTransaction *transaction in queue.transactions) {
        NSString *productID = transaction.payment.productIdentifier;

        if ([productID isEqualToString:@"facePuppetsSave"]) {
            NSLog(@"called");
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setBool:YES forKey:@"isPurchase"];
            [userDefaults synchronize];
        }
    }          
}

以下是我已有的方法。

 -(void)paymentQueue:(SKPaymentQueue *)queue
updatedTransactions:(NSArray *)transactions {
    for (SKPaymentTransaction *transaction in transactions) {
        switch (transaction.transactionState) {
            case SKPaymentTransactionStatePurchasing:
                break;
            case SKPaymentTransactionStatePurchased:
                if ([transaction.payment.productIdentifier
                     isEqualToString:@"facePuppetsSave"]) {

                    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                    [userDefaults setBool:YES forKey:@"isPurchase"];
                    [userDefaults synchronize];

                    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:
                                              @"Purchase completed. Saving now available." message:nil delegate:
                                              self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
                    [alertView show];
                    [alertView release];
                }
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored:
                NSLog(@"Restored ");
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                break;
            default:
                break;
        }
    }
}


-(void)productsRequest:(SKProductsRequest *)request
    didReceiveResponse:(SKProductsResponse *)response
{
    SKProduct *validProduct = nil;
    int count = [response.products count];
    if (count>0) {


        validProducts = response.products;
        [validProducts retain];
        validProduct = [response.products objectAtIndex:0];
}

另外,对于新的测试用户示例,我注意到paymentQueueRestoreCompletedTransactionsFinished方法中的“被调用”的NSLog被调用了两次,而paymentQueue:updatedTransactions:方法的“恢复”根本没有被调用

如何修改我的代码以便正确恢复?

1 个答案:

答案 0 :(得分:0)

关注此:      #pragma mark SKPaymentTransactionOBserver

  - (void)storeRestoreTransactionsFinished:(NSNotification*)notification
  {
NSLog(@"- (void)storeRestoreTransactionsFinished:(NSNotification*)notification");
[apDelegate hideLoading];
  }

  - (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)restoreTransaction:(SKPaymentTransaction *)transaction {
NSLog(@"restoreTransaction...");

[self validateReceiptForTransaction:transaction];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
 }
 - (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];
}