1

我已经使用 MKStoreManager 在我的一个应用程序中实现了应用程序购买。现在从苹果那里得到了一个新的指导方针,如果你在应用程序购买,你必须给用户,恢复已经购买的应用程序的选项。所以我已经完成了像这样。在“恢复”按钮单击时,调用此方法。

- (void) checkPurchasedItems
{
   [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; 
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}

从这里开始,这个方法被解雇了

- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
    {
     NSMutableArray* purchasableObjects  = [[NSMutableArray alloc] init];

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

    }

但现在我有一个疑问,我怎样才能检查这个恢复是否有效。任何人都可以指导我。提前谢谢。

4

1 回答 1

3

这是您实施恢复的方式

- (void)loadStore
{
// restarts any purchases if they were interrupted last time the app was open
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];

// get the product description (defined in early sections)
[self requestProUpgradeProductData];
}

- (void)requestProUpgradeProductData
{
NSSet *productIdentifiers = [NSSet setWithObject:kInAppPurchaseProUpgradeProductId];
productsRequest = [[SKProductsRequest alloc]     initWithProductIdentifiers:productIdentifiers];
productsRequest.delegate = self;
[productsRequest start];

// we will release the request object in the delegate callback
}

在此之后它将调用此方法

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSArray *products = response.products;
proUpgradeProduct = [products count] == 1 ? [[products objectAtIndex:0] retain] : nil;
if (proUpgradeProduct)
{
NSLog(@"Product title: %@" , proUpgradeProduct.localizedTitle);
NSLog(@"Product description: %@" , proUpgradeProduct.localizedDescription);
NSLog(@"Product price: %@" , proUpgradeProduct.price);
NSLog(@"Product id: %@" , proUpgradeProduct.productIdentifier);
if ([self canMakePurchases]) {
    if ([self respondsToSelector:@selector(purchaseProUpgrade)]) {
        [self purchaseProUpgrade];
    }
}
else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[self languageSelectedStringForKey:@"Error"] message:@"Cannot connect to Store.\n Please Enable the Buying in settings" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}
}

for (NSString *invalidProductId in response.invalidProductIdentifiers)
{
[SVProgressHUD dismiss];
[cancelButton setEnabled:YES];
[buyNowButton setEnabled:YES];
[restoreButton setEnabled:YES];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Error occured" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
NSLog(@"Invalid product id: %@" , invalidProductId);
}

// finally release the reqest we alloc/init’ed in requestProUpgradeProductData
[productsRequest release];
[[NSNotificationCenter defaultCenter]         postNotificationName:kInAppPurchaseManagerProductsFetchedNotification object:self userInfo:nil];
}
- (void)completeTransaction:(SKPaymentTransaction *)transaction
{
[self recordTransaction:transaction];
[self provideContent:transaction.payment.productIdentifier];
[self finishTransaction:transaction wasSuccessful:YES];
}
- (void)purchaseProUpgrade
{
[SVProgressHUD showInView:self.view status:[self languageSelectedStringForKey:@"Connecting Store"] networkIndicator:YES];
SKPayment *payment = [SKPayment     paymentWithProductIdentifier:kInAppPurchaseProUpgradeProductId];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (void)recordTransaction:(SKPaymentTransaction *)transaction
{
if ([transaction.payment.productIdentifier isEqualToString:kInAppPurchaseProUpgradeProductId])
{
// save the transaction receipt to disk
[[NSUserDefaults standardUserDefaults] setValue:transaction.transactionReceipt forKey:@"proUpgradeTransactionReceipt" ];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}

最后这个方法

- (void)finishTransaction:(SKPaymentTransaction *)transaction wasSuccessful:        (BOOL)wasSuccessful
{
// remove the transaction from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];

NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:transaction, @"transaction" , nil];
if (wasSuccessful)
{
//Write your transaction complete statement required for your project
}
于 2012-07-03T05:01:28.277 回答