鍍金池/ 教程/ iOS/ 恢復(fù)購買記錄
恢復(fù)購買記錄
內(nèi)置購買
介紹
準(zhǔn)備應(yīng)用程序?qū)彶?/span>
創(chuàng)建和配置產(chǎn)品
獲取產(chǎn)品信息
支付請求
使用訂閱
如何測試應(yīng)用程序內(nèi)置購買
產(chǎn)品交付

恢復(fù)購買記錄

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.

用戶恢復(fù)交易來保持訪問他們已經(jīng)購買過的內(nèi)容。 比如,當(dāng)他們換了一個新手機,他們不會丟失任何他們已經(jīng)在舊手機上購買過的產(chǎn)品。包括一些應(yīng)用中的機制讓用戶可以恢復(fù)他們的購買記錄,比如一個恢復(fù)購買按鈕。恢復(fù)購買記錄是提示用戶的應(yīng)用商店的憑據(jù),從而中斷你的應(yīng)用程序流程,因此不要自動恢復(fù)購買記錄,特別是在每次應(yīng)用程序啟動時。

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:

在大多數(shù)情況下,你的應(yīng)用程序只需要刷新它的收據(jù)并在收據(jù)中傳遞產(chǎn)品。 刷新完的收據(jù)中包含了一個用戶在該應(yīng)用中在該設(shè)備或其它設(shè)備中購買的記錄。然而,一些應(yīng)用程序因為以下原因需要采取另一種方法:

  • If you use Apple-hosted content, restoring completed transactions gives your app the transaction objects it uses to download the content.

如果你使用蘋果托管內(nèi)容,對于恢復(fù)已完成的交易,蘋果將給你的應(yīng)用提供一些用來下載內(nèi)容的交易對象。

  • If you need to support versions of iOS earlier than iOS 7, where the app receipt isn’t available, restore completed transactions instead.

如果你需要支持 iOS7 以前的版本,需要存儲已完成的所有交易,因為以前的版本不支持應(yīng)用收據(jù)功能。

  • If your app uses non-renewing subscriptions, your app is responsible for the restoration process.

如果你的應(yīng)用使用非更新訂閱,應(yīng)用程序負責(zé)恢復(fù)過程。

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.

刷新收據(jù)是請求應(yīng)用商店中最近的收據(jù)副本。 刷新一個收據(jù)并不會創(chuàng)建任何新的交易。 盡管你應(yīng)該避免在同一行刷新多次,多次刷新跟一次刷新是一樣的。

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.

恢復(fù)已完成的交易就是為用戶做的每個已完成的交易創(chuàng)建一個新的交易,本質(zhì)上是重復(fù)你的交易隊列觀察者的歷史。 當(dāng)交易被恢復(fù)時,應(yīng)用程序維護自己的狀態(tài)來監(jiān)控它為什么要恢復(fù)交易,以及它需要如何處理它們。多次恢復(fù)將為每個已完成的交易創(chuàng)建多個恢復(fù)的交易。

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.

注意:如果用戶嘗試著重新購買已經(jīng)購買過的產(chǎn)品,而不是使用應(yīng)用程序中的恢復(fù)界面,應(yīng)用商店就創(chuàng)建一個常規(guī)的交易而不是一個恢復(fù)交易。用戶不需要再次支付該產(chǎn)品。 對待這些交易跟原來的交易一模一樣。

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.

適當(dāng)控制用戶可以重新下載的內(nèi)容。 比如,不能一次下載三年量的日報,或者不能一次性下載10M大小的游戲關(guān)卡。

Refreshing the App Receipt

一、刷新應(yīng)用收據(jù)

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.

創(chuàng)建一個收據(jù)刷新請求,設(shè)置一個委托,并開啟該請求。 請求在測試中支持很多可選特性來獲取各種狀態(tài)的收據(jù),比如到期的收據(jù)---詳情請參見 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.

收據(jù)刷新了之后,檢查它并傳遞任何添加的產(chǎn)品。

Restoring Completed Transactions

二、恢復(fù)已完成的交易

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 estoreCompletedTransactionsWithApplicationUsernamer: method to provide the same information when restoring transactions.

應(yīng)用程序通過調(diào)用 SKPaymentQueue 類的 restoreCompletedTransactions 方法來開始該過程。 這樣做就是給應(yīng)用商店發(fā)送一個請求讓它來恢復(fù)或有應(yīng)用程序中的已完成交易。 如果應(yīng)用程序給它的支付請求(payment requests)的applicationUsername特性設(shè)置了一個值,正如In-App Purchase Programming Guide----(四) ----Requesting Payment 中的 “Detecting Irregular Activity,” 中所描述,在恢復(fù)交易時使用 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.

應(yīng)用商店為每個已經(jīng)購買完成的交易生產(chǎn)一個新的交易。 恢復(fù)的交易有一個指向最初交易的引用:SKPaymentTransaction 的實例有一個 originalTransaction 特性, 并且收據(jù)的項目 (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.

注意:恢復(fù)的購買記錄中的日期字段跟初始交易記錄中的有稍微不同的意義。具體詳情,請看 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.

應(yīng)用程序調(diào)用交易隊列觀察者,它帶有每個恢復(fù)交易的 SKPaymentTransactionStateRestored 狀態(tài)。詳情請看:Waiting for the App Store to Process Transactions“.” 這時你所需要做的操作取決于應(yīng)用程序的設(shè)計。

  • 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.

如果你的應(yīng)用程序使用應(yīng)用收據(jù)并且沒有蘋果托管內(nèi)容,則不需要該代碼,因為你的應(yīng)用程序沒有恢復(fù)已完成的交易。 立即結(jié)束任何的恢復(fù)交易。

  • 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.

如果你的應(yīng)用程序使用應(yīng)用收據(jù)并且有蘋果托管內(nèi)容,在開始恢復(fù)進程前讓用戶選擇恢復(fù)哪些產(chǎn)品。 在恢復(fù)過程中,重新下載用戶選擇的內(nèi)容并立即結(jié)束任何其它交易。

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.

如果你的應(yīng)用沒有使用應(yīng)用收據(jù),它將檢查所有的已完成交易并把它們作為已恢復(fù)交易。 它使用跟初始購買邏輯相似的代碼路徑來激活產(chǎn)品,然后結(jié)束交易。

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.

帶有多個產(chǎn)品的應(yīng)用程序,特別是跟內(nèi)容相關(guān)的產(chǎn)品,讓用戶選擇恢復(fù)哪些產(chǎn)品,而不是一次性恢復(fù)所有交易。 這些應(yīng)用保持監(jiān)控哪些已完成交易需要被處理為已經(jīng)恢復(fù),哪些交易可以忽視并立即結(jié)束交易。