1

我有一个用于“保存功能”的非消耗性 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:根本没有调用从该方法“恢复”的 NSLog。

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

4

1 回答 1

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];
}
于 2014-08-11T11:55:28.333 回答