iphone - 在应用程序购买中实现并在iPhone中恢复

标签 iphone ios xcode4.2 in-app-purchase restore

我已经使用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];
        }

    }

但是现在我有一个疑问,我该如何检查这个修复是否正常工作。有人可以事先指导我。谢谢。

最佳答案

这是实现恢复的方法

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

关于iphone - 在应用程序购买中实现并在iPhone中恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11304317/

相关文章:

iphone - pjsip在xcode4.2上的编译问题

objective-c - 未知类型名称 uint32/uint16

iphone - 基于属性加载 View 的背景图像

iphone - YouTube在UITableView中

iphone - 用户启动时应用崩溃,无法复制错误

ios - Pod安装Realm 3.1.1失败

objective-c - 通过 USB 安装 IPA?

iPhone SDK 连接 iTunes

ios - Swift 3 转换

ios - SpriteKit 中的 CGAffineTransform