Restoring Purchased Products
Users restore transactions to maintain access to content they’ve already purchased. For example, when they upgrade to a new phone, they don’t lose all of the items they purchased on the old phone. Include some mechanism in your app to let the user restore their purchases, such as a Restore Purchases button. Restoring purchases prompts for the user’s App Store credentials, which interrupts the flow of your app: because of this, don’t automatically restore purchases, especially not every time your app is launched.
用户恢复交易来保持访问他们已经购买过的内容。 比如,当他们换了一个新手机,他们不会丢失任何他们已经在旧手机上购买过的产品。包括一些应用中的机制让用户可以恢复他们的购买记录,比如一个恢复购买按钮。恢复购买记录是提示用户的应用商店的凭据,从而中断你的应用程序流程,因此不要自动恢复购买记录,特别是在每次应用程序启动时。
In most cases, all your app needs to do is refresh its receipt and deliver the products in its receipt. The refreshed receipt contains a record of the user’s purchases in this app, on this device or any other device. However, some apps need to take an alternate approach for one of the following reasons:
在大多数情况下,你的应用程序只需要刷新它的收据并在收据中传递产品。 刷新完的收据中包含了一个用户在该应用中在该设备或其它设备中购买的记录。然而,一些应用程序因为以下原因需要采取另一种方法:
-
If you use Apple-hosted content, restoring completed transactions gives your app the transaction objects it uses to download the content.
如果你使用苹果托管内容,对于恢复已完成的交易,苹果将给你的应用提供一些用来下载内容的交易对象。
-
If you need to support versions of iOS earlier than iOS 7, where the app receipt isn’t available, restore completed transactions instead.
如果你需要支持iOS7以前的版本,需要存储已完成的所有交易,因为以前的版本不支持应用收据功能。
-
If your app uses non-renewing subscriptions, your app is responsible for the restoration process.
如果你的应用使用非更新订阅,应用程序负责恢复过程。
Refreshing the receipt asks the App Store for the latest copy of the receipt. Refreshing a receipt does not create any new transactions. Although you should avoid refreshing multiple times in a row, this action would have same result as refreshing it just once.
刷新收据是请求应用商店中最近的收据副本。 刷新一个收据并不会创建任何新的交易。 尽管你应该避免在同一行刷新多次,多次刷新跟一次刷新是一样的。
Restoring completed transactions creates a new transaction for every completed transaction the user made, essentially replaying history for your transaction queue observer. While transactions are being restored, your app maintains its own state to keep track of why it’s restoring completed transactions and how it needs to handle them. Restoring multiple times creates multiple restored transactions for each completed transaction.
恢复已完成的交易就是为用户做的每个已完成的交易创建一个新的交易,本质上是重复你的交易队列观察者的历史。 当交易被恢复时,应用程序维护自己的状态来监控它为什么要恢复交易,以及它需要如何处理它们。多次恢复将为每个已完成的交易创建多个恢复的交易。
Note: If the user attempts to purchase a product that’as already been purchased, rather than using your app’s restoration interface, the App Store creates a regular transaction instead of a restore transaction. The user isn’t charged again for the product. Treat these transactions the exact same way you treated the original transactions.
注意:如果用户尝试着重新购买已经购买过的产品,而不是使用应用程序中的恢复界面,应用商店就创建一个常规的交易而不是一个恢复交易。用户不需要再次支付该产品。 对待这些交易跟原来的交易一模一样。
Give the user an appropriate level of control over what content is redownloaded. For example, don’t download three years worth of daily newpapers or hundreds of megabytes worth of game levels all at once.
适当控制用户可以重新下载的内容。 比如,不能一次下载三年量的日报,或者不能一次性下载10M大小的游戏关卡。
Refreshing the App Receipt
一、刷新应用收据
Create a receipt refresh request, set a delegate, and start the request. The request supports optional properties for obtaining receipts in various states during testing such as expired receipts—for details, see the values for the initWithReceiptProperties:
method of SKReceiptRefreshRequest
.
创建一个收据刷新请求,设置一个委托,并开启该请求。 请求在测试中支持很多可选特性来获取各种状态的收据,比如到期的收据---详情请参见 SKReceiptRefreshRequest 类的initWithReceiptProperties: 方法的值:
request = [[SKReceiptRefreshRequest alloc] init]; |
request.delegate = self; |
[request start]; |
After the receipt is refreshed, examine it and deliver any products that were added.
收据刷新了之后,检查它并传递任何添加的产品。
Restoring Completed Transactions
二、恢复已完成的交易
Your app starts the process by calling the restoreCompletedTransactions
method of SKPaymentQueue
. This sends a request to the App Store to restore all of your app’s completed transactions. If your app sets a value for the applicationUsername
property of its payment requests, as described in “Detecting Irregular Activity,” use the restoreCompletedTransactionsWithApplicationUsername:
method to provide the same information when restoring transactions.
应用程序通过调用 SKPaymentQueue类的
restoreCompletedTransactions 方法来开始该过程。 这样做就是给应用商店发送一个请求让它来恢复或有应用程序中的已完成交易。 如果应用程序给它的支付请求(payment requests)的applicationUsername特性设置了一个值,正如In-App Purchase Programming Guide----(四) ----Requesting Payment 中的 “Detecting Irregular Activity,” 中所描述,在恢复交易时使用
restoreCompletedTransactionsWithApplicationUsername:
方法来提供相同的信息。
The App Store generates a new transaction for each transaction that was previously completed. The restored transaction has a reference to the original transaction: instances of SKPaymentTransaction
have a originalTransaction
property, and the entries in the receipt have an Original Transaction Identifier field.
应用商店为每个已经购买完成的交易生产一个新的交易。 恢复的交易有一个指向最初交易的引用:SKPaymentTransaction
的实例有一个 originalTransaction
特性, 并且收据的项目(entries)中有一个初始交易识别码字段。
Note: The date fields have slightly different meanings for restored purchases. For details, see the Purchase Date and Original Purchase Date fields in Receipt Validation Programming Guide.
注意:恢复的购买记录中的日期字段跟初始交易记录中的有稍微不同的意义。具体详情,请看Receipt Validation Programming Guide 中的购买日期和初始购买日期字段。
Your transaction queue observer is called with a status of SKPaymentTransactionStateRestored
for each restored transaction, as described in “Waiting for the App Store to Process Transactions.” The action you take at this point depends on the design of your app.
应用程序调用交易队列观察者,它带有每个恢复交易的SKPaymentTransactionStateRestored 状态。详情请看:“Waiting for the App Store to Process Transactions.” 这时你所需要做的操作取决于应用程序的设计。
-
If your app uses the app receipt and doesn’t have Apple-hosted content, this code isn’t needed because your app doesn’t restore completed transactions. Finish any restored transactions immediately.
如果你的应用程序使用应用收据并且没有苹果托管内容,则不需要该代码,因为你的应用程序没有恢复已完成的交易。 立即结束任何的恢复交易。
-
If your app uses the app receipt and has Apple-hosted content, let the user select which products to restore before starting the restoration process. During restoration, re-download the user-selected content and finish any other transactions immediately.
如果你的应用程序使用应用收据并且有苹果托管内容,在开始恢复进程前让用户选择恢复哪些产品。 在恢复过程中,重新下载用户选择的内容并立即结束任何其它交易。
NSMutableArray *productIDsToRestore = <# From the user #>;
SKPaymentTransaction *transaction = <# Current transaction #>;
if ([productIDsToRestore containsObject:transaction.transactionIdentifier]) {
// Re-download the Apple-hosted content, then finish the transaction
// and remove the product identifier from the array of product IDs.
} else {
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
-
If your app doesn’t use the app receipt, it examines all completed transactions as they’re restored. It uses a similar code path to the original purchase logic to make the product available and then finishes the transaction.
如果你的应用没有使用应用收据,它将检查所有的已完成交易并把它们作为已恢复交易。 它使用跟初始购买逻辑相似的代码路径来激活产品,然后结束交易。
Apps with more than a few products, especially products with associated content, let the user select which products to restore instead of restoring everything all at once. These apps keep track of which completed transactions need to be processed as they’re restored and which transactions can be ignored by finishing them immediately.
带有多个产品的应用程序,特别是跟内容相关的产品,让用户选择恢复哪些产品,而不是一次性恢复所有交易。 这些应用保持监控哪些已完成交易需要被处理为已经恢复,哪些交易可以忽视并立即结束交易。