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 therestoreCompletedTransactionsmethod ofSKPaymentQueue. 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 inDetecting Irregular Activity“,” use theestoreCompletedTransactionsWithApplicationUsernamer: 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 ofSKPaymentTransactionhave aoriginalTransactionproperty, 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 inReceipt Validation Programming Guide.
注意:恢復的購買記錄中的日期字段跟初始交易記錄中的有稍微不同的意義。具體詳情,請看Receipt Validation Programming Guide中的購買日期和初始購買日期字段。
Your transaction queue observer is called with a status ofSKPaymentTransactionStateRestoredfor each restored transaction, as described inWaiting 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.
帶有多個產品的應用程序,特別是跟內容相關的產品,讓用戶選擇恢復哪些產品,而不是一次性恢復所有交易。? 這些應用保持監控哪些已完成交易需要被處理為已經恢復,哪些交易可以忽視并立即結束交易。