2012-07-03 76 views
1

我已经实现了在应用程序购买使用MKStoreManager.Now从我的应用程序之一,从苹果新的指导方针,如果你在应用程序购买,你必须给用户,恢复已经购买的应用程序的选项。所以我做了这样的事情。点击'恢复'按钮,调用此方法。实现在应用程序购买和它的恢复在iphone

- (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]; 
     } 

    } 

但现在我有一个疑问,我怎么能检查这个恢复提前工作或not.Can任何人指导me.thanks。

+0

您可以通过在itunesConnect创建测试用户检查。使用测试用户购买你的应用程序并删除你的应用程序并检查恢复是否正常 – Sumanth

+0

@Sumanth我已经为此创建了一个沙箱帐户。可以知道我使用的代码是正确的还是不是用于恢复? –

+0

并且在使用我创建的沙箱帐户进行测试时,我收到了此警告:“此帐户没有权限在应用内购买,您可以在iTunes Connect中更改帐户权限。” –

回答

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 
}