2012-07-03 3 views
1

Я реализовал покупку приложения в одном из своих приложений с помощью MKStoreManager. Теперь вы получили новое руководство от Apple, которое, если вы занимаетесь покупкой приложения, вы должны предоставить пользователю, вариант для восстановления уже приобретенного приложения. Так что я сделал это так. Нажав кнопку «restore», вызывается этот метод.Реализация в покупке приложения и его восстановление в 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 
} 
Смежные вопросы